nginx 如何安装 nginx_lua_module 模块 以及 echo-nginx-module 模块

来源:互联网 发布:易名域名 编辑:程序博客网 时间:2024/05/18 03:23

ngx_lua_module 是一个nginx http模块,它把 lua 解析器内嵌到 nginx,用来解析并执行lua 语言编写的网页后台脚本。

特性

  • 支持Windows和Linux平台。
  • 支持高并发高性能。
  • HTML网页中内嵌LUA脚本代码,类似于PHP。
  • 支持非阻塞的数据库操作,目前只支持MYSQL。
  • 支持异步的文件IO操作。
  • 支持非阻塞的SOCKET IO操作。

下面简要介绍下 ngx_lua_module 模块的安装。


方式一:使用大牛 ZhangYichun 提供的集成包快速安装。

非常简单,下载 ngx_openresty,该集成包中有:NginxLuaLuajitngx_lua,以及一些有用的Nginx第三方模块。

安装步骤:

[plain] view plaincopyprint?
  1. ./configure --with-luajit   
  2. make   
  3. make install  

安装完成!

方式二:Ngx_lua可以手动编译进Nginx

首先,我的 Nginx 安装路径为:/usr/local/nginx

这里,我就不赘述Nginx 的安装步骤了,详细请见文章《Centos系统下 Nginx 服务器安装》


我将尝试编译的两个模块:
echolua

所需要的模块如下:

[plain] view plaincopyprint?
  1. liujit             http://luajit.org  
  2. lua                http://www.lua.org  
  3. ngx_devel_kit      https://github.com/simpl/ngx_devel_kit  
  4. echo-nginx-module  https://github.com/agentzh/echo-nginx-module  
  5. lua-nginx-module   https://github.com/chaoslawful/lua-nginx-module  

安装步骤:

1、Luajit2.0(推荐)或者 Lua5.1(Lua5.2暂不支持)

[plain] view plaincopyprint?
  1. wget http://luajit.org/download/LuaJIT-2.0.0-beta9.tar.gz  
  2. tar zxvf LuaJIT-2.0.0-beta9.tar.gz  
  3. cd LuaJIT-2.0.0-beta9  
  4. make   
  5. sudo make install PREFIX=/usr/local/luajit  

Note: to avoid overwriting a previous version,
the beta test releases only install the LuaJIT executable under the versioned name (i.e. luajit-2.0.0-beta10). 
You probably want to create a symlink for convenience, with a command like this:

sudo ln -sf luajit-2.0.0-beta9 /usr/local/bin/luajit (加上这句命令)


下面需要配置一下 luajit 或 lua 的环境变量(Nginx编译时需要):

[plain] view plaincopyprint?
  1. -- luajit --  
  2. # tell nginx's build system where to find LuaJIT:  
  3. export LUAJIT_LIB=/path/to/luajit/lib  
  4. export LUAJIT_INC=/path/to/luajit/include/luajit-2.0  
  5.   
  6. -- lua --  
  7. # or tell where to find Lua if using Lua instead:  
  8. export LUA_LIB=/path/to/lua/lib  
  9. export LUA_INC=/path/to/lua/include  


我的测试环境里,配置如下:

[plain] view plaincopyprint?
  1. export LUAJIT_LIB=/usr/local/luajit/lib  
  2. export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0  

2、安装 ngx_devel_kit (NDK) 模块

[plain] view plaincopyprint?
  1. cd /usr/local  
  2. git clone https://github.com/simpl/ngx_devel_kit.git  

下载完成后,将在 /usr/local/ 目录下生成子目录 ngx_devel_kit。


3、安装 lua-nginx-module 模块

[plain] view plaincopyprint?
  1. cd /usr/local  
  2. git clone https://github.com/chaoslawful/lua-nginx-module.git  
下载完成后,将在 /usr/local/ 目录下生成子目录 lua-nginx-module。


4、重新编译Nginx,需要注意编译顺序!

[plain] view plaincopyprint?
  1. ./configure --prefix=/usr/local/nginx \  
  2.             --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB" \  
  3.             --add-module=/usr/local/ngx_devel_kit \  
  4.             --add-module=/usr/local/echo-nginx-module \  
  5.             --add-module=/usr/local/lua-nginx-module  
  6. make -j2  
  7. make install  

注释:重新编译 Nginx 二进制,Nginx 需要 quit 再启动。而普通配置更新则 reload 即可:
[plain] view plaincopyprint?
  1. kill -HUP `cat /path/nginx/logs/nginx.pid`  
  2. /usr/local/nginx/sbin/nginx -s reload  

模块编译成功!

重启Nginx服务器!

我在编译安装 Nginx 的第三方模块时,碰到一个错误:

[plain] view plaincopyprint?
  1. /usr/local/nginx/sbin/ngxin -s reload  
  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  

百事不得其解,后来Google之,发现了解决办法。

在 Nginx 编译时,需要指定 RPATH,加入下面选项即可:

[plain] view plaincopyprint?
  1. ./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"  
  2.   
  3. 或者  
  4.   
  5. export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH  


5、测试Lua

Nginx.conf 配置文件中,加入以下代码:

[plain] view plaincopyprint?
  1. location /echo {  
  2.     default_type 'text/plain';  
  3.     echo 'hello echo';  
  4. }  
  5.   
  6. location /lua {  
  7.     default_type 'text/plain';  
  8.     content_by_lua 'ngx.say("hello, lua")';  
  9. }  

重启Nginx服务器:

[plain] view plaincopyprint?
  1. /usr/local/nginx/sbin/nginx -s reload  

使用curl测试:

[plain] view plaincopyprint?
  1. [root@localhost] curl http://localhost/echo  
  2. hello echo  
  3. [root@localhost] curl http://localhost/lua  
  4. hello lua  

测试结果表明,两个模块都安装成功!

关于ngx_lua模块的详细文档见下:
http://wiki.nginx.org/HttpLuaModule
0 0
原创粉丝点击