通过yum 安装的nginx ,无法像源码安装添加模块只需make重新编译,需要通过平滑升级的方式,替换旧nginx 可执行文件。
查看Nginx已经加载的模块及版本,configure arguments参数非常重要,后面安装Lua模块的时候,需要基于这个为基础,添加新的模块
$ nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --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-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --with-ld-opt=-Wl,-rpath,/usr/local/LuaJIT/lib --add-module=/opt/ngx_devel_kit-0.3.0 --add-module=/opt/lua-nginx-module-0.10.13
$ cd /opt/ && wget http://nginx.org/download/nginx-1.20.1.tar.gz
$ tar -xf nginx-1.20.1.tar.gz
需要先安装Lua JIT,并且依赖ngx_devel_kit
安装Lua JIT LuaJIT官网
$ wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
$ tar -xf LuaJIT-2.0.5.tar.gz
$ cd LuaJIT-2.0.5/
$ make install PREFIX=/usr/local/LuaJIT
安装成功后最后一行出现
==== Successfully installed LuaJIT 2.0.5 to /usr/local/LuaJIT ====
添加环境变量
$ tail -2 /etc/profile
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
安装ngx_devel_kit 项目地址
安装lua-nginx-module 项目地址
下载解压,不需要安装
$ wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz
$ tar -xf v0.3.0.tar.gz
$ wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
$ tar -xf v0.10.13.tar.gz
编译Nginx
下载编译依赖工具包
$ yum install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel readline-devel openssl-devel libxml2 libxml2-dev libxslt-devel gperftools-devel redhat-rpm-config.noarch gd-devel
编译之前先备份
$ cp /usr/sbin/nginx /root
$ ./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --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-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --with-ld-opt=-Wl,-rpath,/usr/local/LuaJIT/lib --add-module=/opt/ngx_devel_kit-0.3.0 --add-module=/opt/lua-nginx-module-0.10.13
此过程可能会报错,提示缺少依赖模块,对应安装就行,可谷歌搜索,下面列举一些
Linux Nginx安装以及可能出现错误
安装完所有依赖后可正常编译安装
$ make -j2
$ /opt/nginx-1.20.1/objs/nginx -V # 查看nginx 是否添加了模块
$ make install # make install会覆盖之前安装nginx,不需要用到
$ systemctl stop nginx
$ cp /opt/nginx-1.20.1/objs/nginx /usr/sbin/
$ systemctl restart nginx && nginx -V
location /lua {
default_type 'text/plain';
return 200 'hello lua!';
}
location /hello_lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua!")';
}
curl https://acc.abc.com/lua
hello lua!
curl https://acc.abc.com/hello_lua
hello, lua!
Nginx安装lua-nginx-module模块