特别感谢zhangyichun大佬的分享
安装步骤
安装luajit
[root@nginx-4 ~] wget https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20220111.tar.gz
[root@nginx-4 ~] tar -zxvf v2.1-20220111.tar.gz
[root@nginx-4 ~] cd luajit2-2.1-20220111
[root@nginx-4 luajit2-2.1-20220111] make && make install PREFIX=/opt/luajit
make比较慢,如果是多核平台可以使用
make -j<n>
,n
是核数量, 后面不在赘述
此处的安装路径为
/opt/luajit
,也可以选择自己的路径
安装后,输出两个环境变量
[root@nginx-4 ~] export LUAJIT_LIB=/opt/luajit/luajit/lib
[root@nginx-4 ~] export LUAJIT_INC=/opt/luajit/luajit/include/luajit-2.1
安装ngx_devel_kit
[root@nginx-4 ~] wget https://github.com/vision5/ngx_devel_kit/archive/refs/tags/v0.3.1.tar.gz
[root@nginx-4 ~] tar -zxvf v0.3.1.tar.gz
[root@nginx-4 ~] cd ngx_devel_kit-0.3.1
[root@nginx-4 ngx_devel_kit-0.3.1] make && make install PREFIX=/opt/luajit
下载ngx_lua
[root@nginx-4 ~] wget https://github.com/openresty/lua-nginx-module/archive/refs/tags/v0.10.21rc1.tar.gz
[root@nginx-4 ~] tar -zxvf v0.10.21rc1.tar.gz
安装nginx
从官网下载nginx源码
[root@nginx-4 ~] wget
[root@nginx-4 ~] tar -zxvf nginx-1.21.5.tar.gz
[root@nginx-4 ~] cd nginx-1.21.5
[root@nginx-4 nginx-1.21.5] ./configure --prefix=/opt/nginx --add-module=~/ngx_http_auth_pam_module --add-dynamic-module=~/lua-nginx-module-0.10.21rc1 --add-dynamic-module=~/ngx_devel_kit-0.3.1 --with-pcre --with-stream --with-http_ssl_module
[root@nginx-4 nginx-1.21.5] make && make install
pcre模块被lua模块依赖,一定要,否则会pcre函数找不到,下面有列出遇到的问题
其他模块根据自己的情况安装,例如本处还添加了ssl模块
dynamic-module
是从ng 1.9.11+开始支持,之前的版本请直接使用add-module
如果使用dynamic-module
编译,需要在nginx.conf
文件中使用load_module
来添加使用:
load_module /opt/nginx/modules/ngx_http_lua_module.so;
安装lua-resty-core和lua-resty-lrucache
lua-resty-core和lua-resty-lrucache被ngx_lua依赖,需要手动安装
-
[root@nginx-4 ~] wget https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.11.tar.gz [root@nginx-4 ~] tar -zxvf v0.11.tar.gz [root@nginx-4 ~] cd lua-resty-core-0.1.23rc1 [root@nginx-4 lua-resty-core-0.1.23rc1] make install PREFIX=/opt/nginx
-
[root@nginx-4 ~] wget https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.23rc1.tar.gz [root@nginx-4 ~] tar -zxvf v0.1.23rc1.tar.gz [root@nginx-4 ~] cd lua-resty-lrucache-0.11 [root@nginx-4 lua-resty-lrucache-0.11] make install PREFIX=/opt/nginx
在nginx.conf的http部分将lua模块引入
lua_package_path "/opt/nginx/lib/lua/?.lua;;";
测试lua安装
新增一个lua脚本
cat >> /opt/nginx/lua/hell_lua.lua << EOF
ngx.say("hello lua");
EOF
在nginx.conf中新增一个http块
location /lua {
default_type 'text/html';
content_by_lua_file lua/hell_lua.lua; #相对于nginx安装目录
}
执行 nginx -s reload
重新加载配置,打开浏览器输入 ip/lua
,页面响应 hello lua,即表示安装成功
常见问题
-
启动nginx报错
libluajit-5.1.so.2: cannot open shared object file: No such file or directory
选择手动安装luajit,安装的时候使用的lib库链接是用的环境变量的方式:[root@nginx-4 nginx]# export LUAJIT_LIB=/opt/luajit/lib [root@nginx-4 nginx]# export LUAJIT_INC=/opt/luajit/include/luajit-2.1
因此编译nginx没有问题,但是在运行 nginx -t检查就会出现找不到libluajit库的问题:
[root@nginx-4 nginx]# ./sbin/nginx -tnginx: [emerg] dlopen() "/usr/local/nginx/modules/ngx_http_lua_module.so" failed (libluajit-5.1.so.2: cannot open shared object file: No such file or directory) in /usr/local/nginx/conf/nginx.conf:14
解决
使用id手动链接lib可以解决问题。[root@nginx-4 nginx]# touch /etc/ld.so.conf.d/luajit.conf[ root@nginx-4 nginx]# echo "/opt/luajit/lib" >> /etc/ld.so.conf.d/luajit.conf [root@nginx-4 ld.so.conf.d]# ldconfig
-
启动nginx报错
/usr/local/nginx/modules/ngx_http_lua_module.so: undefined symbol: pcre_malloc
找不到pcre_malloc
函数,看名字是依赖了pcre库
,查看安装的pcre库
:[root@nginx-4 ~]# rpm -qa | grep pcrepcre-8.32-17.el7.aarch64 pcre-devel-8.32-17.el7.aarch64 pcre2-10.23-2.el7.aarch64pcre2-utf16-10.23-2.el7.aarch64 openresty-pcre-8.44-2.el7.aarch64 pcre2-utf32-10.23-2.el7.aarch64 pcre2-devel-10.23-2.el7.aarch64
如果没有安装,可以使用
yum -y install pcre pcre-devel
安装如果无法使用
yum
安装,则可以使用离线安装pcre
的方式重新编译并安装nginx:
[root@nginx-4 nginx-1.21.5] ./configure --add-module=/root/ngx_http_auth_pam_module --add-dynamic-module=/root/lua-nginx-module-0.10.21rc1 --add-dynamic-module=/root/ngx_devel_kit-0.3.1 --with-pcre --with-stream --with-http_ssl_module [root@nginx-4 nginx-1.21.5]# make -j2 && make install
如果是手动安装的
pcre库
,则上述--with-pcre
必须改为--with-pcre=<pcre安装路径>
注意添加
lua库
安装的时候必须要配置luajit库
路径,在安装说明中的两个环境变量:LUAJIT_LIB
和LUAJIT_INC