nginx 中添加 lua 模块,支持lua脚本以及遇到的坑

参考文档:https://www.cnblogs.com/yulibostu/articles/10529989.html
工具版本说明

nginxluajitngx_devel_kitlua-nginx-module备注
V1.17.5V2.0.4v0.3.0v0.10.9rc7强烈建议,除了nginx版本之外,其他工具版本都使用跟我这个一样的,我是各种组合版本试错的过来人

一、安装带lua模块的nginx

nginx上面一共需要安装3个东西:luajit,ngx_devel_kit,lua-nginx-module

1.下载安装LuaJIT-2.0.4.tar.gz

这里有点小坑,从luajit.org中下载的luaJIT nginx启动会报warn,可以看后面的排坑技巧

cd /data/soft/nginx

wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz

tar xzvf LuaJIT-2.0.4.tar.gz

cd LuaJIT-2.0.4

make install PREFIX=/usr/local/luajit

 
#添加环境变量!

export LUAJIT_LIB=/usr/local/luajit/lib

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

2.下载解压ngx_devel_kit

cd /data/soft/nginx

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

tar -xzvf v0.3.0.tar.gz

3.下载解压lua-nginx-module

这里是重点啊!!!很多坑都是这个家伙带来的
一定要用v0.10.9rc7版本,我试了其他几个版本,比如说v0.10.8v0.10.15等,都是不行的,都会遇到下面说的那些坑,只有v0.10.9rc7不会。

cd /data/soft/nginx

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz

tar -xzvf v0.10.9rc7.tar.gz

4.下载nginx,编译安装

cd /data/soft/nginx

wget http://nginx.org/download/nginx-1.17.5.tar.gz

tar -xzvf nginx-1.17.5.tar.gz

我用的就是1.17.5这个版本的,我还有其他机子用的1.15.3,最好不要用太低的版本吧。。。

然后接着。

cd /data/soft/nginx/nginx-1.17.5

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-pcre --add-module=/data/soft/nginx/lua-nginx-module-0.10.9rc7 --add-module=/data/soft/nginx/ngx_devel_kit-0.3.0 --with-stream

make

make install

如果是在原有nginx上面加lua模块的话,记得只执行make就够了哦,不要执行make install,不然会覆盖原有的东西。

好了好了 ,搞完了。你试试看会不会报错。。反正我是没问题了。。报错报的脑壳痛。。

算了再写一个验证lua模块的代码吧。。

5.验证lua模块

vim /usr/local/nginx/conf/nginx.conf

###在server模块里面,随便找个地方添加下面的代码
location /lua {
            default_type 'text/plain';

            content_by_lua 'ngx.say("hello, lua")';
        }
        

在这里插入图片描述
然后

/usr/local/nginx/sbin/nginx -t

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

最后验证

curl 127.0.0.1:5580/lua

在这里插入图片描述

二.安装过程中遇到的各种坑

1.让我不要用这个luajit版本,可以用openresty提供的luajit优化版本,或者干脆直接用openresty

[alert] 12687#0: detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)

在这里插入图片描述
这个倒问题不大,是个warn而已,需要卸载之前的luajit,到他说的网站去下载。
先卸载原有luagit

cd /data/soft/nginx/LuaJIT-2.0.4
make uninstall

然后从https://github.com/openresty/luajit2下载安装

git clone https://github.com/openresty/luajit2
cd luajit2
make && make install

剩下的步骤跟一开始装luajit是一样的。具体的看自己的目录写。

export LUAJIT_LIB=/usr/local/lib

export LUAJIT_INC=/usr/local/include/luajit-2.1

然后重新编译nginx,懒得写了。。。跟前面一样

2. 说resty.core模块找不到

明明用起来是没啥问题的,但是他就是启动的时候报错,我都想骂人了。

[error] 12687#0: lua_load_resty_core failed to load the resty.core module from https://github.com/openresty/lua-resty-core; ensure you are using an OpenResty release from https://openresty.org/en/download.html (rc: 2, reason: module 'resty.core' not found:
	no field package.preload['resty.core']
	no file '/data/soft/soft_package/nginx/sockproc/lua-resty-shell/lib/resty/core.lua'
	no file './resty/core.lua'
	no file '/usr/local/LuaJIT/share/luajit-2.1.0-beta3/resty/core.lua'
	no file '/usr/local/share/lua/5.1/resty/core.lua'
	no file '/usr/local/share/lua/5.1/resty/core/init.lua'
	no file '/usr/local/LuaJIT/share/lua/5.1/resty/core.lua'
	no file '/usr/local/LuaJIT/share/lua/5.1/resty/core/init.lua'
	no file './resty/core.so'
	no file '/usr/local/lib/lua/5.1/resty/core.so'
	no file '/usr/local/LuaJIT/lib/lua/5.1/resty/core.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
	no file './resty.so'
	no file '/usr/local/lib/lua/5.1/resty.so'
	no file '/usr/local/LuaJIT/lib/lua/5.1/resty.so'
	no file '/usr/local/lib/lua/5.1/loadall.so')

在这里插入图片描述
这个问题到git上面看了 ,差不多是这个地方吧:https://github.com/openresty/lua-nginx-module/issues/1509
就是在nginx.conf 中的 http{}模块中加入下面这行代码:

lua_load_resty_core off;

之后nginx reload就不会报那个烦人的错了。

3. error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

这个错误好说,网上说了很多,有两种方法:
方法1:
执行下面的代码(注意前面的路径是你自己安装的luajit路径,正常安装跟我这个路径不一样哦)

ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

方法2:
执行下面的代码(同样还是注意前面的路径)

echo "/usr/local/luajit/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
  • 21
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值