1. 程式人生 > >OpenResty 最佳實踐學習--安裝和helloworld(1)

OpenResty 最佳實踐學習--安裝和helloworld(1)

一:簡介

OpenResty® 是一個基於 Nginx 與 Lua 的高效能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模組以及大多數的依賴項。用於方便地搭建能夠處理超高併發、擴充套件性極高的動態 Web 應用、Web 服務和動態閘道器。

OpenResty® 通過匯聚各種設計精良的 Nginx 模組(主要由 OpenResty 團隊自主開發),從而將 Nginx 有效地變成一個強大的通用 Web 應用平臺。這樣,Web 開發人員和系統工程師可以使用 Lua 指令碼語言調動 Nginx 支援的各種 C 以及 Lua 模組,快速構造出足以勝任 10K 乃至 1000K 以上單機併發連線的高效能 Web 應用系統。

OpenResty® 的目標是讓你的Web服務直接跑在 Nginx 服務內部,充分利用 Nginx 的非阻塞 I/O 模型,不僅僅對 HTTP 客戶端請求,甚至於對遠端後端諸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都進行一致的高效能響應。

總結和拓展:
1.OpenResty 是 Nginx 與 Lua 的結合;
2.OpenResty 是多程序模式,會有一個 master 程序和多個 worker 程序。Master 程序管理 worker 程序,向各 worker 程序傳送訊號,監控 work 程序狀態;
3.OpenResty 是非同步非阻塞 ;

怎樣理解阻塞非阻塞與同步非同步的區別?知乎
4.子查詢:OpenResty 中有三種方式發起子請求:capture、exec、redirect;
5.OpenResty 快取機制。

二:安裝

下載和安裝在Openresty的官網都有介紹,但是這裡還是要進行說明一下:下載和安裝
我的環境是 Linux CentOs 32位。

1.下載安裝包

我下載的是最新的Openresty版本, openresty-1.11.2.5.tar.gz,上傳到伺服器,進行解壓,命令如下:

tar -xzvf openresty-VERSION.tar.gz

示例中的 VERSION替換成 OpenResty的版本號, 比如 1.11.2.5。

2.安裝前的準備
yum install readline-devel pcre-devel openssl-devel gcc

yum線上安裝需要收費,如果yum不可以使用,使用本地yum源安裝,具體配置見 Linux學習——yum學習和光碟yum源搭建

3.安裝

在解壓完之後,進行安裝:

tar -xzvf openresty-VERSION.tar.gz
cd openresty-VERSION/
./configure
make
sudo make install

示例中的 VERSION替換成 OpenResty的版本號, 比如 1.11.2.5。

預設, openresty 程式會被安裝到/usr/local/openresty目錄。

三:Hello World

1.第一種常規配置方案

如果之前有使用過nginx,那麼這個配置和nginx很基本一致!
首先在新建兩個目錄,然後在conf/下新建nginx.conf ,我這裡當做測試用,命令如下

mkdir openresty
cd openresty/
mkdir logs/ conf/
cd conf

touch nginx.conf
vim nginx.conf

在nginx.conf 寫

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }
    }
}

然後啟動openresty,啟動命令和nginx一致。

/usr/local/openresty/nginx/sbin/nginx -c /dufy/openresty/conf/nginx.conf
#啟動後檢視一下服務
ps -ef | grep  nginx 
curl http://localhost:8080/
#<p>hello, world</p>
2.第二種Lua配置

在conf下面新建nginx.conf 和 lua.conf ,命令如下

touch nginx.conf  lua.conf

在lua.conf 中寫內容:

#lua.conf
server {
        listen 8080;
        location /lua {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world Lua!</p>")
            ';
        }
    }

在nginx.conf 中寫內容,並且需要新增 lua模組路徑:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
 #lua模組路徑,多個之間”;”分隔,其中”;;”表示預設搜尋路徑,預設到/usr/servers/nginx下找  
lua_package_path "/usr/servers/lualib/?.lua;;";  #lua 模組  
lua_package_cpath "/usr/servers/lualib/?.so;;";  #c模組 
include lua.conf #lua.conf和nginx.conf 在同一目錄下
}

啟動openrestyrest,然後訪問:

/usr/local/openresty/nginx/sbin/nginx -c /dufy/openresty/conf/nginx.conf
#啟動後檢視一下服務
ps -ef | grep  nginx 

curl http://localhost:8080/lua
#<p>hello, world Lua!</p>

四:參考文章


如果帥氣(美麗)、睿智(聰穎),和我一樣簡單善良的你看到本篇博文中存在問題,請指出,我虛心接受你讓我成長的批評,謝謝閱讀!
祝你今天開心愉快!

歡迎訪問我的csdn部落格,我們一同成長!

不管做什麼,只要堅持下去就會看到不一樣!在路上,不卑不亢!