1.安裝lua
1.1安裝LuaJIT
[root@localhost ~]# yum install readline-devel
[root@localhost ~]# mkdir -p lua-file
[root@localhost ~]# cd lua-file/
[root@localhost lua_file]# wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
[root@localhost lua_file]# tar -zxvf LuaJIT-2.0.5.tar.gz
[root@localhost lua_file]# ls
LuaJIT-2.0.5 LuaJIT-2.0.5.tar.gz
[root@localhost lua_file]# cd LuaJIT-2.0.5
[root@localhost LuaJIT-2.0.5]# make && make install PREFIX=/usr/local/luajit
1.2配置LuaJIT環境變數
[root@localhost lua-file]# vim /etc/profile
#/etc/profile 檔案中加入環境變數
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
[root@localhost lua-file]# source /etc/profile
1.3 lua安裝測驗
[root@localhost lua-file]# vim hello.lua
print('hello world lua');
[root@localhost lua-file]# lua hello.lua
hello world lua
2.ngx_devel_kit和lua-nginx-module
ngx_devel_kit簡稱NDK,提供函式和宏處理一些基本任務,減輕第三方模塊開發的代碼量,
lua-nginx-module是nginx的lua模塊
[root@localhost ~]# mkdir -p /usr/local/src/nginx
[root@localhost ~]# cd /usr/local/src/nginx/
[root@localhost nginx]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
[root@localhost nginx]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
[root@localhost nginx]# ls
v0.10.9rc7.tar.gz v0.3.0.tar.gz
[root@localhost nginx]# tar -zxvf v0.10.9rc7.tar.gz
[root@localhost nginx]# tar -zxvf v0.3.0.tar.gz
3.查看已安裝好的nginx
3.1查看原安裝
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module

3.2 安裝依賴
[root@localhost nginx]# yum -y install openssl openssl-devel zlib zlib-devel pcre-devel
3.3 進入nginx解壓目錄,安裝擴展
[root@localhost ~]# cd nginx-1.14.2
[root@localhost nginx-1.14.2]#
3.3.1.清空之前的編譯檔案
[root@localhost nginx-1.14.2]# make clean
3.3.2 執行 nginx -V 查看之前的編譯引數
[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module
3.3.3 添加擴展執行編譯
[root@localhost nginx-1.14.2]# ls /usr/local/src/nginx/
lua-nginx-module-0.10.9rc7 ngx_devel_kit-0.3.0 v0.10.9rc7.tar.gz v0.3.0.tar.gz
[root@localhost nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module --add-module=/usr/local/src/nginx/lua-nginx-module-0.10.9rc7/ --add-module=/usr/local/src/nginx/ngx_devel_kit-0.3.0/
3.3.4.執行make (不能執行 make install)
[root@localhost nginx-1.14.2]# make
3.3.5.(打開新連接視窗操作)重命名 nginx 舊版本二進制檔案,即 sbin 目錄下的 nginx(期間 nginx 并不會停止服務)
客戶端2
[root@localhost nginx-1.14.2]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# mv nginx nginx.old
[root@localhost sbin]# ls
nginx.old
3.3.6.拷貝一份新編譯的二進制檔案到安裝目錄
[root@localhost nginx-1.14.2]# pwd
/root/nginx-1.14.2
[root@localhost nginx-1.14.2]# cd objs/
[root@localhost objs]# ll
total 9124
drwxr-xr-x. 3 root root 17 Nov 16 22:55 addon
-rw-r--r--. 1 root root 18837 Nov 16 22:55 autoconf.err
-rw-r--r--. 1 root root 68709 Nov 16 22:55 Makefile
-rwxr-xr-x. 1 root root 9132336 Nov 16 22:57 nginx
-rw-r--r--. 1 root root 5341 Nov 16 22:57 nginx.8
-rw-r--r--. 1 root root 7493 Nov 16 22:55 ngx_auto_config.h
-rw-r--r--. 1 root root 657 Nov 16 22:55 ngx_auto_headers.h
-rw-r--r--. 1 root root 6209 Nov 16 22:55 ngx_modules.c
-rw-r--r--. 1 root root 86808 Nov 16 22:57 ngx_modules.o
drwxr-xr-x. 9 root root 91 Nov 16 22:55 src
[root@localhost objs]# cp nginx /usr/local/nginx/sbin/
[root@localhost objs]# ls /usr/local/nginx/sbin/
nginx nginx.old


3.3.7.在原始碼目錄執行 make upgrade 開始升級
[root@localhost nginx-1.14.2]# make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
###查看是否安裝成功
[root@localhost nginx-1.14.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-pcre --with-http_ssl_module --with-http_gzip_static_module --add-module=/usr/local/src/nginx/lua-nginx-module-0.10.9rc7/ --add-module=/usr/local/src/nginx/ngx_devel_kit-0.3.0/



啟動nginx,如果有這個報錯:nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
#解決方案
[root@localhost nginx-1.14.2]# vim /etc/ld.so.conf
#新增一條
/usr/local/luajit/lib
#保存檔案,執行ldconfig
[root@localhost nginx-1.14.2]# ldconfig
4 nginx引入lua除錯
4.1 nginx.conf引入lua
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
#關閉代碼快取 ,修改lua腳本不需要重啟
lua_code_cache off;
#測驗lua
server {
listen 81;
server_name localhost;
#設定變數
set $test "hello world lua";
location /lua_test {
content_by_lua '
ngx.header.content_type="text/plain";
ngx.say(ngx.var.test);
';
}
}

4.2 重啟nginx,執行訪問
[root@localhost nginx]# killall nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx/conf/nginx.conf:22

瀏覽器執行訪問:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/223775.html
標籤:其他
