nginx安装lua支持步骤
1、下载相关安装包(luajit、ngx_devel_kit、lua-nginx-module)
wget https://github.com/LuaJIT/LuaJIT/archive/v2.0.4.tar.gz
wget https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20240626.tar.gz
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
wget --no-check-certificate https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.27.tar.gz
wget --no-check-certificate https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.12.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.25.tar.gz
2、解压(路径:/usr/local/lua)
tar -xzvf v2.1-20240626.tar.gz
tar -xzvf v0.3.0.tar.gz
tar -xzvf v0.1.27.tar.gz
tar -xzvf v0.12.tar.gz
tar -xzvf v0.10.9rc7.tar.gz
3、安装
cd luajit2-2.1-20240626
make install PREFIX=/usr/local/lua/LuaJIT
显示如下,则是安装成功了
cd ../lua-resty-core-0.1.27
make install PREFIX=/usr/local/lua/lua_core
cd ../lua-resty-lrucache-0.12
make install PREFIX=/usr/local/lua/lua_core
4、添加环境变量
vi /etc/profile
将命令添加到文件里面
export LUAJIT_LIB=/usr/local/lua/LuaJIT/lib
export LUAJIT_INC=/usr/local/lua/LuaJIT/include/luajit-2.0
执行环境变量
source /etc/profile
查看是否生效
export
如下图是已经配置完成并且生效的
5、查看nginx以前编辑的配置信息
nginx -V
原始配置信息如下
6、覆盖安装nginx(此处建议先把以前的nginx备份一下以防意外哦)
下载对应版本的nginx
wget -c https://nginx.org/download/nginx-1.26.0.tar.gz
解压
tar -xzvf nginx-1.26.0.tar.gz
进入nginx文件夹
cd nginx-1.26.0
指定拓展安装位置
./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-pcre=/usr/local/lua/pcre-8.33 --add-module=/usr/local/lua/ngx_devel_kit-0.3.0 --add-module=/usr/local/lua/lua-nginx-module-0.10.25
编译
make
成功如下:
安装
make && make install
成功如下:
7、验证Lua
在server模块里面,随便找个地方添加下面的代码
location /lua { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; }
启动nginx
/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
测试
curl 172.16.123.79/lua
8、报错及处理如下:
8.1 nginx编译报错
下载pcre
wget http://downloads.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
解压
tar -xzvf pcre-8.33.tar.gz
文件夹赋权
chmod -R 777 pcre-8.33
进入文件夹
cd pcre-8.33
初始化配置
./configure
编译
make
安装
make install
进入测试
./pcretest
8.2 nginx编译报错
编译nginx前运行
export CFLAGS="-fPIC"
export CPPFLAGS="$CFLAGS"
8.3 nginx启动报错
报错如下:
查找文件
find / -name libluajit-5.1.so.2
将文件复制到/usr/local/lib/
cp /查出来的文件路径/libluajit-5.1.so.2 /usr/local/lib/
echo "/usr/local/lib" >>/etc/ld.so.conf
/sbin/ldconfig
8.4 nginx启动报错
复制
cp -a /usr/local/lua/lua_core/lib/lua/resty/* /usr/local/lua/LuaJIT/share/luajit-2.1/resty
9、core和lua版本对应
wget https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.29.tar.gz
wget https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.14.tar.gz
只能用于0.10.27版本
wget https://github.com/openresty/lua-resty-core/archive/refs/tags/v0.1.22.tar.gz
wget https://github.com/openresty/lua-resty-lrucache/archive/refs/tags/v0.11.tar.gz
只能用于0.10.19 或 0.10.20版本