Nginx
echo
模塊lua
模塊Mysql
ps: echo模塊,能夠讓咱們像在PHP使用echo同樣,打印出參數。
可是要加 default_type "text/html」;否則會出現請求頁面出現下載的狀況。html
location
:【http://www.cnblogs.com/zlingh...】使用luajit 的luarocks
相似於PHP的composer,mysql
安裝mysqlnginx
安裝lua 的mysql擴展前,必定要安裝mysql.
$ luarockt install luasql-mysql
redis
例子:sql
nginx.conf
location /luatest { default_type "text/html」; content_by_lua_file /Users/Keil/study/lua/test.lua; }
當頁面匹配到 luatest,就會執行test.lua腳本。配置好 default_type "text/html」;
,否則服務器會把test.lua文件下載下來。
ps:content_by_lua_file
命令:告訴服務器加載和執行lua文件。數據庫
test.lua中的代碼:服務器
local luasql =require("luasql.mysql") --建立環境對象 env = luasql.mysql() -- ----鏈接數據庫 conn = env:connect("wechats","root","","127.0.0.1") -- ----設置數據庫的編碼格式 conn:execute"SET NAMES UTF8" ----執行數據庫操做 cur = conn:execute("select * from status") row = cur:fetch({},"a") while row do var = string.format("%d %s\n", row.id, row.name) ngx.say(var) row = cur:fetch(row,"a") end conn:close() --關閉數據庫鏈接 env:close()
nginx -s reload
使用 openresty
也能夠作到這個,並且更簡單,openresty
是一個集成的擴展集合,裏面有nginx,也有lua,並且給用戶配置好了,lua的redis,mysql擴展。適合新的服務器中使用;若是已有服務器,又不影響業務,能夠手動安裝lua擴展,不影響業務的使用。composer
這兄弟寫的很詳細。按照上面走一遍就能夠了。fetch