閱讀946 返回首頁    go 技術社區[雲棲]


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

介紹

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

OpenResty 的額外拓展:

  1. OpenSSL 1.0.2,提供 ALPN 支持,支持 HTTP/2
  2. Nginx-CT,透明證書提高 HTTPS 網站的安全性和瀏覽器支持
  3. ngx_PageSpeed,Google 家的網站性能優化工具
  4. Brotli,實現比 Gzip 更高的壓縮率
  5. Jemalloc,優化內存管理

準備

雲翼計劃

學生用戶可以在阿裏雲官網上進行學生認證後購買一定配置的ECS,僅需9.9元每月,學生用戶無需擔心花費過多用在服務器的問題。

學生用戶在經過學生認證過後就可以在相關網址進行購買,網址為:https://promotion.aliyun.com/ntms/campus2017.html,
買好了服務器就可以去剛剛注冊好的賬號管理裏的管理控製台去查看服務器以及它的一些配置。

遠程控製

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

設置安全組(重要)

如果不設置好安全組,究竟是無法訪問還是編譯失敗會分不清的。

需要開放 : 80 和 443 端口

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

教程

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

設定版本變量

如果軟件版本更新後,為了方便起見,後續修改版本號隻需修改下麵的變量即可。 在 SSH終端 中輸入:

# Version
OpenSSLVersion='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 update
apt 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 /root
mkdir src
cd src

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

#下載 OpenSSL,Ubuntu 16.04(不包括)以下版本請刪除下麵的 # 以下載
#wget https://www.openssl.org/source/$OpenSSLVersion.tar.gz
#tar xzf $OpenSSLVersion.tar.gz

wget 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 提示頁了 https://pics.mf8.biz/mf8/awxi3.png![image](https://yqfile.alicdn.com/137e540c5e3ae5cf31758fbee7062cfcbac7399e.png)

最後更新:2017-10-30 11:04:39

  上一篇:go  WCF技術剖析之二十四: ServiceDebugBehavior服務行為是如何實現異常的傳播的?
  下一篇:go  WCF版的PetShop之一:PetShop簡介[提供源代碼下載]