Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

CentOS 編譯安裝 Nginx 報錯「PCRE library」解決方案

編輯:Linux 系統常見問題解答

錯誤現象

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.

原因分析

Nginx 的 HTTP Rewrite 模組依賴 PCRE (Perl Compatible Regular Expressions) 庫進行正則表達式處理。系統缺少 PCRE 開發套件或未指定來源路徑。

解決方案:指定 PCRE 原始碼路徑編譯

1. 下載 PCRE 原始碼

wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz
tar -zxvf pcre-8.45.tar.gz
# 解壓至 /usr/local/src/pcre-8.45 或任意目錄

2. 重新配置 Nginx,指定 --with-pcre

./configure \
  --prefix=/usr/local/nginx \
  --with-pcre=/usr/local/src/pcre-8.45 \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_gzip_static_module

關鍵點:--with-pcre 參數後接的是 PCRE 原始碼目錄,而非安裝目錄。Nginx 會內建編譯 PCRE,不依賴系統庫。

3. 編譯安裝

make && make install

替代方案:安裝系統 PCRE 開發包

CentOS/RHEL

yum install -y pcre-devel
./configure --prefix=/usr/local/nginx

Debian/Ubuntu

apt-get install -y libpcre3-dev
./configure --prefix=/usr/local/nginx

驗證安裝

/usr/local/nginx/sbin/nginx -V
# 輸出應包含:--with-pcre=... 或 --with-pcre=system

常見模組依賴對照表

Nginx 模組所需庫開發包
HTTP RewritePCREpcre-devel / libpcre3-dev
HTTP SSLOpenSSLopenssl-devel / libssl-dev
HTTP Gzipzlibzlib-devel / zlib1g-dev
HTTP/2OpenSSL 1.0.2+同上

一鍵編譯腳本範例

#!/bin/bash
NGINX_VER="1.24.0"
PCRE_VER="8.45"
ZLIB_VER="1.2.13"
OPENSSL_VER="1.1.1w"

cd /usr/local/src
wget -q https://nginx.org/download/nginx-${NGINX_VER}.tar.gz
wget -q https://sourceforge.net/projects/pcre/files/pcre/${PCRE_VER}/pcre-${PCRE_VER}.tar.gz
wget -q https://zlib.net/zlib-${ZLIB_VER}.tar.gz
wget -q https://www.openssl.org/source/openssl-${OPENSSL_VER}.tar.gz

tar -xzf nginx-${NGINX_VER}.tar.gz
tar -xzf pcre-${PCRE_VER}.tar.gz
tar -xzf zlib-${ZLIB_VER}.tar.gz
tar -xzf openssl-${OPENSSL_VER}.tar.gz

cd nginx-${NGINX_VER}
./configure \
  --prefix=/usr/local/nginx \
  --with-pcre=../pcre-${PCRE_VER} \
  --with-zlib=../zlib-${ZLIB_VER} \
  --with-openssl=../openssl-${OPENSSL_VER} \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_gzip_static_module \
  --with-http_stub_status_module

make -j$(nproc) && make install
Copyright © Windows教程網 All Rights Reserved