ng_lua环境搭建

前言:

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

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


所需要的模块如下:

LuaJIT-2.0.2.tar.gz
lua-nginx-module(v0.8.6.tar.gz)
pcre-8.33.tar.gz(nginx需要)
echo-nginx-module(v0.57.tar.gz)

ngx_devel_kit(v0.2.18.tar.gz)


JIT
通常,程序有两种运行方式:静态编译与动态直译。
静态编译的程序在执行前全部被翻译为机器码,而动态直译执行的则是一句一句边运行边翻译。
即时编译(Just-In-Time Compiler)则混合了这二者,一句一句编译源代码,但是会将翻译过的代码缓存起来以降低性能损耗。
JAVA、.NET 实现都使用即时编译以提供高速的代码执行。

注意:
在nginx.conf中添加”lua_code_cache on/off”,来开启是否将代码缓存,所以每次变更”*.lua”文件时,必须reload nginx才可生效。仅针对”set_by_lua_file, content_by_lua_file, rewrite_by_lua_file, and access_by_lua_file”有效, 因为其他为写在配置文件中,更改代码也必须reload nginx。在生产环境下,不能禁用cache。同时在lua代码中使用”dofile” 或 “loadfie” 来加载外部lua脚本将不会对它进行缓存,应该使用”require”来代替。禁用cache,当且仅当在调式代码下。

LuaJIT
是一个利用JIT编译技术把Lua脚本直接编译成机器码由CPU运行
版本:2.0 与 2.1
当前稳定版本为 2.0。
2.1为版本与ngx_lua将有较大性能提升,主要是CloudFlare公司对luajit的捐赠。
FFI库,是LuaJIT中最重要的一个扩展库。
1. 它允许从纯Lua代码调用外部C函数,使用C数据结构;
2. 就不用再像Lua标准math库一样,编写Lua扩展库;
3. 把开发者从开发Lua扩展C库(语言/功能绑定库)的繁重工作中释放出来;



安装步骤:

1. 安装luaJIT

wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
tar -xzvf LuaJIT-2.0.2.tar.gz
cd LuaJIT-2.0.2
make && make install

 下面需要配置一下 luajit 或 lua 的环境变量(Nginx编译时需要),如果用luajit则执行下面两行,用lua则执行下面两行

 (当然也可以修改/etc/profile,加入下面两行。再source /etc/profile使之立即生效)

 -- luajit -- 

 # tell nginx's build system where to find LuaJIT:  
 export LUAJIT_LIB=/usr/local/lib
 export LUAJIT_INC=/usr/local/include/luajit-2.0

 -- lua --  
# or tell where to find Lua if using Lua instead:  
export LUA_LIB=/path/to/lua/lib  
export LUA_INC=/path/to/lua/include 

2. 解压lua模块

说明由于大量的 Lua 代码写在 Nginx 中,会使配置文件显得很繁琐,所以这里使用 content_by_lua_file 来引入 Lua 脚本文件
要使用 content_by_lua_file,需要安装 nginx_lua_module 模块
cd /data0/source_code
wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz
tar -zxvf v0.8.6.tar.gz

3.安装nginx

 安装ngin所需要的pcre库(用来做正则匹配的?):
<span style="font-family: Arial, Helvetica, sans-serif;">wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz</span>
tar -zxvf pcre-8.33.tar.gz
cd pcre-8.33
./configure
make && make install


下载echo-nginx-module,解压

wget https://github.com/openresty/echo-nginx-module/archive/v0.57.tar.gz
tar -zxvf v0.57.tar.gz

 下载ngx_devel_kit,解压
wget -c https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz
tar -zxvf v0.2.18.tar.gz

 安装nginx:
wget http://nginx.org/download/nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure  --user=www --group=www --prefix=/data0/nginx-1.4.2 \
--add-module=/data0/source_code/ngx_devel_kit-0.2.18 \
--add-module=/data0/source_code/echo-nginx-module-0.57 \
--add-module=/data0/source_code/lua-nginx-module-0.8.6 \
--with-http_stub_status_module \
--with-pcre=/data0/source_code/pcre-8.33/ 

编译后成功后,会看到下面的描述:
  Configuration summary
  + using PCRE library: /data0/source_code/pcre-8.33/
  + OpenSSL library is not used
  + using builtin md5 code
  + sha1 library is not found

  + using system zlib library


  nginx path prefix: "/data0/nginx-1.4.2"
  nginx binary file: "/data0/nginx-1.4.2/sbin/nginx"
  nginx configuration prefix: "/data0/nginx-1.4.2/conf"
  nginx configuration file: "/data0/nginx-1.4.2/conf/nginx.conf"
  nginx pid file: "/data0/nginx-1.4.2/logs/nginx.pid"
  nginx error log file: "/data0/nginx-1.4.2/logs/error.log"
  nginx http access log file: "/data0/nginx-1.4.2/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

  安装:
make -j2
make install 
ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

启动nginx:

 /data0/nginx/sbin/nginx -c /data0/nginx/conf/nginx.conf

启动nginx时遇到的问题:报错nginx: [emerg] getpwnam(“www”) failed 错误:

nginx: [emerg] getpwnam(“www”) failed
解决方案一
在nginx.conf中 把user nobody的注释去掉既可
解决方案二
错误的原因是没有创建www这个用户,应该在服务器系统中添加www用户组和用户www,如下命令:
/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www

测试:

1、直接嵌入脚本,修改nginx.conf,加入:
        location /hello { 
            default_type 'text/plain'; 
            content_by_lua 'ngx.say("hello, lua")'; 
        }
执行:

curl 'http://localhost/hello',

出现:hello,lua 证明OK了


2、调用脚本文件

(1)在Nginx根目录下,新建lua文件夹,并创建test_lua.lua脚本,脚本内容如下:

ngx.say("hello, lua")


(2)修改nginx.conf配置文件
在nginx的 http block里面,加入下面一行代码,以便引入lua脚本:

lua_package_path "/data0/nginx-1.4.2/lua/?.lua;;";  (注意引号里面有两个分号)


为了使得 lua 脚本的修改能及时生效,需要在nginx配置文件的server block里面加入一行代码:

lua_code_cache off;  #如果没有加入这一行,或者设置为on的话,则需要重启nginx

在配置文件里面加入下面的代码:
        location /hello {
            content_by_lua_file lua/test_lua.lua;
        }

    

注意修改完配置文件需要重新加载一下才回生效:/data0/nginx-1.4.2/sbin/nginx -s reload


curl 'http://localhost/hello',出现:hello,lua 证明OK了

其他

1、如何给一个已经装好的nginx再装其他模块?

例如如果之前我没有装echo-nginx-module这个模块,现在我想装上怎么办呢?下载下来这个模块的tar.gz包,解压,到nginx安装包所在目录,重新解压nginx安装包(原来原来已经有解压的文件夹,删除),然后切到解压后的nginx目录,注意这里需要将之前已经编译的模块重新加入再编译,再执行make,最后将编译后生成的新的nginx文件覆盖原来的nginx文件即可。具体说明:

nginx文件非常小但是性能非常的高效,这方面完胜apache,nginx文件小的一个原因之一是nginx自带的功能相对较少,好在nginx允许第三方模块,第三方模块使得nginx越发的强大. 在安装模块方面,nginx显得没有apache安装模块方便,当然也没有php安装扩展方便.在原生的nginx,他不可以动态加载模块,所以当你安装第三方模块的时候需要覆盖nginx文件.接下来看看如何安装nginx第三模块吧.

nginx第三方模块安装方法:

./configure --prefix=/你的安装目录  --add-module=/第三方模块目录

以安装pagespeed模块实例

在未安装nginx的情况下安装nginx第三方模块

 ./configure --prefix=/usr/local/nginx-1.4.1 \
--with-http_stub_status_module \
--with-http_ssl_module --with-http_realip_module \
--with-http_image_filter_module \
--add-module=../ngx_pagespeed-master --add-module=/第三方模块目录

make

make install

在已安装nginx情况下安装nginx模块
./configure --prefix=/usr/local/nginx-1.4.1 \
 --with-http_stub_status_module \
 --with-http_ssl_module --with-http_realip_module \
 --with-http_image_filter_module \
 --add-module=../ngx_pagespeed-master
 make && make install
 /usr/local/nginx-1.4.1/sbin/nginx -s stop
 cp objs/nginx /usr/local/nginx/sbin/nginx

 
相比之下仅仅多了一步覆盖nginx文件. 

总结,安装nginx安装第三方模块实际上是使用–add-module重新安装一次nginx,不要make install而是直接把编译目录下objs/nginx文件直接覆盖老的nginx文件.如果你需要安装多个nginx第三方模块,你只需要多指定几个相应的–add-module即可.

备注:重新编译的时候,记得一定要把以前编译过的模块一同加到configure参数里面.(如何查看原来装了哪些模块: nginx -V)

nginx提供了非常多的nginx第三方模块提供安装,地址http://wiki.nginx.org/3rdPartyModules


2、在编译安装 Nginx 的第三方模块时,碰到一个错误:
/data0/nginx-1.4.2/sbin/ngxin -s reload  
/data0/nginx-1.4.2/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  


更多关于ngx_lua模块的介绍:

http://wiki.nginx.org/HttpLuaModule

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值