nginx 与 lua 开发环境搭建,nginxlua


首先下载最新版的 相关软件 的安装文件。

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 -t
total 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 nginx
1 S root      8758     1  0  80   0 -  1612 -      09:26 ?        00:00:00 nginx: master process ./nginx
5 S nobody    8759  8758  0  80   0 -  1662 -      09:26 ?        00:00:00 nginx: worker process
5 S nobody    8760  8758  0  80   0 -  1662 -      09:26 ?        00:00:00 nginx: worker process
0 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 -lh
total 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

 

相关内容