ngx_lua_module is a nginx http module, which embeds the lua parser into nginx, which is used to parse and execute web background scripts written in lua language.
Features:
Support Windows and Linux platforms.
Support high concurrency and high performance.
The LUA script code is embedded in the HTML webpage, similar to PHP.
Supports non-blocking database operations, currently only supports MYSQL.
Supports asynchronous file IO operations.
Support non-blocking SOCKET IO operation.
The following briefly introduces the installation of the ngx_lua_module module.
Method 1: Use the integration package provided by Daniel ZhangYichun to install quickly.
Very simple, download ngx_openresty, the integrated package includes: Nginx, Lua or Luajit, ngx_lua, and some useful third-party Nginx modules.
Installation steps:
./configure –with-luajit
make
make install
The installation is complete!
Method 2: Ngx_lua can be manually compiled into Nginx.
First, my Nginx installation path is: /usr/local/nginx.
Two modules I will try to compile: echo, lua. The required modules are as follows:
liujit http://luajit.org
lua http://www.lua.org
ngx_devel_kit https://github.com/simpl/ngx_devel_kit
echo-nginx-module https://github.com/agentzh/echo-nginx-module
lua-nginx-module https://github.com/chaoslawful/lua-nginx-module
Installation steps:
1. Luajit2.0 (recommended) or Lua5.1 (Lua5.2 is not supported yet)
wget http://luajit.org/download/LuaJIT-2.0.0-beta9.tar.gz
tar zxvf LuaJIT-2.0.0-beta9.tar.gz
cd LuaJIT-2.0.0-beta9
make
sudo make install PREFIX=/usr/local/luajit
Note: to avoid overwriting a previous version,
The beta test releases only install the LuaJIT executable under the versioned name (i.e. luajit-2.0.0-beta10).
You probably want to create a symlink for convenience, with a command like this:
sudo ln -sf luajit-2.0.0-beta9 /usr/local/bin/luajit (add this command)
Next, you need to configure the environment variables of luajit or lua (needed when Nginx is compiled):
— luajit —
# tell nginx’s build system where to find LuaJIT:
export LUAJIT_LIB=/path/to/luajit/lib
export LUAJIT_INC=/path/to/luajit/include/luajit-2.0
— lua —
# or tell where to find Lua if using Lua instead:
export LUA_LIB=/path/to/lua/lib
export LUA_INC=/path/to/lua/include
In my test environment, the configuration is as follows:
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
2. Install ngx_devel_kit (NDK) module
cd /usr/local
git clone https://github.com/simpl/ngx_devel_kit.git
After the download is complete, a subdirectory ngx_devel_kit will be generated under the /usr/local/ directory.
3. Install lua-nginx-module module
cd /usr/local
git clone https://github.com/chaoslawful/lua-nginx-module.git
After the download is complete, a subdirectory lua-nginx-module will be generated under the /usr/local/ directory.
4. RecompileNginx, you need to pay attention to the compilation order!
./configure –prefix=/usr/local/nginx \
–with-ld-opt=”-Wl,-rpath,$LUAJIT_LIB” \
–add-module=/usr/local/ngx_devel_kit \
–add-module=/usr/local/echo-nginx-module \
–add-module=/usr/local/lua-nginx-module
make -j2
make install
Note: Recompile the Nginx binary, Nginx needs to quit and restart. Ordinary configuration updates can be reloaded: kill -HUP `cat /path/nginx/logs/nginx.pid`
/usr/local/nginx/sbin/nginx -s reload
Module compiled successfully! Restart the Nginx server!
When I compiled and installed the third-party module of Nginx, I encountered an error:
/usr/local/nginx/sbin/ngxin -s reload
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
Pepsi was puzzled, and later Google found a solution.
When compiling Nginx, you need to specify RPATH, just add the following options:
./configure –with-ld-opt=”-Wl,-rpath,$LUAJIT_LIB”
or
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
5. TestLua
In the Nginx.conf configuration file, add the following code:
location /echo {
default_type ‘text/plain’;
echo ‘hello echo’;
}
location /lua {
default_type ‘text/plain’;
content_by_lua ‘ngx.say(“hello, lua”)’;
}
Restart the Nginx server: /usr/local/nginx/sbin/nginx -s reload
Test withcurl:
[root@localhost] curl http://localhost/echo
hello echo
[root@localhost] curl http://localhost/lua
hello lua
The test results show that both modules are installed successfully! See below for detailed documentation about the ngx_lua module:
http://wiki.nginx.org/HttpLuaModule