Home > Article > Backend Development > Another way to build nginx_lua environment
It’s very simple, download ngx_openresty, the integration package includes: Nginx, Lua or Luajit, ngx_lua, and some useful Nginx third-party modules.
For example:
nginx’s third-party module redis. This package is essentially a .lua file, which is a library file that provides some interfaces for accessing redis:Download it:
git clone https:// github.com/agentzh/lua-resty-redis.git
Copy:
In this package, there is a Lib directory. Copy the files and subdirectories in the Lib directory to the directory configured by lua_package_path above (here is /data /nginx-1.4.2/)
Write a simple lua program to connect to redis and get the content inside:
For example: write a test_redis.lua and put it under /data0/nginx-1.4.2/lua/local redis = require "resty.redis" local cache = redis.new() local ok, err = cache.connect(cache, '127.0.0.1', '6379') cache:set_timeout(60000) if not ok then ngx.say("failed to connect:", err) return end res, err = cache:set("dog", "an aniaml") if not ok then ngx.say("failed to set dog: ", err) return end ngx.say("set result: ", res) local res, err = cache:get("dog") if not res then ngx.say("failed to get dog: ", err) return end if res == ngx.null then ngx.say("dog not found.") return end ngx.say("dog: ", res) local ok, err = cache:close() if not ok then ngx.say("failed to close:", err) return end
The above introduces another method of setting up the nginx_lua environment, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.