摘要:

本文记录如何安装ngx_lua模块

nginx_lua_module是由淘宝的工程师清无(王晓哲)和春来(章亦春)所开发的nginx第三方模块,它能将lua语言嵌入到nginx配置中,从而使用lua就极大增强了nginx的能力

http://wiki.nginx.org/HttpLuaModule


正文:

1 下载luajit 2.0并安装

http://luajit.org/download.html

我是直接使用源码make && make install

所以lib和include是直接放在/usr/local/lib和usr/local/include


2 下载nginx源码,解压

注意版本号,如果机子上已经装了nginx,不想升级的话,请使用/to/nginx/sbin/nginx –v

来查看版本号


3  下载ngx_devel_kit HERE 解压

4  下载nginx_lua_module HERE 解压


5 进入nginx源码文件夹

cd nginx-1.0.11/


6 导入环境变量,编译

export LUAJIT_LIB=/usr/local/lib    #这个很有可能不一样    
export LUAJIT_INC=/usr/local/include/luajit-2.0  #这个很有可能不一样


./configure --prefix=/opt/nginx \    #nginx的安装路径    
--add-module=/path/to/ngx_devel_kit \   #ngx_devel_kit 的源码路径    
--add-module=/path/to/lua-nginx-module  #nginx_lua_module 的源码路径


make -j2    
make install


7 测试是否成功:

nginxconfig中加入


location /hello {    
     default_type 'text/plain';    
     content_by_lua 'ngx.say("hello, lua")';    
}


使用/to/nginx/sbin/nginx –t 检查nginx配置,此时应该没有报错


8 /to/nginx/sbin/nginx   #启动nginx

或者/to/nginx/sbin/nginx –s reload #重启nginx



访问192.168.100.1/hello

会出现“hello,lua”


安装成功!




补充:

1.如遇到找不到库文件
echo “/usr/local/lib” > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
即可
2./usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory  
在 Nginx 编译时,需要指定 RPATH,记得加入下面选项:
./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"  
或者export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH



相关链接:
http://www.cnblogs.com/yjf512/archive/2012/03/27/2419577.html

http://www.th7.cn/system/lin/201311/47418.shtml