nginx + lua 环境搭建+创建项目;openresty搭建+创建项目
2016-04-27 19:33
561 查看
某位大虾说过,天下武功,唯快不破!Nginx的看家本领就是速度,Lua的拿手好戏亦是速度,这两者的结合在速度上无疑有基因上的优势。话不多说,进入话题。
准备需要的压缩包并解压好:
nginx-1.9.7【nginx源码】
lua-nginx-module-master【配置nginx,让其支持lua】
LuaJIT-2.0.2【lua即时编译器】
ngx_devel_kit-0.2.18【nginx开发工具包】
echo-nginx-module-0.58 【输出一些简单的信息】
rds-json-nginx-module-0.13 【处理Json数据】
set-misc-nginx-module-0.28 【标准的HttpRewriteModule指令的扩展,很多功能】
redis2-nginx-module 【lua操作redis】
等等
安装luajit(这步很重要,一定要确保没出错)
cd LuaJIT-2.0.2
make
make install
安装nginx
注意:让nginx支持lua,有两种方法,一是使用luajit即时编译器,二是使用lua编译器。推荐使用luajit,因为效率高。
cd nginx-1.9.7
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
./configure \
–prefix=/usr/local/nginx \
–with-http_stub_status_module \
–add-module=../lua-nginx-module-master \
–add-module=../ngx_devel_kit-0.2.18 \
–add-module=../echo-nginx-module-0.58 \
–add-module=../rds-json-nginx-module-0.13 \
–add-module=../set-misc-nginx-module-0.28 \
–add-module=../redis2-nginx-module
make
make install
echo ‘/usr/local/lib’ >> /etc/ld.so.conf.d/lua.conf
ldconfig
如果没有问题,就安装好了
测试
进入nginx.conf,修改它,在location / {。。。}下面新建一个
location /lua {
default_type ‘text/html’;
content_by_lua ‘ngx.say(“Hello Lua!”)’;
}
保存退出,如果想知道配置文件有没有错可以使用如下命令
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
好了,配置没有问题,启动nginx
/usr/local/nginx/sbin/nginx
可以在浏览器输入:http://ip地址/lua
Hello Lua!—->说明环境配置成功
也可以直接通过命令测试:
[root@localhost /]# curl http://127.0.0.1/lua
[root@localhost /]# Hello Lua!
大功告成
创建项目
由于大量的 Lua 代码写在 Nginx 中,会使配置文件显得很繁琐,所以这里使用 content_by_lua_file 来引入 Lua 脚本文件。
这个的目的是为了好管理你的项目,极力推荐
参考:
总结:以这种方式搭建的nginx + lua环境,在过程中很容易出错,因为受各个版本的限制,而且配置非常繁琐,如果后期遇到添加新的模块涉及重新编译nginx等等,所以接下来推荐另外一个搭建nginx + lua环境,你会发现春天来了,而且很简单,丝毫不影响它的快—–那就是openresty
Openresty 是一套基于NGINX核心的相对完整的Web应用开发框架,包含了ngx_lua在内的众多第三方优秀的NGINX C模块,同时也集成了一系列常用的lua-resty-*类库,例如redis, mysql等,特别地,Openresty依赖的NGINX核心和LuaJIT版本都是经过非常充分的测试的,也打了不少必要的补丁。
下载openresty压缩包 :
http://openresty.org/cn/download.html
cd openresty-1.9.7.4
make
make install (没错就这么快,就这么简单)
返回上一级目录会多出一个目录:openresty,你所需要的都会在这里,包括对mysql,redis等等都自动加上了,而且不需要什么配置。。。接下来的步骤和前面一样的
初学者lua注意事项
在平时测试或者开发中,建议开启禁止缓存lua
比如:
location /vtoken{
default_type ‘text/html’;
lua_code_cache off; 默认是启动的,生产环境可关闭
content_by_lua_file/usr/local/openresty/nginx/html/Control_center/lua/verifyToken.lua;
}
建议
也是由于工作需要,接触了这个轻量级的脚本,目前写过简单的脚本,比如操作redis,mysql等等。过程中了解的最多是它和C密切相关,它两互调函数等。这么折腾一下也渐渐感兴趣来了,虽然现在还体会不到它的优势,希望后期会有好的收获;学习的话先从环境开始搭建,看看lua的语法,根据自己的需求试着去写一些附带逻辑性的脚本,慢慢的来。。。
准备需要的压缩包并解压好:
nginx-1.9.7【nginx源码】
lua-nginx-module-master【配置nginx,让其支持lua】
LuaJIT-2.0.2【lua即时编译器】
ngx_devel_kit-0.2.18【nginx开发工具包】
echo-nginx-module-0.58 【输出一些简单的信息】
rds-json-nginx-module-0.13 【处理Json数据】
set-misc-nginx-module-0.28 【标准的HttpRewriteModule指令的扩展,很多功能】
redis2-nginx-module 【lua操作redis】
等等
安装luajit(这步很重要,一定要确保没出错)
cd LuaJIT-2.0.2
make
make install
安装nginx
注意:让nginx支持lua,有两种方法,一是使用luajit即时编译器,二是使用lua编译器。推荐使用luajit,因为效率高。
cd nginx-1.9.7
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
./configure \
–prefix=/usr/local/nginx \
–with-http_stub_status_module \
–add-module=../lua-nginx-module-master \
–add-module=../ngx_devel_kit-0.2.18 \
–add-module=../echo-nginx-module-0.58 \
–add-module=../rds-json-nginx-module-0.13 \
–add-module=../set-misc-nginx-module-0.28 \
–add-module=../redis2-nginx-module
make
make install
echo ‘/usr/local/lib’ >> /etc/ld.so.conf.d/lua.conf
ldconfig
如果没有问题,就安装好了
测试
进入nginx.conf,修改它,在location / {。。。}下面新建一个
location /lua {
default_type ‘text/html’;
content_by_lua ‘ngx.say(“Hello Lua!”)’;
}
保存退出,如果想知道配置文件有没有错可以使用如下命令
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
好了,配置没有问题,启动nginx
/usr/local/nginx/sbin/nginx
可以在浏览器输入:http://ip地址/lua
Hello Lua!—->说明环境配置成功
也可以直接通过命令测试:
[root@localhost /]# curl http://127.0.0.1/lua
[root@localhost /]# Hello Lua!
大功告成
创建项目
由于大量的 Lua 代码写在 Nginx 中,会使配置文件显得很繁琐,所以这里使用 content_by_lua_file 来引入 Lua 脚本文件。
这个的目的是为了好管理你的项目,极力推荐
参考:
http://www.jb51.net/article/61451.htm
总结:以这种方式搭建的nginx + lua环境,在过程中很容易出错,因为受各个版本的限制,而且配置非常繁琐,如果后期遇到添加新的模块涉及重新编译nginx等等,所以接下来推荐另外一个搭建nginx + lua环境,你会发现春天来了,而且很简单,丝毫不影响它的快—–那就是openresty
Openresty 是一套基于NGINX核心的相对完整的Web应用开发框架,包含了ngx_lua在内的众多第三方优秀的NGINX C模块,同时也集成了一系列常用的lua-resty-*类库,例如redis, mysql等,特别地,Openresty依赖的NGINX核心和LuaJIT版本都是经过非常充分的测试的,也打了不少必要的补丁。
下载openresty压缩包 :
http://openresty.org/cn/download.html
cd openresty-1.9.7.4
make
make install (没错就这么快,就这么简单)
返回上一级目录会多出一个目录:openresty,你所需要的都会在这里,包括对mysql,redis等等都自动加上了,而且不需要什么配置。。。接下来的步骤和前面一样的
初学者lua注意事项
在平时测试或者开发中,建议开启禁止缓存lua
比如:
location /vtoken{
default_type ‘text/html’;
lua_code_cache off; 默认是启动的,生产环境可关闭
content_by_lua_file/usr/local/openresty/nginx/html/Control_center/lua/verifyToken.lua;
}
建议
也是由于工作需要,接触了这个轻量级的脚本,目前写过简单的脚本,比如操作redis,mysql等等。过程中了解的最多是它和C密切相关,它两互调函数等。这么折腾一下也渐渐感兴趣来了,虽然现在还体会不到它的优势,希望后期会有好的收获;学习的话先从环境开始搭建,看看lua的语法,根据自己的需求试着去写一些附带逻辑性的脚本,慢慢的来。。。
相关文章推荐
- nginx代理指定目录
- 访问Nginx发生SSL connection error的一种情况
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- Nginx 学习笔记(一)
- 网站502与504错误分析
- 用zabbix监控nginx_status状态
- 艰难完成 nginx + puma 部署 rails 4的详细记录
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍