首先下載最新版的 相關軟件 的安裝文件。
nginx: http://nginx.org/en/download.html
LuaJIT: http://luajit.org/download.html
lua-nginx-module: https://github.com/openresty/lua-nginx-module/releases/tag/v0.9.16
ngx_devel_kit: https://github.com/simpl/ngx_devel_kit/releases/tag/v0.2.19
[root@localhost src]# ll -ttotal 22408-rw-r--r--. 1 root root 65029 Sep 13 09:00 ngx_devel_kit-0.2.19.tar.gz-rw-r--r--. 1 root root 523900 Sep 13 09:00 lua-nginx-module-0.9.16.tar.gz-rw-r--r--. 1 root root 847615 Sep 13 09:00 LuaJIT-2.0.4.tar.gz-rw-r--r--. 1 root root 832104 Sep 8 13:23 nginx-1.8.0.tar.gz
1. 先解壓安裝 LuaJIT
tar zxvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4 進去
make && make install
搞定。
然后分別解壓 ngx_devel_kit-0.2.19.tar.gz 和 lua-nginx-module-0.9.16.tar.gz
2. 再解壓安裝 nginx ,加入 ngx_devel_kit 和 lua-nginx-module 模塊:
[root@localhost nginx]# ./configure --add-module=/usr/local/src/lua-nginx-module-0.9.16 /> --add-module=/usr/local/src/ngx_devel_kit-0.2.19
[root@localhost nginx]# make && make install
安裝完成,測試一般會報錯:
[root@localhost sbin]# ./nginx./nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
解決方法:
[root@localhost lib]# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf[root@localhost lib]# cat /etc/ld.so.conf.d/usr_local_lib.conf/usr/local/lib[root@localhost lib]# ldconfig
在測試,OK。
[root@localhost sbin]# ./nginx[root@localhost sbin]# ps -elf|grep nginx1 S root 8758 1 0 80 0 - 1612 - 09:26 ? 00:00:00 nginx: master PRocess ./nginx5 S nobody 8759 8758 0 80 0 - 1662 - 09:26 ? 00:00:00 nginx: worker process5 S nobody 8760 8758 0 80 0 - 1662 - 09:26 ? 00:00:00 nginx: worker process0 S root 8762 1723 0 80 0 - 1088 - 09:26 pts/1 00:00:00 grep ngin
配置了 ndk 和 lua-nginx-module 的nginx 要別原來大一些:
[root@localhost sbin]# ls -lhtotal 7.1M-rwxr-xr-x. 1 root root 4.4M Sep 13 09:20 nginx-rwxr-xr-x. 1 root root 2.8M Sep 9 10:50 nginx.old
3. 測試Lua 模塊配置是否成功:
在 nginx.conf 文件中加入:
location /lua { set $test "hello, world."; content_by_lua ' ngx.header.content_type = "text/plain"; ngx.say(ngx.var.test); '; }
然后 nginx -t; 測試無錯誤,然后 nginx -s reload; 重新加載nginx.conf文件
然后訪問 192.168.1.200/lua:
看到結果,表示 nginx, Lua 開發環境搭建成功。
上面配置的意思,就是定義一個變量 $test, 然后以 text/pain 格式輸出它的值。$test定義變量,讓人想起 php 和 shell 變量的定義。
其實,nginx 和 lua 有專門的已經配置好了的開發環境: OpenResty 和 Tengine
新聞熱點
疑難解答