nginx的强大,lua的高性能,真是一个不错的组合,合到一起就无敌了,呵呵。
下面开始配置nginx,使其支持lua,是通过一个nginx模块实现的,
模块地址:https://github.com/chaoslawful/lua-nginx-module
下载链接:https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.10.tar.gz
1、下载源码、解压缩
a、nginx源码:
1
2
|
[root@localhost ~]
# wget http://nginx.org/download/nginx-1.4.2.tar.gz
[root@localhost ~]
# tar zxvf nginx-1.4.2.tar.gz
|
b、lua模块
1
2
|
[root@localhost ~]# wget -O lua-nginx-module-
0.8
.
10
.tar.gz https:
//github.com/chaoslawful/lua-nginx-module/archive/v0.8.10.tar.gz
[root@localhost ~]# tar zxvf lua-nginx-module-
0.8
.
10
.tar.gz
|
c、luajit(lua即时编译器)
1
2
|
[root@localhost ~]# wget http:
//luajit.org/download/LuaJIT-2.0.2.tar.gz
[root@localhost ~]# tar zxvf LuaJIT-
2.0
.
2
.tar.gz
|
d、ngx_devel_kit(nginx开发工具包)
1
2
|
[root@localhost ~]
# wget -O ngx_devel_kit-0.2.18.tar.gz https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz
[root@localhost ~]
# tar zxvf ngx_devel_kit-0.2.18.tar.gz
|
e、echo-nginx(用户开发调试输出)
[root@localhost ~]
# wget -O echo-nginx.zip https://github.com/openresty/echo-nginx-module/archive/v0.55.zip
[root@localhost ~]
# unzip echo-nginx.zip
2、安装luajit
1
2
|
[root@localhost ~]
# make
[root@localhost ~]
# make install
|
3、安装nginx
方法a、使用luajit即时编译器
1
2
3
4
5
6
7
8
9
10
11
|
[root@localhost nginx-1.4.1]
# export LUAJIT_LIB=/usr/local/lib
[root@localhost nginx-1.4.1]
# export LUAJIT_INC=/usr/local/include/luajit-2.0
[root@localhost nginx-1.4.1]
# ./configure \
--prefix=
/usr/local/nginx-1
.4.1 \
--with-http_stub_status_module \
--add-module=..
/lua-nginx-module-0
.8.9 \
--add-module=..
/ngx_devel_kit-0
.2.18
[root@localhost nginx-1.4.1]
# make -j 4
[root@localhost nginx-1.4.1]
# make install
[root@localhost ~]
# echo '/usr/local/lib' >> /etc/ld.so.conf.d/lua.conf
[root@localhost ~]
# ldconfig
|
方法b、使用lua编译器
注意:使用lua编译器的话,需要先安装lua执行环境,一般的系统都是预装好的,如果没有预装 好的话,可以用yum安装,如下:
1
|
[root@localhost ~]
# yum install lua lua-devel
|
安装nginx:
1
2
3
4
5
6
7
8
9
|
[root@localhost nginx-1.4.1]
# export LUA_LIB=/usr/lib64
[root@localhost nginx-1.4.1]
# export LUA_INC=/usr/include
[root@localhost nginx-1.4.1]
# ./configure \
--prefix=
/usr/local/nginx-1
.4.1 \
--with-http_stub_status_module \
--add-module=..
/lua-nginx-module-0
.8.9 \
--add-module=..
/ngx_devel_kit-0
.2.18
--add-module=../echo-nginx
[root@localhost nginx-1.4.1]
# make -j 4
[root@localhost nginx-1.4.1]
# make install
|
注意:让nginx支持lua,有两种方法,一是使用luajit即时编译器,二是使用lua编译器。推荐使用luajit,因为效率高。其中ngx_devel_kit的作用有2个,一是开发用的,二是可以在错误日志中记录nginx处理阶段信息(rewrite phase,access phase,content phase),需要将错误日志级别调高,调试时可以设置成debug。
4、验证安装
使用lua编译器时显示如下:
1
2
|
[root@localhost ~]# lsof -p
3359
| grep -i lua
nginx
3359
root mem REG
8
,
3
183920
394551
/usr/lib64/liblua-
5.1
.so
|
使用luajit即时编译器时显示如下:
1
2
|
[root@localhost ~]
# lsof -p 13177 | grep -i lua
nginx 13177 root mem REG 8,3 452024 405089
/usr/local/lib/libluajit-5
.1.so.2.0.2
|
5、验证配置指令和输出
修改nginx.conf配置文件,加入下面指令:
1
2
3
|
location / {
content_by_lua 'ngx.say("hello world!")';
}
|
重启nginx,用curl测试
1
2
3
4
5
6
7
8
|
[root@localhost ~]
# curl -i localhost
HTTP
/1
.1 200 OK
Server: nginx
/1
.4.1
Date: Tue, 24 Sep 2013 23:23:58 GMT
Content-Type: application
/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
hello world!
|
转自:http://haoyun.blog.51cto.com/2038762/1301257
参考:http://blog.chinaunix.net/uid-26212859-id-3256667.html