Centos7 下 Openresty 從安裝到入門

Openresty簡介

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

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

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

參考 組件 能夠知道 OpenResty® 中包含了多少軟件。shell

參考 上路 學習如何從最簡單的 hello world 開始使用 OpenResty® 開發 HTTP 業務,或前往 下載 直接獲取 OpenResty® 的源代碼包開始體驗。後端

官網安裝說明

根據官網的描述提供了不一樣的安裝方式,例如:使用yum安裝、源碼編譯安裝等等。我目前暫時使用yum安裝方式進行部署看看。centos

設置安裝的yum源

你能夠在你的 CentOS 系統中添加 openresty 倉庫,這樣就能夠便於將來安裝或更新咱們的軟件包(經過 yum update 命令)。運行下面的命令就能夠添加咱們的倉庫:bash

sudo yum install yum-utils -y
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
複製代碼

根據上面的命令,先執行看看,以下:服務器

安裝Openresty

而後就能夠像下面這樣安裝軟件包,好比 openrestysession

sudo yum install openresty -y
複製代碼

安裝命令行工具 resty

安裝 openresty-resty 包:併發

sudo yum install openresty-resty -y
複製代碼

安裝以後,就能夠查看一下版本號,以下:

[root@centos7 ~]# resty -V
resty 0.21
nginx version: openresty/1.13.6.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.1.0h  27 Mar 2018
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl/include' --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.13 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../ngx_stream_lua-0.0.5 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl/lib' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-dtrace-probes --with-stream --with-stream_ssl_module --with-http_ssl_module
[root@centos7 ~]# 
複製代碼

查看Opm工具

命令行工具 opmopenresty-opm 包裏,而 restydoc 工具在 openresty-doc 包裏頭。

列出全部 openresty 倉庫裏頭的軟件包:

sudo yum --disablerepo="*" --enablerepo="openresty" list available
複製代碼

從上圖能夠看出,還有不少工具包能夠安裝。可是目前,我先不安裝了。等到須要的時候,再進行安裝。

參考 OpenResty RPM 包頁面獲取這些包更多的細節。

入門

在上面已經安裝好了Openresty以後,下面能夠部署一個Hello world的示例。

建立相關工做目錄

建立work目錄以及相應的log日誌目錄、conf配置目錄

mkdir ~/work
cd ~/work
mkdir logs/ conf/
複製代碼

配置命令:

[root@centos7 ~]# cd ~
[root@centos7 ~]# 
[root@centos7 ~]# mkdir work
[root@centos7 ~]# 
[root@centos7 ~]# cd work/
[root@centos7 work]# ls
[root@centos7 work]# mkdir logs
[root@centos7 work]# 
[root@centos7 work]# mkdir conf
[root@centos7 work]# 
[root@centos7 work]# ls
conf  logs
[root@centos7 work]# pwd
/root/work
[root@centos7 work]# 
複製代碼

建立logs/用於記錄文件和conf/配置文件的目錄。

準備nginx.conf配置文件

建立一個簡單的純文本文件,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>")
            ';
        }
    }
}
複製代碼

若是您熟悉Nginx配置,那麼您應該很是熟悉它。不管如何,OpenResty只是一個加強版的 Nginx。您能夠充分利用Nginx世界中全部現有的好東西。

啓動Nginx服務器

假設你已經安裝了OpenResty/usr/local/openresty(這是默認值),咱們使咱們的nginx咱們的可執行OpenResty咱們可用的安裝PATH環境:

PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH
複製代碼

執行步驟以下

[root@centos7 conf]# ls /usr/local/openresty
bin  COPYRIGHT  luajit  lualib  nginx  openssl  pcre  site  zlib
[root@centos7 conf]# 
[root@centos7 conf]# ls /usr/local/openresty/nginx/
conf  html  logs  sbin  tapset
[root@centos7 conf]# ls /usr/local/openresty/nginx/sbin/
nginx  stap-nginx
[root@centos7 conf]# 
[root@centos7 conf]# PATH=/usr/local/openresty/nginx/sbin:$PATH
[root@centos7 conf]# export PATH
[root@centos7 conf]# env | grep open
PATH=/usr/local/openresty/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 conf]# 
[root@centos7 conf]# nginx -V
nginx version: openresty/1.13.6.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.1.0h  27 Mar 2018
複製代碼

而後咱們以這種方式用咱們的配置文件啓動nginx服務器:

[root@centos7 conf]# cd ..
[root@centos7 work]# ls
conf  logs
[root@centos7 work]# pwd
/root/work
[root@centos7 work]# 
[root@centos7 work]# nginx -p `pwd`/ -c conf/nginx.conf 
[root@centos7 work]# ps -ef | grep nginx
root       2199   2181  0 14:57 ?        00:00:00 nginx: master process nginx -g daemon off;
104        2227   2199  0 14:57 ?        00:00:00 nginx: worker process
root       2489      1  0 15:41 ?        00:00:00 nginx: master process nginx -p /root/work/ -c conf/nginx.conf
nobody     2490   2489  0 15:41 ?        00:00:00 nginx: worker process
root       2504   1933  0 15:42 pts/0    00:00:00 grep --color=auto nginx
[root@centos7 work]# 
複製代碼

錯誤消息將轉到stderr設備或logs/error.log(當前工做目錄中的默認錯誤日誌文件)。

訪問咱們的HelloWorld Web服務

咱們可使用curl訪問咱們的HelloWorld新Web服務:

[root@centos7 work]# curl http://localhost:8080/

<p>hello, world</p>
[root@centos7 work]# 
複製代碼

若是一切正常,咱們應該獲得輸出

<p>hello, world</p>
複製代碼

相關文章
相關標籤/搜索