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

前端之家收集整理的这篇文章主要介绍了在CentOS 6.9 x86_64的OpenResty 1.13.6.1上使用LuaRocks示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是我阅读春哥OpenResty官网主页中“Using LuaRocks”一节的实操记录,整理如下。

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

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

详细过程参见本博博文

http://www.jb51.cc/article/p-ylsveqwv-bro.html

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

3.配置我们的OpenResty应用
vim Nginx.conf
添加以下内容

  1. worker_processes 1; # we could enlarge this setting on a multi-core machine
  2. user root;
  3. error_log logs/error.log warn;
  4.  
  5. events {
  6. worker_connections 1024;
  7. }
  8.  
  9. http {
  10. #must use absolute path
  11. lua_package_path '/root/or_test/conf/using_luarocks/?.lua;;';
  12.  
  13. server {
  14. listen 80;
  15. server_name localhost;
  16.  
  17. location = /luarocks {
  18. #rewrite_by_lua_file "conf/using_luarocks/foo.lua";
  19. content_by_lua '
  20. local foo = require("foo")
  21. foo.say("hello,luarocks!")
  22. --ngx.say("Hello world!")
  23. ';
  24. }
  25. }
  26. }

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


创建与conf下面的using_luarocks子文件夹存放lua文件
mkdir -p /root/or_test/conf/using_luarocks
存入foo.lua

  1. module("foo",package.seeall)
  2.  
  3. local bar = require "bar"
  4.  
  5. ngx.say("bar loaded")
  6.  
  7. function say (var)
  8. bar.say(var)
  9. end
存入bar.lua
  1. module("bar",package.seeall)
  2.  
  3. local rocks = require "luarocks.loader"
  4. local md5 = require "md5"
  5.  
  6. ngx.say("rocks and md5 loaded")
  7.  
  8. function say (a)
  9. ngx.say(md5.sumhexa(a))
  10. 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

猜你在找的CentOS相关文章