萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Linux 從源碼編譯安裝 Nginx 完整實戰指南 (依賴解決、模組選擇、熱升級)

源碼編譯安裝 Nginx 可精確控制模組、優化編譯參數、支援最新特性 (如 HTTP/3、動態模組),是生產環境定製化部署的首選。本文記錄從依賴安裝、配置參數、編譯安裝、環境變數配置到平滑升級的完整流程。

前置依賴安裝 (CentOS/RHEL 為例)

# 核心建構工具
yum groupinstall "Development Tools" -y

# Nginx 必需庫
yum install -y pcre-devel zlib-devel openssl-devel

# 可選模組依賴
# HTTP/2 (ngx_http_v2_module) 需 OpenSSL 1.0.2+ (支援 ALPN)
# HTTP/3 (QUIC) 需啟用 --with-http_v3_module --with-http_ssl_module --with-openssl=../openssl-1.1.1 (需編譯 OpenSSL)
# GeoIP
yum install -y geoip-devel
# Image filter
yum install -y gd-devel
# Perl 相容正則 (PCRE) 已含於 pcre-devel
# 可選:Libatomic (ARM/某些架構需要)
yum install -y libatomic_ops-devel

下載與驗證源碼

cd /usr/local/src
NGINX_VER=1.24.0
wget https://nginx.org/download/nginx-${NGINX_VER}.tar.gz
wget https://nginx.org/download/nginx-${NGINX_VER}.tar.gz.asc
# 驗證 GPG 簽名 (推薦)
gpg --keyserver keyserver.ubuntu.com --recv-keys 0xA1C052F8  # Maxim Dounin
gpg --verify nginx-${NGINX_VER}.tar.gz.asc nginx-${NGINX_VER}.tar.gz

tar -xzf nginx-${NGINX_VER}.tar.gz
cd nginx-${NGINX_VER}

配置編譯參數 (生產環境推薦配置)

./configure \
--prefix=/usr/local/nginx \
--user=www --group=www \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module=dynamic \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-threads \
--with-file-aio \
--with-pcre-jit \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -fPIC'

關鍵模組說明

  • --with-http_v2_module:HTTP/2 支援 (需 OpenSSL 1.0.2+)。
  • --with-http_ssl_module:HTTPS 支援。
  • --with-http_stub_status_module:狀態頁監控。
  • --with-threads:多執行緒 I/O (讀取/發送檔案)。
  • --with-file-aio:非同步 I/O (Linux aio)。
  • --with-pcre-jit:PCRE JIT 編譯加速正則。
  • =dynamic:編譯為動態模組 (.so),啟動時按需載入,減少核心體積。

編譯與安裝

# 編譯 (利用所有 CPU 核心)
make -j$(nproc)

# 安裝
make install

# 驗證安裝
/usr/local/nginx/sbin/nginx -V

環境變數配置 (全局可用 nginx 命令)

cat > /etc/profile.d/nginx.sh <<'EOF'
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
EOF
source /etc/profile.d/nginx.sh

# 驗證
nginx -v
# nginx version: nginx/1.24.0

系統服務註冊

Systemd 服務單元 (/usr/lib/systemd/system/nginx.service)

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
systemctl status nginx

平滑升級 (零停機) 標準流程

#!/bin/bash
# upgrade_nginx.sh
NEW_VER=1.25.1
cd /usr/local/src
wget https://nginx.org/download/nginx-${NEW_VER}.tar.gz
tar -xzf nginx-${NEW_VER}.tar.gz
cd nginx-${NEW_VER}

# 使用原有配置參數 (從舊版 nginx -V 獲取)
./configure $(/usr/local/nginx/sbin/nginx -V 2>&1 | grep 'configure arguments:' | sed 's/.*configure arguments: //')
make -j$(nproc)

# 熱替換
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak.$(date +%F)
cp objs/nginx /usr/local/nginx/sbin/nginx

# 語法檢查
/usr/local/nginx/sbin/nginx -t

# 平滑升級信號序列
kill -USR2 $(cat /usr/local/nginx/logs/nginx.pid)  # 啟動新 Master
sleep 3
kill -WINCH $(cat /usr/local/nginx/logs/nginx.pid.oldbin)  # 關閉舊 Worker
sleep 2
kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid.oldbin)   # 關閉舊 Master

echo "升級完成,當前版本: $(/usr/local/nginx/sbin/nginx -v 2>&1)"

常見編譯錯誤排查

錯誤訊息原因解決方案
C compiler cc is not found缺少 gccyum install gcc
PCRE library not found缺少 pcre-develyum install pcre-devel
zlib library not found缺少 zlib-develyum install zlib-devel
OpenSSL library not found缺少 openssl-develyum install openssl-devel
undefined reference to `__sync_add_and_fetch_4'32位系統原子操作缺失加入 --with-cc-opt='-march=i686 -pthread'
SSL modules require OpenSSL 1.0.2+系統 OpenSSL 過舊編譯安裝新版 OpenSSL 並指定 --with-openssl=../openssl-1.1.1
copyright © 萬盛學電腦網 all rights reserved