【雲計算的1024種玩法】手把手教你如何編譯一個高性能 OpenResty

介紹

本教程將介紹如何一步一步手動編譯 OpenResty,OpenResty 是一個基於 Nginx 與 Lua 的高性能 Web 平臺,擁有很是好的拓展性讓服務器發揮更好性能。教程中將所有依賴 Linux 發行版組建中的依賴,而免除編譯帶來的後期維護成本。html

我有幾張阿里雲幸運券分享給你,用券購買或者升級阿里雲相應產品會有特惠驚喜哦!把想要買的產品的幸運券都領走吧!快下手,立刻就要搶光了。nginx

OpenResty 的額外拓展:git

  1. OpenSSL 1.0.2,提供 ALPN 支持,支持 HTTP/2github

  2. Nginx-CT,透明證書提升 HTTPS 網站的安全性和瀏覽器支持瀏覽器

  3. ngx_PageSpeed,Google 家的網站性能優化工具緩存

  4. Brotli,實現比 Gzip 更高的壓縮率安全

  5. Jemalloc,優化內存管理性能優化

準備

雲翼計劃

學生用戶能夠在阿里雲官網上進行學生認證後購買必定配置的ECS,僅需9.9元每個月,學生用戶無需擔憂花費過多用在服務器的問題。bash

學生用戶在通過學生認證事後就能夠在相關網址進行購買,網址爲:promotion.aliyun.com/ntms/campus…
買好了服務器就能夠去剛剛註冊好的帳號管理裏的管理控制檯去查看服務器以及它的一些配置。服務器

遠程控制

【雲計算的1024種玩法】使用 DMS 只要一個瀏覽器輕鬆搞定運維任務
【雲計算的1024種玩法】ECS和輕量應用服務器的遠程控制入門

設置安全組(重要)

若是不設置好安全組,到底是沒法訪問仍是編譯失敗會分不清的。

須要開放 : 80 和 443 端口

【雲計算的1024種玩法】用好阿里雲的安全組

教程

本教程以,Ubuntu 16.04 LTS 64位版 爲例。

設定版本變量

若是軟件版本更新後,爲了方便起見,後續修改版本號只需修改下面的變量便可。 在 SSH終端 中輸入:

# VersionOpenSSLVersion='openssl-1.0.2l';NginxCTVersion='1.3.2';PageSpeedVersion='1.12.34.2';SystemBit='X64';OpenRestyVersion='openresty-1.11.2.5';複製代碼

注: 截止本次更新,OpenResty 1.11.2 版本最高只能搭配 OpenSSL 1.0.2。
上述軟件版本更新查看: OpenSSLNginx-CTPageSpeedOpenResty

安裝依賴

更新系統軟件源緩存順便升級組件:

apt updateapt upgrade -y複製代碼

安裝依賴組件:

apt install build-essential libreadline-dev libncurses5-dev libpcre3 libpcre3-dev libssl-dev zlib1g-dev unzip git perl make libjemalloc1 libjemalloc-dev複製代碼

下載源碼

這裏將 OpenResty 所需的源代碼均放置在 /root/src 目錄下,方便管理。

cd /rootmkdir srccd src複製代碼

下載 OpenResty 和其拓展的源代碼:

#下載 OpenSSL,Ubuntu 16.04(不包括)如下版本請刪除下面的 # 如下載#wget https://www.openssl.org/source/$OpenSSLVersion.tar.gz#tar xzf $OpenSSLVersion.tar.gzwget https://github.com/grahamedgecombe/nginx-ct/archive/v$NginxCTVersion.tar.gz
tar xzf v$NginxCTVersion.tar.gz

git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
git submodule update --init
cd ../

wget https://github.com/pagespeed/ngx_pagespeed/archive/v$PageSpeedVersion-beta.zip
unzip v$PageSpeedVersion-beta.zip
cd ngx_pagespeed-$PageSpeedVersion-beta/
wget https://dl.google.com/dl/page-speed/psol/$PageSpeedVersion-$SystemBit.tar.gz
tar -xzvf $PageSpeedVersion-$SystemBit.tar.gz
cd ../

wget -c https://openresty.org/download/$OpenRestyVersion.tar.gz
tar zxf $OpenRestyVersion.tar.gz 查看更多複製代碼

編譯 OpenResty

cd $OpenRestyVersion
./configure --prefix=/usr/local/openresty \
--user=www-data --group=www-data \--add-module=../ngx_brotli \--add-module=../nginx-ct-$NginxCTVersion \--add-module=../ngx_pagespeed-$PageSpeedVersion-beta \--with-http_v2_module \--with-http_ssl_module \--with-http_gzip_static_module \--with-ld-opt='-ljemalloc'make && make install複製代碼

Ubuntu 16.04(不包括) 如下版本請在倒數第二行添加:

--with-openssl=../$OpenSSLVersion \複製代碼

設置變量

[ -z "`grep ^'export PATH=' /etc/profile`" ] && echo "export PATH=/usr/local/openresty/nginx/sbin:\$PATH" >> /etc/profile
[ -n "`grep ^'export PATH=' /etc/profile`" -a -z "`grep /usr/local/openresty/ /etc/profile`" ] && sed -i "s@^export PATH=\(.*\)@export PATH=/usr/local/openresty/nginx/sbin:\1@" /etc/profile
. /etc/profile複製代碼

後面就能夠用,nginx -t 檢測配置是否正確,nginx -s reload 重載 Nginx 了。

建立相關目錄

mkdir /data/wwwlogs/ -p
mkdir /data/wwwroot/default/ -p
cp /usr/local/openresty/nginx/html/index.html /data/wwwroot/default/複製代碼

設置服務和開機啓動

建立 /etc/systemd/system/openresty.service 文件,內容:

cd /etc/systemd/system/wget https://gist.githubusercontent.com/ivmm/dbf03e6c7970488652878bb8ddc3a775/raw/48436d911d08e57774c759bdb50548dec31dc86f/openresty.service複製代碼

編輯 /usr/local/openresty/nginx/conf/nginx.conf 文件爲:

cd /usr/local/openresty/nginx/conf/rm nginx.conf -rf
wget https://gist.githubusercontent.com/ivmm/ab81dee184b64036bd4b8d5abe676264/raw/1cbfbc387aa956f6d9afe39d60e2b8c988a10688/nginx.conf複製代碼

從新加載 systemd 服務,以便它能夠找到咱們的文件:

systemctl daemon-reload複製代碼

經過 systemd 重啓 OpenResty:

systemctl restart openresty複製代碼

設置開機啓動:

systemctl enable openresty複製代碼

打開你的服務器 IP,就能看到安裝好的 OpenResty 提示頁了 pics.mf8.biz/mf8/awxi3.p…image

相關文章
相關標籤/搜索