./configure --prefix=/usr/local/nginx
# 錯誤 1
./configure: error: the HTTP rewrite module requires the PCRE library.
# 安裝 pcre-devel 後再次 configure
# 錯誤 2
./configure: error: the HTTP cache module requires md5 functions from OpenSSL library.
You can either disable the module by using --without-http-cache option,
or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.
yum install -y pcre-devel openssl-devel zlib-devel gcc make
pcre-devel:提供 pcre.h、libpcre.a/.so,解決 rewrite 報錯。openssl-devel:提供 openssl/ssl.h、libssl.a/.so、libcrypto.a/.so,解決 cache/ssl 報錯。zlib-devel:gzip 壓縮模組需要 (建議同裝)。gcc make:編譯工具鏈。apt-get update && apt-get install -y libpcre3-dev libssl-dev zlib1g-dev build-essential
wget https://nginx.org/download/nginx-1.24.0.tar.gz # 換成最新穩定版
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-zlib \
--with-openssl
make -j$(nproc)
make install
# 建立軟鏈接方便調用
ln -sf /usr/local/nginx/sbin/nginx /usr/sbin/nginx
# 驗證
nginx -V 2>&1 | grep -o -E '(pcre|openssl|zlib)' | sort -u
# 下載 OpenSSL 源碼
wget https://www.openssl.org/source/openssl-3.0.12.tar.gz
tar -zxf openssl-3.0.12.tar.gz
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-openssl=../openssl-3.0.12 \
--with-openssl-opt="enable-ktls enable-ec_nistp_64_gcc_128 no-weak-ssl-ciphers no-ssl3 no-tls1 no-tls1_1"
# --with-openssl 指向源碼目錄,Nginx 會內建編譯靜態鏈接
優點:不受系統 OpenSSL 版本漏洞影響、部署單文件無依賴。缺點:編譯慢、二進制體積大、需自行跟進 OpenSSL 安全更新重編譯。
| 現象 | 原因 | 對策 |
|---|---|---|
| configure: error: C compiler cc not found | 缺 gcc | yum install gcc / apt install build-essential |
| pcre-config not found | 裝了 pcre 但漏裝 pcre-devel | yum install pcre-devel |
| openssl/ssl.h: No such file or directory | 缺 openssl-devel | yum install openssl-devel |
| ld: cannot find -lpcre | 靜態鏈接缺 .a 文件 | 確認 pcre-devel 安裝、或指定 --with-pcre=PATH |
| nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) | 80 埠被佔用 (Apache/舊 Nginx) | systemctl stop httpd; pkill nginx; 或改埠口 |
安裝 pcre-devel + openssl-devel (+ zlib-devel) 即可解決 99% Nginx 編譯依賴報錯,剩下 1% 通常是版本不匹配或路徑指定錯誤。