Nginx 編譯安裝 lua nginx module 動態模組 - lyonwang/TechNotes GitHub Wiki
查看原本安裝的 nginx 的編譯參數
$sudo nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/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 --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'
下載 Nginx 原始碼(注意版本)
$ cd /opt/
$ wget http://nginx.org/download/nginx-1.14.2.tar.gz
$ tar -zxvf nginx-1.14.2.tar.gz
安裝 lua-nginx-module
安裝 LuaJIT
$ wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
$ tar -zxvf LuaJIT-2.0.5.tar.gz
$ cd LuaJIT-2.0.5
$ make install PREFIX=/usr/local/LuaJIT
設置 PATH: /etc/profile 文件結尾加上以下內容:
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
安裝 nginx_devel_kit (下載解壓不需安裝)
$ cd /opt/
$ wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz
$ tar zxvf v0.3.0.tar.gz
$ ls | grep ngx_devel_kit
ngx_devel_kit-0.3.0
編譯 Nginx
安裝 pcre 相依程式庫
$ yum install readline-devel pcre-devel openssl-devel
注意:重新编译 Nginx 二进制,Nginx 需要停止重启。而普通配置更新则 reload 即可:(因本文是用動態模組方式安裝,只須重載即可,不需停止)
$ sudo systemctl stop nginx
或者
$ sudo nginx -s stop
添加以下編譯參數於第一步取得編譯參數結尾
--with-ld-opt=-Wl,-rpath,/usr/local/LuaJIT/lib --add-dynamic-module=/opt/ngx_devel_kit-0.3.0 --add-dynamic-module=/opt/lua-nginx-module-0.10.13
成為以下 ./configure 的參數,並進行編譯與安裝
$ cd /opt/nginx-1.12.2/
$ ./configure --prefix=/etc/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 --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-ld-opt=-Wl,-rpath,/usr/local/LuaJIT/lib --add-dynamic-module=/opt/ngx_devel_kit-0.3.0 --add-dynamic-module=/opt/lua-nginx-module-0.10.13
$ make -j2
$ make install
编译成功后,会把模块安装在nginx/modules/目录。查看:
$ ls /etc/nginx/modules/
ndk_http_module.so ngx_http_lua_module.so
接下来我们需要在nginx.conf配置中加入以下两行,实现动态调用模块:
load_module /etc/nginx/modules/ndk_http_module.so;
load_module /etc/nginx/modules/ngx_http_lua_module.so;
載入模組區段:
worker_processes 1;
load_module xxx;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
}
重載 Nginx
$ sudo nginx -s reload