Centos7環境下搭建Nginx+Lua+Redis進行數據存取


 

1.安裝依賴環境

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2.安裝LuaJIT

cd /usr/local/

mkdir LuaJIT

cd /usr/local/LuaJIT
wget http:
//luajit.org/download/LuaJIT-2.0.2.tar.gz
tar –xvf LuaJIT
-2.0.2.tar.gz cd LuaJIT-2.0.2 make install

3.安裝nginx

cd /usr/local/

mkdir nginx 

下載ngx_devel_kit

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

下載lua-nginx-module

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz

下載nginx

wget http://nginx.org/download/nginx-1.12.1.tar.gz 

解壓文件

tar -xvf ngx_devel_kit-0.3.0 .tar.gz

tar -xvf lua-nginx-module-0.10.9rc7.tar.gz

tar -xvf nginx-1.12.1.tar.gz

編譯nginx

cd nginx-1.12.1

./configure --prefix=/usr/local/nginx --add-module=../ngx_devel_kit-0.3.0 --add-module=../lua-nginx-module-0.10.9rc7

安裝

make

make install

或者合並兩個命令(4表示並行運行任務的數量)

make -j 4 && make install 

啟動nginx

cd /usr/local/nginx/sbin

./nginx

測試是否啟動成功:

訪問  http:127.0.0.1:80

顯示welcome to n ginx!表示啟動成功

 

 

 

關閉nginx

./nginx –s stop

 

4.安裝redis

cd /usr/local

mkdir redis 

下載

wget http://download.redis.io/releases/ redis-2.8.17.tar.gz

解壓

tar -xvf redis-2.8.17.tar.gz

進入解壓后的文件夾

cd redis-2.8.17

安裝

make

啟動redis(&表示后台運行)

cd src

./redis-server & 

啟動成功后按ctrl+c退出啟動界面

測試

./redis-cli

set foo liq  ---OK

get foo ---liq

關閉redis

SHUTDOWN

 

 

5.配置redis主從

 

5.1主reids 配置文件

 復制一份redis的配置文件:

cd /usr/local/redis/redis-2.8.17

mkdir –p 6379/data 6380/data

cp redis.conf  /usr/local/redis/redis-2.8.17/6379/data/redis-6379.conf

編輯文件

cd /usr/local/redis/ redis-2.8.17/6379/data/

vim redis-6379.conf
cd /usr/local/redis/ redis-2.8.17/6379/data/

vim redis-6379.conf

5.1.1

  daemonize no

  修改為:

  daemonize yes  (后台程序方式運行)

5.1.2

  pidfile /var/run/redis_6379.pid

  修改為:

  pidfile /usr/local/redis/redis-2.8.17/6379/redis_6379.pid

5.1.3

  //設置請求密碼

  requirepass system

5.1.4

  //設置數據文件路徑

  dir /usr/local/redis/redis-2.8.17/6379/data

 

 

5.2從reids 配置文件

 

cp /usr/local/redis/redis-2.8.17/6379/data/redis-6379.conf /usr/local/redis/redis-2.8.17/6380/data/redis-6380.conf

5.2.1

       port 改為6380

5.2.2

       pidfile 改為/usr/local/redis/redis-2.8.17/6380/redis_6380.pid

5.2.2

  刪除 requirepass system

5.2.3

  //設置數據文件路徑

  dir /usr/local/redis/redis-2.8.17/6380/data

5.2.4

  添加從屬關系

  slaveof 127.0.0.1 6379

5.2.5

  添加主redis訪問密碼

       masterauth system

 

 

5.3啟動redis

 

/usr/local/redis/redis-2.8.17/src/redis-server /usr/local/redis/redis-2.8.17/6379/redis-6379.conf
/usr/local/redis/redis-2.8.17/src/redis-server /usr/local/redis/redis-2.8.17/6380/redis-6380.conf

 

5.4測試主從

在主redis中存數據

cd /usr/local/redis/redis-2.8.17/src

./redis-cli -h 127.0.0.1 -p 6379 auth system

set foo test

ctrl+c退出

在從redis中取數據

./redis-cli -h 127.0.0.1 -p 6380

get foo

 

 

6.安裝lua-resty-redis

 

https://github.com/openresty/lua-resty-redis.git下載lua-resty-redis-master后解壓

將lib包安裝到lua庫

cd lua-resty-redis-master

make && make install

安裝完成之后在/usr/local/lib/lua/resty里面會有redis.lua

 

7.修改nginx配置文件

cd /usr/local/nginx/conf

vim nginx.conf

7.1

http內添加 lua_package_path "/usr/local/lib/lua/?.lua;;";

7.2

server  listen改為 9080;

7.3

server 內添加

location /lua/set {

             default_type 'text/plain';

           content_by_lua_file conf/lua/setKeyValue.lua;

    }

    location /lua/get {

             default_type 'text/plain';

           content_by_lua_file conf/lua/getKey.lua;

    }

 

完整配置文件nginx.cnof:

 

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    lua_package_path "/usr/local/lib/lua/?.lua;;";

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       9080;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

        }

        location /lua/set {

             default_type 'text/plain';

           content_by_lua_file conf/lua/setKeyValue.lua;

        }

        location /lua/get {

             default_type 'text/plain';

           content_by_lua_file conf/lua/getKey.lua;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }
    }
}

 

8.編寫content_by_lua_file對應的lua腳本

  8.1

#對應nginx中的配置:  content_by_lua_file conf/lua/setKeyValue.lua;

cd /usr/local/nginx/conf

mkdir lua

cd lua

vim setKeyValue.lua

setKeyValue.lua
代碼:
--receive request params

local request_method = ngx.var.request_method

local args = nil

local key = nil

local value = nil

--獲取參數的值

if "GET" == request_method then

    args = ngx.req.get_uri_args()

elseif "POST" == request_method then

    ngx.req.read_body()

    args = ngx.req.get_post_args()

end

key = args["key"]

value = args["value"]

 

--connect redis

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 

 

-- 請注意這里 auth 的調用過程

-- check password

local count

count, err = cache:get_reused_times()

if 0 == count then

    ok, err = cache:auth("system")

if not ok then

       ngx.say("failed to auth: ", err)

       return

end

elseif err then

    ngx.say("failed to get reused times: ", err)

return

end

 

local res, err = cache:set(key, value) 

if not res then 

        ngx.say("failed to set "..key..": ", err) 

        return 

end 

 

if res == ngx.null then 

        ngx.say(key.." not found.") 

        return 

end 

 

ngx.say("set redis value >>> "..key..": ", res) 

 

 

local ok, err = cache:close() 

 

if not ok then 

        ngx.say("failed to close:", err) 

        return 

end 

 

8.2

#對應nginx中的配置: content_by_lua_file conf/lua/getKey.lua;

vim getKey.lua

getKey.lua代碼:

 

--receive request params

local request_method = ngx.var.request_method

local args = nil

local key = nil

local value = nil

--獲取參數的值

if "GET" == request_method then

    args = ngx.req.get_uri_args()

elseif "POST" == request_method then

    ngx.req.read_body()

    args = ngx.req.get_post_args()

end

key = args["key"]

value = args["value"]

 

--connect redis

local redis = require "resty.redis"

 

local cache = redis.new() 

 

local ok, err = cache.connect(cache, '127.0.0.1', '6380') 

 

cache:set_timeout(60000) 

 

if not ok then 

        ngx.say("failed to connect:", err) 

        return 

end 

 

local res, err = cache:get(key) 

if not res then 

        ngx.say("failed to get "..key..": ", err) 

        return 

end 

 

if res == ngx.null then 

        ngx.say(key.." not found.") 

        return 

end 

ngx.say("get from redis >>> "..key..": ", res) 

local ok, err = cache:close() 

 

if not ok then 

        ngx.say("failed to close:", err) 

        return 

end  

 

9.開啟端口映射(如果不是安裝在虛擬機上的不需要此步驟)

9.1開啟虛擬機端口映射:

 

9.2開放9080端口的防火牆(--permanent永久生效,沒有此參數重啟后失效):

firewall-cmd --zone=public --add-port=9080/tcp  --permanent

 

10.重新啟動nginx

cd /usr/local/nginx/sbin

./nginx 

11.查看效果

 

 

存值 -- 訪問網址並傳參: 

http://localhost:9080/lua/set?key=foo&value=test1

 

 

取值 -- 訪問網址並傳參:

http://localhost:9080/lua/get?key=foo

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM