安裝OpenResty(Nginx倉庫)
首先你的Linux虛擬機必須聯網
1)安裝開發庫
首先要安裝OpenResty的依賴開發庫,執行命令:
yum install -y pcre-devel openssl-devel gcc --skip-broken2)安裝OpenResty倉庫
你可以在你的 CentOS 系統中添加 openresty 倉庫,這樣就可以便于未來安裝或更新我們的軟件包(通過 yum check-update 命令)。運行下面的命令就可以添加我們的倉庫:
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo如果提示說命令不存在,則運行:
yum install -y yum-utils然后再重復上面的命令
3)安裝OpenResty
然后就可以像下面這樣安裝軟件包,比如 openresty:
yum install -y openresty4)安裝opm工具
opm是OpenResty的一個管理工具,可以幫助我們安裝一個第三方的Lua模塊。
如果你想安裝命令行工具 opm,那么可以像下面這樣安裝 openresty-opm 包:
yum install -y openresty-opm5)目錄結構
默認情況下,OpenResty安裝的目錄是:/usr/local/openresty
看到里面的nginx目錄了嗎,OpenResty就是在Nginx基礎上集成了一些Lua模塊。
6)配置nginx的環境變量
打開配置文件:
vi /etc/profile在最下面加入兩行:
export NGINX_HOME=/usr/local/openresty/nginxexport PATH=${NGINX_HOME}/sbin:$PATHNGINX_HOME:后面是OpenResty安裝目錄下的nginx的目錄
然后讓配置生效:
source /etc/profile2.啟動和運行
OpenResty底層是基于Nginx的,查看OpenResty目錄的nginx目錄,結構與windows中安裝的nginx基本一致:
所以運行方式與nginx基本一致:
# 啟動nginxnginx# 重新加載配置nginx -s reload# 停止nginx -s stopnginx的默認配置文件注釋太多,影響后續我們的編輯,這里將nginx.conf中的注釋部分刪除,保留有效部分。
修改/usr/local/openresty/nginx/conf/nginx.conf文件,內容如下:
#user nobody;worker_processes 1;error_log logs/error.log;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfileon; keepalive_timeout 65; server {listen 8081;server_name localhost;location / { root html; index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html { root html;} }}在Linux的控制臺輸入命令以啟動nginx:
nginx然后訪問頁面:http://192.168.150.101:8081,注意ip地址替換為你自己的虛擬機IP:
3.備注
加載OpenResty的lua模塊:
#lua 模塊lua_package_path '/usr/local/openresty/lualib/?.lua;;';#c模塊 lua_package_cpath '/usr/local/openresty/lualib/?.so;;';common.lua
-- 封裝函數,發送http請求,并解析響應local function read_http(path, params) local resp = ngx.location.capture(path,{method = ngx.HTTP_GET,args = params, }) if not resp then-- 記錄錯誤信息,返回404ngx.log(ngx.ERR, 'http not found, path: ', path , ', args: ', args)ngx.exit(404) end return resp.bodyend-- 將方法導出local _M = { read_http = read_http} return _M釋放Redis連接API:
-- 關閉redis連接的工具方法,其實是放入連接池local function close_redis(red) local pool_max_idle_time = 10000 -- 連接的空閑時間,單位是毫秒 local pool_size = 100 --連接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) if not ok thenngx.log(ngx.ERR, '放入redis連接池失敗: ', err) endend讀取Redis數據的API:
-- 查詢redis的方法 ip和port是redis地址,key是查詢的keylocal function read_redis(ip, port, key) -- 獲取一個連接 local ok, err = red:connect(ip, port) if not ok thenngx.log(ngx.ERR, '連接redis失敗 : ', err)return nil end -- 查詢redis local resp, err = red:get(key) -- 查詢失敗處理 if not resp thenngx.log(ngx.ERR, '查詢Redis失敗: ', err, ', key = ' , key) end --得到的數據為空處理 if resp == ngx.null thenresp = nilngx.log(ngx.ERR, '查詢Redis數據為空, key = ', key) end close_redis(red) return respend開啟共享詞典:
# 共享字典,也就是本地緩存,名稱叫做:item_cache,大小150mlua_shared_dict item_cache 150m;p == ngx.null thenresp = nilngx.log(ngx.ERR, "查詢Redis數據為空, key = ", key)endclose_redis(red)return respend
開啟共享詞典:```nginx# 共享字典,也就是本地緩存,名稱叫做:item_cache,大小150mlua_shared_dict item_cache 150m;到此這篇關于安裝OpenResty(Nginx倉庫)的文章就介紹到這了,更多相關OpenResty Nginx倉庫內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. Tomcat配置https SSL證書的項目實踐2. 關于Linux搭建RabbitMQ集群環境圖文詳解3. 關于Read-only file system問題的解決4. linux如何通過防火墻iptables做隔離端口的腳本5. Linux系統 第2節 虛擬機中安裝Kali系統6. 關于Windows Server 2012上安裝.NET Framework 3.5的問題7. iis7.5中讓html與shtml一樣支持include功能(添加模塊映射)8. 分布式監控系統之Zabbix 使用SNMP、JMX信道采集數據的原理解析9. IDEA2022創建Web項目配置Tomcat的詳細圖文說明10. ubuntu開機后ROS程序自啟動問題
