nginx lua环境搭建

1.8k 词

安装nginx及搭建本地测试环境

下载最新版的nginx

$ cd /usr/local/src 
$ wget http://nginx.org/download/nginx-1.11.3.tar.gz
$ tar -zxvf nginx-1.11.3.tar.gz

安装Luajit(最新版)

$ cd /usr/local/src
$ wget http://luajit.org/download/LuaJIT-2.1.0-beta2.tar.gz
$ tar -zxvf LuaJIT-2.1.0-beta2.tar.gz
$ cd ./LuaJIT-2.1.0-beta2
$ make && make install

在/usr/local/src目录下创建nginx-module目录,在nginx-module目录下下载第三方模块:
lua-nginx-module:

$ git clone https://github.com/chaoslawful/lua-nginx-module.git
$ git clone https://github.com/agentzh/echo-nginx-module.git 
$ git clone https://github.com/simpl/ngx_devel_kit.git

为了避免冲突我这里是重新创建一个安装目录

$ mkdir /usr/local/nginx2   

修改nginx配置:
进入nginx-1.11.3目录

$ ./configure --prefix=/usr/local/nginx 
    --add-module=../nginx-module/lua-nginx-module 
    --add-module=../nginx-module/echo-nginx-module 
    --add-module=../nginx-module/ngx_devel_kit  
    --with-http_ssl_module 
    --with-http_v2_module 
    --with-pcre=/usr/local/src/pcre 
    --with-zlib=/usr/local/src/zlib 
    --with-openssl=/usr/local/src/openssl 

$ make && make install

这里就不详细的介绍各个模块的下载和安装,不清楚的可以到之前的文章查看详细安装配置编译Nginx

导入lua

luajit默认是安装在/usr/local/lib目录下,但是nginx是从/usr/lib下找luajit,因此两种解决办法,一种是安装完成后手动cp luajit库移一下,另一种是安装nginx config前先导入环境变量,告诉nginx去哪里找luajit

$ export LUAJIT_LIB=/usr/local/lib
$ export LUAJIT_INC=/usr/local/include/luajit-2.0

如果运行nginx报error while loading shared libraries: libluajit-5.1.so.2: cannot open shared 错误,说明上面的修改还是没有生效。

尝试一下下面的方法:

如果共享库文件安装到了/usr/local/lib(很多开源的共享库都会安装到该目录下)或其它”非/lib或/usr/lib”目录下, 那么在执行ldconfig命令前, 还要把新共享库目录加入到共享库配置文件/etc/ld.so.conf中, 操作如下:

$ cat /etc/ld.so.conf

$ echo "/usr/local/lib" >> /etc/ld.so.conf

$ ldconfig

测试环境

$ vim /usr/example/lua/test.lua

添加如下内容:

$ ngx.say("hello world");

然后修改nginx安装目录下的nginx.conf

location /test {  
     default_type 'text/html';  
     content_by_lua_file /usr/example/lua/test.lua;  
 }

在浏览器中输入:yourServerIp/test

如果出现:出现:hello world

恭喜!nginx+lua开发环境就安装成功了!