CentOS安裝OpenResty(Nginx+Lua)開發環境

一.簡介


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

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

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

總結和拓展:nginx

  • OpenResty 是 Nginx 與 Lua 的結合;
  • OpenResty 是多進程模式,會有一個 master 進程和多個 worker 進程。Master 進程管理 worker 進程,向各 worker 進程發送信號,監控 work 進程狀態;
  • OpenResty 是異步非阻塞 ;怎樣理解阻塞非阻塞與同步異步的區別?知乎
  • 子查詢:OpenResty 中有三種方式發起子請求:capture、exec、redirect;
  • OpenResty 緩存機制。

Nginx+Lua架構思惟導圖:
Nginx+Lua架構思惟導圖git

二.關閉SELinux


先臨時關閉,而後永久關閉github

setenforce 0 # 臨時關閉
sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config # 永久關閉

三.防火牆開啓80端口


先臨時開啓80端口,而後再永久開啓web

iptables -I INPUT -p tcp --dport 80 -j ACCEPT # 臨時生效
/etc/init.d/iptables save # 保存到配置文件,永久生效
/etc/init.d/iptables status # 查看iptables當前狀態

四.yum安裝


對於一些常見的 Linux 發行版本,OpenResty® 提供 官方預編譯包。確保你首先用這種方式來安裝。redis

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

yum install yum-utils -y
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

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

yum install openresty -y

若是你想安裝命令行工具 resty,那麼能夠像下面這樣安裝 openresty-resty 包:

yum install openresty-resty -y

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

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

yum --disablerepo="*" --enablerepo="openresty" list available

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

五.源碼包編譯安裝


下載

從下載頁 Download下載最新的 OpenResty® 源碼包,而且像下面的示例同樣將其解壓:

wget https://openresty.org/download/openresty-1.13.6.2.tar.gz

安裝前的準備

必須將這些庫 perl 5.6.1+, libpcre, libssl安裝在您的電腦之中。 對於 Linux來講, 您須要確認使用 ldconfig 命令,讓其在您的系統環境路徑中能找到它們。

推薦您使用yum安裝如下的開發庫:

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

安裝

tar -zxvf openresty-1.13.6.2.tar.gz
cd openresty-1.13.6.2
./configure
make
make install

若是您的電腦支持多核 make 工做的特性, 您能夠這樣編譯:

make -j2

默認, --prefix=/usr/local/openresty 程序會被安裝到/usr/local/openresty目錄。您能夠指定各類選項,好比:

./configure --prefix=/opt/openresty \
            --with-luajit \
            --without-http_redis2_module \
            --with-http_iconv_module \
            --with-http_postgres_module

試着使用 ./configure --help 查看更多的選項。

六.配置


第一種常規配置方案

修改nginx.conf配置文件

cd /usr/local/openresty/nginx/conf
mv nginx.conf nginx.conf.$(date +%Y%m%d)
vim 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>") ';
        }
    }
}

添加環境變量

echo "export PATH=$PATH:/usr/local/openresty/nginx/sbin" >> /etc/profile
source /etc/profile

而後啓動openresty,啓動命令和nginx一致。

nginx -c /usr/local/openresty/nginx/conf/nginx.conf

啓動後查看一下服務

ps -ef | grep  nginx

訪問 Web 服務

curl http://localhost:8080/

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

<p>hello, world</p>

重啓 Web 服務

nginx  -s reload

第二種lua配置方案

添加lua.conf配置文件

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

修改nginx.conf配置文件

cd /usr/local/openresty/nginx/conf
mv nginx.conf nginx.conf.$(date +%Y%m%d)
vim nginx.conf
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    #lua模塊路徑,多個之間」;」分隔,其中」;;」表示默認搜索路徑,默認到/usr/servers/nginx下找  
    lua_package_path "/usr/local/openresty/lualib/?.lua;;";  #lua模塊
    lua_package_cpath "/usr/local/openresty/lualib/?.so;;";  #c模塊 
    include lua.conf; #lua.conf和nginx.conf在同一目錄下
}

添加環境變量

echo "export PATH=$PATH:/usr/local/openresty/nginx/sbin" >> /etc/profile
source /etc/profile

而後啓動openresty,啓動命令和nginx一致。

nginx -c /usr/local/openresty/nginx/conf/nginx.conf

#啓動後查看一下服務

ps -ef | grep  nginx

訪問 Web 服務

curl http://localhost:8080/

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

<p>hello, world Lua!</p>

配置lua代碼文件

咱們把lua代碼放在nginx配置中會隨着lua的代碼的增長致使配置文件太長很差維護,所以咱們應該把lua代碼移到外部文件中存儲。

在conf文件夾下建立lua文件夾,專門用來存放lua文件

mkdir /usr/local/openresty/nginx/conf/lua

建立test.lua文件

cd /usr/local/openresty/nginx/conf/lua
vim test.lua
ngx.say("test lua");

修改conf/lua.conf文件

vim /usr/local/openresty/nginx/conf/lua.conf
server {
        listen 8080;
        location / {
            default_type text/html;
            lua_code_cache off; #關閉lua代碼緩存,調試時關閉,正式環境開啓
            content_by_lua_file conf/lua/test.lua; #相對於nginx安裝目錄
        }
    }

關閉緩存後會看到以下報警(忽略無論)

nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/lua.conf:5

重啓 Web 服務

nginx  -s reload

七.測試性能


安裝壓力測試工具ab

yum -y install httpd-tools

壓力測試

  • -c:每次併發數爲10個
  • -n:共發送50000個請求
ab -c10 -n50000 http://localhost:8080/

測試報詳解

Server Software:        Apache          #服務器軟件
Server Hostname:        localhost       #域名
Server Port:            80              #請求端口號

Document Path:          /               #文件路徑
Document Length:        40888 bytes     #頁面字節數

Concurrency Level:      10              #請求的併發數
Time taken for tests:   27.300 seconds  #總訪問時間
Complete requests:      1000            #請求成功數量
Failed requests:        0               #請求失敗數量
Write errors:           0
Total transferred:      41054242 bytes  #請求總數據大小(包括header頭信息)
HTML transferred:       40888000 bytes  #html頁面實際總字節數
Requests per second:    36.63 [#/sec] (mean)  #每秒多少請求,這個是很是重要的參數數值,服務器的吞吐量
Time per request:       272.998 [ms] (mean)     #用戶平均請求等待時間 
Time per request:       27.300 [ms] (mean, across all concurrent requests)
                                                # 服務器平均處理時間,也就是服務器吞吐量的倒數 
Transfer rate:          1468.58 [Kbytes/sec] received  #每秒獲取的數據長度

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       43   47   2.4     47      53
Processing:   189  224  40.7    215     895
Waiting:      102  128  38.6    118     794
Total:        233  270  41.3    263     945

Percentage of the requests served within a certain time (ms)
  50%    263    #50%用戶請求在263ms內返回
  66%    271    #66%用戶請求在271ms內返回
  75%    279    #75%用戶請求在279ms內返回
  80%    285    #80%用戶請求在285ms內返回
  90%    303    #90%用戶請求在303ms內返回
  95%    320    #95%用戶請求在320ms內返回
  98%    341    #98%用戶請求在341ms內返回
  99%    373    #99%用戶請求在373ms內返回
 100%    945 (longest request)

八.啓動腳本


添加啓動腳本

vim /etc/init.d/openresty

腳本內容以下

#!/bin/sh
#
# openresty - this script starts and stops the openresty daemin
#
# chkconfig:   - 85 15
# description:  OpenResty is a full-fledged web platform that integrates \ 
#		the standard Nginx core, LuaJIT, many carefully written Lua libraries, \
#		lots of high quality 3rd-party Nginx modules, and most of their external dependencies.
# processname: openresty
# config:      /usr/local/openresty/nginx/conf/nginx.conf
# pidfile:     /usr/local/openresty/nginx/logs/nginx.pid
# Url http://www.cnblogs.com/wushuaishuai/
# Last Updated 2018.07.15

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/openresty/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/openresty/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/openresty/nginx/logs/nginx.pid"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    #service php-fpm start
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    $nginx -s stop
    echo_success
    retval=$?
    echo
    #service php-fpm stop
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    $nginx -s reload
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

賦予執行權限

chmod u+x /etc/init.d/openresty

九.使用systemctl來管理openresty

#查看openresty狀態
systemctl status openresty.service

#啓動openresty
systemctl start openresty.service

#設置openresty開機自啓動
systemctl enable openresty.service

#重啓openresty
systemctl restart openresty.service

#中止openresty
systemctl stop openresty.service

#取消openresty開機自啓動
systemctl disable openresty.service

十.參考資料


跟我學OpenResty(Nginx+Lua)開發目錄貼

相關文章
相關標籤/搜索