关注小众语言、AI,记录、分享技术点滴!

0%

centos下安装nginx_lua_module模块

ngx_lua_module 是一个nginx http模块,它把 lua 解析器内嵌到 nginx,用来解析并执行lua 语言编写的网页后台脚本。

特性:
支持Windows和Linux平台。
支持高并发高性能。
HTML网页中内嵌LUA脚本代码,类似于PHP。
支持非阻塞的数据库操作,目前只支持MYSQL。
支持异步的文件IO操作。
支持非阻塞的SOCKET IO操作。

方式一:使用集成包安装

下载 ngx_openresty,该集成包中有:Nginx,Lua或Luajit,ngx_lua,以及一些有用的Nginx第三方模块。

1
2
3
4
wget https://github.com/openresty/ngx_openresty  
./configure --with-luajit
make
make install

方式二:Ngx_lua可以手动编译进Nginx(或Tengine)

假定nginx的安装路径为:/usr/local/nginx

所需要的模块如下:
luajit2 https://github.com/openresty/luajit2
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

安装步骤:

1、安装openresty提供的luajit2

1
2
3
4
5
wget -O luajit2.zip https://codeload.github.com/openresty/luajit2/zip/v2.1-agentzh  
unzip luajit2.zip
cd luajit2
make
make install

下面配置 luajit的环境变量(Nginx编译时需要)

1
2
export LUAJIT_LIB=/usr/local/lib  
export LUAJIT_INC=/usr/local/include/luajit-2.1

2、安装 ngx_devel_kit (NDK) 模块

1
2
wget -O ngx_devel_kit.zip https://codeload.github.com/simplresty/ngx_devel_kit/zip/master  
unzip ngx_devel_kit.zip -d /usr/local

3、安装 lua-nginx-module 模块

1
2
wget -O lua-nginx-module.zip https://codeload.github.com/openresty/lua-nginx-module/zip/master  
unzip lua-nginx-module.zip -d /usr/local

4、重新编译Nginx,需要注意编译顺序

1
2
3
4
5
6
7
./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

注意:重新编译 Nginx 二进制,Nginx 需要 quit 再启动。而普通配置更新则 reload 即可。

quit

1
kill -HUP cat /usr/local/nginx/logs/nginx.pid

reload

1
/usr/local/nginx/sbin/nginx -s reload

模块编译完成!

在编译安装 Nginx 的第三方模块时,碰到一个错误:

1
2
/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

解决办法。
在 Nginx 编译时,需要指定 RPATH,加入下面选项即可

1
./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"

或者

1
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH