Nginx的編譯及部署
阿新 • • 發佈:2020-12-12
技術標籤:nginxCentos7nginxlinuxcentos
Nginx是一款優秀的開源軟體,支援在多平臺執行. Centos擁有良好的系統結構和工具軟體生態系統, 是一款基於Linux的非常流行的發行版本.我們選擇將他作為本書的作業系統環境
- 編譯環境準備
1.1. 作業系統準備
(1) 系統服務安裝
Centos可用最小化安裝, 安裝完畢之後, 用如下命令補充工具
yum -y install epel-release #安裝擴充套件工具包yum源
yum install net-cools wget nscd lsof #安裝工具
(2)DNS快取
編輯/etc/resolv.conf配置DNS伺服器, 開啟NSCD服務, 快取CDN,提高域名解析響應速度
systemctl start ncsd.service #啟動NSCD服務
systemctl enable nscd.service
(3)修改檔案開啟數量限制
作業系統預設單程序最大開啟檔案數目為1024, 要想實現高併發, 可以把單程序的檔案開啟數調整為65536
echo "* soft nofile 65536 #*表示所用使用者
\* hard nofile 65536" >> /etc/security/limits.conf
- Nginx原始碼編譯
2.1. Nginx原始碼獲取
mkdir -p /opt/data/source
cd /opt/data/source
wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
2.2. 編譯配置引數
編譯Nginx原始碼檔案時,首先需要通過編譯配置命令configure進行編譯配置.可以通過./configure --help
獲得更多的編譯配置引數
2.3 程式碼編譯
安裝編譯工具以及依賴庫,指令碼如下:
yum -y install gcc pcre-devel zlib-devel openssl-devel libxml2-devel \ libxslt-devel gd-devel GeoIP-devel jemalloc-devel libatomic_ops-devel \ perl-devel perl-ExtUtils-Embed
編譯所有功能模組,指令碼如下
#執行編譯配置
./configure
#程式碼編譯及安裝
make&make install
可以根據需求靈活調整
2.4.新增第三方模組
Nginx的功能是以模組方式存在的,同時支援新增第三方開發的功能模組. 在執行configure時,通過–add-module=PATH引數指定第三方模組的程式碼路徑,在make時就可以進行同步編譯了
新增第三方靜態模組的方法如下:
./configure --add-module=../ngx_http_proxy_connect_module
新增第三方動態模組
./configure --add-dynamic-module=../ngx_http_proxy_connect_module \
--wit-compat
- Nginx部署
3.1. 環境配置
Nginx編譯成功之後,為了便於操作維護, 建議把nginx執行檔案的路徑新增到環境變數中, 可以通過一下命令來完成
cat > /etc/profile.d/nginx.sh<<EOF
PATH=$PATH:/usr/local/nginx/sbin
EOF
source /etc/profile
在Centos作業系統中, 配置檔案通常放在/etc目錄下, 建議將Nginx的conf目錄軟連線到/etc目錄下
ln -s /usr/local/nginx/conf /etc/nginx
3.2. 命令列引數
Nginx執行檔案的命令列引數可以通過-h來獲取,Nginx命令列引數如下:
nginx -h
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
上述程式碼的具體引數解釋說明如下:
- v引數: 顯示Nginx執行檔案的版本資訊
- V引數: 顯示Nginx執行檔案的版本資訊和編譯配置引數
- t引數: 進行配置檔案語法檢查,測試配置檔案的有效性
- T引數: 進行配置檔案語法檢查,測試配置檔案有效性.同時輸出所有有效配置內容
- q引數: 在測試配置檔案有效性,同時輸出所有有效配置內容
- s引數: 傳送訊號給Nginx主程序,訊號可以是一下四個:
stop: 快速關閉
quit: 正常關閉
reopen: 重新開啟日誌檔案
reload: 重新載入配置檔案,啟動一個載入新配置檔案的Woker Process,正常關閉一個載入舊配置檔案的Worker Process - p引數: 指定Nginx的執行目錄, 預設為configure時的安裝目錄, 通常為/usr/local/nginx
- c引數: 指定nginx.conf檔案的位置, 預設為conf/nginx.conf
- g引數: 外部指定配置檔案的全域性指令
3.3. 註冊系統服務
Centos系統環境中使用systemd進行系統和服務管理, 可以按需守護程序,並通過systemctl 命令進行systemd的監測和控制.為了方便Nginx應用程序的維護和管理, 此處把Nginx註冊成系統服務, 由systemd進行服務管理
cat >/usr/lib/systemd/system/nginx.conf<<EOF
[Unit] #記錄service檔案的通用資訊
Description=The Nginx HTTP and reverse proxy server #Nginx服務描述資訊
After=network.target remote-fs.target nss-lookup.target # Nginx服務啟動依賴, 在指定服務之後啟動
[Service] #記錄service檔案的service資訊
Type=forking #標準UNIX Daemon使用的啟動方式
PIDFile=logs/nginx.pid # Nginx服務的pid檔案位置
ExecStartPre=/usr/bin/rm -f logs/nginx.pid #Nginx服務啟動前,刪除舊的pid檔案
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q #Nginx服務啟動前執行配置檔案檢測
ExecStart=/usr/local/nginx/sbin/nginx -g "pid logs/nginx.pid;" #啟動Nginx服務
ExecReload=/usr/local/nginx/sbin/nginx -t -q #Nginx服務重啟前執行配置檔案監測
ExecReload=/usr/local/nginx/sbin/nginx -s reload -g "pid logs/nginx.pid;" #重啟Nginx服務
ExecStop=/bin/kill -s HUP $MAINPID #關閉Nginx服務
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install] #記錄servie檔案的安裝資訊
WantedBy=multi-user.target #多使用者環境下啟動
EOF
systemctl enable nginx #開機啟動
systemctl start nginx #啟動服務
systemctl stop nginx #暫停服務
systemctl reload nginx #重啟服務
systemctl status nginx #檢視服務狀態