您的位置:首页 > 运维架构 > Linux

在CentOS 6.9 x86_64的OpenResty 1.13.6.1上使用LuaRocks示例

2017-12-29 16:08 555 查看
下面是我阅读春哥OpenResty官网主页中“Using LuaRocks”一节的实操记录,整理如下。

https://openresty.org/cn/using-luarocks.html

1.在CentOS 6.9 x86_64搭建Lua开发环境

详细过程参见本博博文

http://blog.csdn.net/tao_627/article/details/78925211

2.通过LuaRocks安装 Lua MD5 库

在本示例中, 我们将使用Lua MD5 library作为服务器上的一个例子, 所以我们需要通过LuaRocks来安装它:

luarocks install md5

3.配置我们的OpenResty应用

vim nginx.conf

添加以下内容

worker_processes  1;   # we could enlarge this setting on a multi-core machine
user root;
error_log  logs/error.log warn;

events {
worker_connections  1024;
}

http {
#must use absolute path
lua_package_path '/root/or_test/conf/using_luarocks/?.lua;;';

server {
listen       80;
server_name  localhost;

location = /luarocks {
#rewrite_by_lua_file "conf/using_luarocks/foo.lua";
content_by_lua '
local foo = require("foo")
foo.say("hello, luarocks!")
--ngx.say("Hello world!")
';
}
}
}


我们希望最终的目录结构如下



创建与conf下面的using_luarocks子文件夹存放lua文件

mkdir -p /root/or_test/conf/using_luarocks

存入foo.lua

module("foo", package.seeall)

local bar = require "bar"

ngx.say("bar loaded")

function say (var)
bar.say(var)
end
存入bar.lua
module("bar", package.seeall)

local rocks = require "luarocks.loader"
local md5 = require "md5"

ngx.say("rocks and md5 loaded")

function say (a)
ngx.say(md5.sumhexa(a))
end

4.开启Nginx服务

首先测试配置文件合法性

nginx -p ~/or_test -c ~/or_test/conf/using_luarocks.conf -t

重启OpenResty服务

nginx -p ~/or_test -c ~/or_test/conf/using_luarocks.conf -s reload

查看进程是否正常

ps auxf | grep nginx

查看端口是否启动

netstat -ntlp



5.测试我们的应用

现在我们通过curl 工具或者任意兼容HTTP协议的浏览器测试我们的应用:

curl -v http://localhost/luarocks
我们在第一次运行的时候得到以下的内容:

rocks and md5 loaded

bar loaded

85e73df5c41378f830c031b81e4453d2



第二次运行的时候得到以下内容:

85e73df5c41378f830c031b81e4453d2



6.基准测试

现在,让我们来做一些基准测试吧:

ab -c10 -n50000 http://127.0.0.1/luarocks
测试在是我的CentOS 6.9 x86_64虚拟机上进行的, 下面是测试中产生的数据





7.特殊说明

这里OpenResty默认包含了LuaJIT,系统中没有完整的lua程序,我使用源码安装了完整的lua,并继续安装了模块管理工具LuaRocks,

使用LuaRocks去安装其它lua相关的模块(OpenResty中没有包含的),并通过在nginx.conf中引用这些lua脚本来实现一些业务需求,比如这里的md5模块。这个思路是可行的。

8.参考文献

[1].https://openresty.org/cn/using-luarocks.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: