萬盛學電腦網

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

Nginx 編譯安裝報錯 "HTTP rewrite module requires PCRE" 與 "HTTP cache module requires md5/OpenSSL" 完整解決方案

錯誤現象

./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.

根因

  • PCRE:Nginx Rewrite 模組依賴 Perl Compatible Regular Expressions 庫。
  • OpenSSL:HTTP cache、SSL、gzip 等模組依賴 OpenSSL 提供的 MD5/SHA1/加密函數。
  • CentOS/RHEL 最小化安裝**缺少開發包 (-devel)**,僅含運行時庫。

一鍵解決 (CentOS/RHEL/yum)

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:編譯工具鏈。

Debian/Ubuntu (apt)

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 (不依賴系統動態庫)

# 下載 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缺 gccyum install gcc / apt install build-essential
pcre-config not found裝了 pcre 但漏裝 pcre-develyum install pcre-devel
openssl/ssl.h: No such file or directory缺 openssl-develyum 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; 或改埠口

生產環境建議

  • 使用 **官方倉庫/第三方倉庫 (nginx.org repo, EPEL, Ondřej Surý PPA) 安裝預編譯 RPM/DEB**,自動處理依賴、安全更新、systemd 整合、logrotate、SELinux 政策。
  • 僅在**極度定製化需求** (第三方模組、特殊編譯參數、靜態鏈接 OpenSSL) 時手工編譯。
  • 編譯安裝後務必建立 **systemd service 文件**、**logrotate 配置**、**日誌目錄權限**、**配置文件版本控制 (Git)**。

安裝 pcre-devel + openssl-devel (+ zlib-devel) 即可解決 99% Nginx 編譯依賴報錯,剩下 1% 通常是版本不匹配或路徑指定錯誤。

copyright © 萬盛學電腦網 all rights reserved