–
nginx使用lua模块有两种方式:一种是将lua模块编译到nginx执行文件中,在编译nginx的时候将lua模块文件位置添加到编译参数中。另外一种是使用openresty现成的源码进行编译,openresty已经将nginx和lua源码放在一起,直接编译就可以使用
下面使用将lua模块编译到nginx执行文件中这种方式。一般情况都是nginx正在运行,然后因为业务需求需要lua模块支持,然后重新将lua编译到nginx,然后将nginx热升级到新的版本
这里先模拟nginx正常运行,然后在此基础上进行升级
nginx首先编译 [root@master ~]#yum install -y gcc gcc-c++ autoconf automake \ libtool make cmake zlib zlib-devel openssl \ openssl-devel pcre-devel libxslt-devel gd-devel patch \ perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel curl [root@master ~]#wget http://nginx.org/download/nginx-1.12.1.tar.gz [root@master ~]#tar xf nginx-1.12.1.tar.gz [root@master ~]#cd nginx-1.12.1 [root@master ~/nginx-1.12.1]#./configure && make -j8 && make install [root@master /usr/local/nginx/sbin]#./nginx 到此nginx就正常运行了,然后进行升级nginx使用lua模块,下面是具体的升级过程 [root@master ~]#wget https://openresty.org/download/openresty-1.13.6.1.tar.gz [root@master ~]#ls openresty-1.13.6.1.tar.gz [root@master ~]#tar xf openresty-1.13.6.1.tar.gz -C /opt [root@master ~]#ls openresty-1.13.6.1.tar.gz [root@master ~]#cd /opt/openresty-1.13.6.1/ [root@master /opt/openresty-1.13.6.1]#ls bundle configure COPYRIGHT patches README.markdown README-win32.txt util 先编译安装LuaJIT [root@master /opt/openresty-1.13.6.1]#cd bundle/LuaJIT-2.1-20171103/ [root@master /opt/openresty-1.13.6.1/bundle/LuaJIT-2.1-20171103]#ls COPYRIGHT doc dynasm etc Makefile README src [root@master /opt/openresty-1.13.6.1/bundle/LuaJIT-2.1-20171103]#make [root@master /opt/openresty-1.13.6.1/bundle/LuaJIT-2.1-20171103]#make install PREFIX=/usr/local/luajit 配置好LuaJIT环境变量 [root@master ~/nginx-1.12.1]#vim /etc/profile export LUAJIT_LIB=/usr/local/luajit/lib export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1 [root@master ~/nginx-1.12.1]#. /etc/profile 这里将openssl升级到openssl-1.0.2n稳定版 [root@master ~]#wget https://www.openssl.org/source/old/1.0.2/openssl-1.0.2n.tar.gz [root@master ~]#ls openresty-1.13.6.1.tar.gz openssl-1.0.2n.tar.gz [root@master ~]#tar xf openssl-1.0.2n.tar.gz -C /opt http://nginx.org/download/ 下载nginx源码 [root@master ~]#wget http://nginx.org/download/nginx-1.12.1.tar.gz 如果需要,可以修改nginx源码,使得http相应header中Server为自定义名称 [root@master ~/nginx-1.12.1]#sed -i "s#\"nginx/\"#\"abc.com/\"#g" ./src/core/nginx.h [root@master ~/nginx-1.12.1]#./configure \ --prefix=/usr/local/nginx \ --with-cc-opt=-O2 \ --with-stream \ --with-stream_ssl_module \ --with-http_ssl_module \ --with-openssl=/opt/openssl-1.0.2n \ --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib \ --add-module=/opt/openresty-1.13.6.1/bundle/ngx_devel_kit-0.3.0 \ --add-module=/opt/openresty-1.13.6.1/bundle/echo-nginx-module-0.61 \ --add-module=/opt/openresty-1.13.6.1/bundle/xss-nginx-module-0.05 \ --add-module=/opt/openresty-1.13.6.1/bundle/ngx_coolkit-0.2rc3 \ --add-module=/opt/openresty-1.13.6.1/bundle/set-misc-nginx-module-0.31 \ --add-module=/opt/openresty-1.13.6.1/bundle/encrypted-session-nginx-module-0.07 \ --add-module=/opt/openresty-1.13.6.1/bundle/srcache-nginx-module-0.31 \ --add-module=/opt/openresty-1.13.6.1/bundle/ngx_lua-0.10.11 \ --add-module=/opt/openresty-1.13.6.1/bundle/ngx_lua_upstream-0.07 \ --add-module=/opt/openresty-1.13.6.1/bundle/headers-more-nginx-module-0.33 \ --add-module=/opt/openresty-1.13.6.1/bundle/array-var-nginx-module-0.05 \ --add-module=/opt/openresty-1.13.6.1/bundle/memc-nginx-module-0.18 \ --add-module=/opt/openresty-1.13.6.1/bundle/redis2-nginx-module-0.14 \ --add-module=/opt/openresty-1.13.6.1/bundle/redis-nginx-module-0.3.7 \ --add-module=/opt/openresty-1.13.6.1/bundle/rds-json-nginx-module-0.15 \ --add-module=/opt/openresty-1.13.6.1/bundle/rds-csv-nginx-module-0.08 \ --add-module=/opt/openresty-1.13.6.1/bundle/ngx_stream_lua-0.0.3 [root@master ~/nginx-1.12.1]#make -j8 [root@master ~/nginx-1.12.1]#echo $? 0 [root@master ~/nginx-1.12.1]#cd objs/ [root@master ~/nginx-1.12.1/objs]#ll -h total 20M drwxr-xr-x 4 root root 4.0K Jul 26 22:31 addon -rw-r--r-- 1 root root 22K Jul 26 22:31 autoconf.err -rw-r--r-- 1 root root 132K Jul 26 22:31 Makefile -rwxr-xr-x 1 root root 20M Jul 26 22:33 nginx -rw-r--r-- 1 root root 5.3K Jul 26 22:32 nginx.8 -rw-r--r-- 1 root root 8.9K Jul 26 22:31 ngx_auto_config.h -rw-r--r-- 1 root root 657 Jul 26 22:31 ngx_auto_headers.h -rw-r--r-- 1 root root 9.9K Jul 26 22:31 ngx_modules.c -rw-r--r-- 1 root root 100K Jul 26 22:33 ngx_modules.o drwxr-xr-x 9 root root 4.0K Jul 26 22:31 src [root@master ~/nginx-1.12.1/objs]#mv /usr/local/nginx/sbin/nginx{,.20180727} [root@master ~/nginx-1.12.1/objs]#cp nginx /usr/local/nginx/sbin/nginx [root@master ~/nginx-1.12.1/objs]#kill -USR2 `cat /usr/local/nginx/logs/nginx.pid` 执行 ps auxf 查看进程树,会发现两个nginx进程 [root@master ~/nginx-1.12.1/objs]#kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin` 修改nginx配置文件 server { listen 80; server_name _; root /data/web; location / { index index.html index.htm index.php; } location /test { default_type text/html; content_by_lua_block { ngx.say("hello world") } } } 访问web可以看到打印,表示成功 到此升级完成,编译前的文件都可以删除
–
gitlab上经过修改,下面的这个配置可以使用
./configure \ --with-stream \ --with-stream_ssl_module \ --with-http_ssl_module \ --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib \ --add-module=/opt/openresty-1.13.6.1/bundle/ngx_devel_kit-0.3.0 \ --add-module=/opt/openresty-1.13.6.1/bundle/echo-nginx-module-0.61 \ --add-module=/opt/openresty-1.13.6.1/bundle/xss-nginx-module-0.05 \ --add-module=/opt/openresty-1.13.6.1/bundle/ngx_coolkit-0.2rc3 \ --add-module=/opt/openresty-1.13.6.1/bundle/set-misc-nginx-module-0.31 \ --add-module=/opt/openresty-1.13.6.1/bundle/encrypted-session-nginx-module-0.07 \ --add-module=/opt/openresty-1.13.6.1/bundle/srcache-nginx-module-0.31 \ --add-module=/opt/openresty-1.13.6.1/bundle/ngx_lua-0.10.11 \ --add-module=/opt/openresty-1.13.6.1/bundle/ngx_lua_upstream-0.07 \ --add-module=/opt/openresty-1.13.6.1/bundle/headers-more-nginx-module-0.33 \ --add-module=/opt/openresty-1.13.6.1/bundle/array-var-nginx-module-0.05 \ --add-module=/opt/openresty-1.13.6.1/bundle/memc-nginx-module-0.18 \ --add-module=/opt/openresty-1.13.6.1/bundle/redis2-nginx-module-0.14 \ --add-module=/opt/openresty-1.13.6.1/bundle/redis-nginx-module-0.3.7 \ --add-module=/opt/openresty-1.13.6.1/bundle/rds-json-nginx-module-0.15 \ --add-module=/opt/openresty-1.13.6.1/bundle/rds-csv-nginx-module-0.08 \ --add-module=/opt/openresty-1.13.6.1/bundle/ngx_stream_lua-0.0.3 \ --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/sbin/nginx \ --modules-path=/usr/local/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 \ --http-client-body-temp-path=/usr/local/nginx/tmp/client_body \ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy \ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi \ --http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi \ --http-scgi-temp-path=/usr/local/nginx/tmp/scgi \ --pid-path=/usr/local/nginx/run/nginx.pid \ --lock-path=/usr/local/nginx/run/lock/nginx \ --user=www \ --group=www \ --with-file-aio \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_xslt_module \ --with-http_image_filter_module \ --with-http_geoip_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_degradation_module \ --with-http_slice_module \ --with-http_stub_status_module \ --with-mail \ --with-mail_ssl_module \ --with-pcre \ --with-pcre-jit \ --with-debug \ --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' \ 安装cjson 修改下面的Makefile [root@master-10 /opt/openresty-1.13.6.1/bundle/lua-cjson-2.1.0.5]#vim Makefile 16 PREFIX = /usr/local/luajit 21 LUA_INCLUDE_DIR ?= $(PREFIX)/include/luajit-2.1 [root@master-10 /opt/openresty-1.13.6.1/bundle/lua-cjson-2.1.0.5]#make 会在当前目录生产动态文件 cjson.so [root@master-10 /opt/openresty-1.13.6.1/bundle/lua-cjson-2.1.0.5]#mkdir -p /usr/local/lib/lua/5.1 [root@master-10 /opt/openresty-1.13.6.1/bundle/lua-cjson-2.1.0.5]#cp cjson.so /usr/local/lib/lua/5.1
–
参考文档
https://github.com/openresty/lua-nginx-module#nginx-compatibility
–
–
–
评论前必须登录!
注册