在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://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 pathlua_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)
endmodule("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))
end4.開啟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 6.9 x86_64的OpenResty 1.13.6.1上使用LuaRocks示例的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 在CentOS 6.9 x86_64搭建
- 下一篇: 在CentOS 6.9 x86_64的O
