首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Apache HTTP Server 2.4.4 源碼編譯安裝完整指南:APR、APR-Util、PCRE 依賴處理

     編輯:PHP 綜合知識

版本與依賴說明

Apache 2.4 系列強制要求外部 APR (Apache Portable Runtime) 1.4+、APR-Util 1.5+、PCRE 8.10+。編譯安裝須先處理這三個依賴庫。

準備工作

  • 源碼包:httpd-2.4.4.tar.bz2、apr-1.4.6.tar.gz、apr-util-1.5.1.tar.gz
  • 系統套件:yum install gcc gcc-c++ make pcre-devel openssl-devel expat-devel -y
  • 工作目錄:/usr/local/src

步驟一:安裝 APR (Apache Portable Runtime)


cd /usr/local/src
tar zxvf apr-1.4.6.tar.gz
cd apr-1.4.6
./configure --prefix=/usr/local/apr
make && make install

步驟二:安裝 APR-Util


cd /usr/local/src
tar zxvf apr-util-1.5.1.tar.gz
cd apr-util-1.5.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
make && make install

關鍵:必須指定 --with-apr=/usr/local/apr/bin/apr-1-config,否則無法找到 APR。

步驟三:安裝 PCRE (若系統版本過舊)


# 檢查系統 PCRE 版本
pcre-config --version
# 若 < 8.10,需源碼升級
cd /usr/local/src
tar zxvf pcre-8.xx.tar.gz
cd pcre-8.xx
./configure --prefix=/usr/local/pcre
make && make install

或直接使用系統套件:yum install pcre-devel -y (RHEL 6+ 通常滿足版本要求)。

步驟四:配置動態庫路徑 (關鍵步驟)

APR/APR-Util/PCRE 安裝至非標準路徑 (/usr/local/apr/lib 等),運行時動態連結器需知曉路徑:


echo "/usr/local/apr/lib" > /etc/ld.so.conf.d/apr.conf
echo "/usr/local/apr-util/lib" >> /etc/ld.so.conf.d/apr.conf
echo "/usr/local/pcre/lib" >> /etc/ld.so.conf.d/apr.conf
ldconfig

或編譯 Apache 時指定 LDFLAGS="-L/usr/local/apr/lib -L/usr/local/apr-util/lib -L/usr/local/pcre/lib" 並設置 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre。

步驟五:編譯安裝 Apache 2.4.4


cd /usr/local/src
tar jxvf httpd-2.4.4.tar.bz2
cd httpd-2.4.4

./configure \
  --prefix=/usr/local/apache24 \
  --enable-so \
  --enable-rewrite \
  --enable-ssl \
  --enable-modules=most \
  --enable-mods-shared=all \
  --with-mpm=event \
  --with-apr=/usr/local/apr \
  --with-apr-util=/usr/local/apr-util \
  --with-pcre=/usr/local/pcre \
  --enable-http2 \
  --with-nghttp2=/usr/local  # 若需 HTTP/2 支援

make && make install

MPM 選擇說明:

  • prefork:傳統多進程,相容非執行緒安全模組 (如舊版 mod_php),效能較低。
  • worker:多進程多執行緒,效能較好,需模組執行緒安全。
  • event (推薦):worker 改良版,專為 Keep-Alive 長連線優化,2.4 預設 MPM。

步驟六:配置系統服務 (systemd)


cat > /etc/systemd/system/httpd24.service <<'EOF'
[Unit]
Description=Apache HTTP Server 2.4
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/apache24/logs/httpd.pid
ExecStart=/usr/local/apache24/bin/apachectl start
ExecReload=/usr/local/apache24/bin/apachectl graceful
ExecStop=/usr/local/apache24/bin/apachectl stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable httpd24
systemctl start httpd24

步驟七:驗證與基礎配置

  1. 瀏覽器訪問 https://IP 顯示 "It works!"。
  2. 檢查模組載入:/usr/local/apache24/bin/httpd -M。
  3. 編輯 /usr/local/apache24/conf/httpd.conf 關鍵設定:

ServerName localhost:80
Listen 80
User apache
Group apache

# 安全設定
ServerTokens Prod
ServerSignature Off
TraceEnable Off
FileETag None

# 效能設定
EnableMMAP Off
EnableSendfile On
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

# 日誌格式
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog logs/access_log combined
ErrorLog logs/error_log
LogLevel warn

常見編譯錯誤速查

錯誤原因解決
apr_version.h not foundAPR 未安裝或路徑錯誤確認 --with-apr 指向正確前綴,apr-1-config 可執行
pcre_version.h not foundPCRE 開發包缺失yum install pcre-devel 或源碼安裝指定 --with-pcre
undefined reference to apr_*連結時找不到 APR 庫ldconfig 更新庫緩存,LD_LIBRARY_PATH 包含 apr/lib
SSL 支援缺失openssl-devel 未安裝yum install openssl-devel
HTTP/2 編譯失敗nghttp2 庫缺失yum install libnghttp2-devel 或源碼安裝 nghttp2

升級與維護建議

  • 關注 Apache 官網安全公告,及時編譯升級至最新 2.4.x 版本。
  • 編譯參數保持一致,僅替換源碼包重新 make && make install,配置檔不變。
  • 定期備份 /usr/local/apache24/conf、/usr/local/apache24/logs。
  • 監控 error_log、access_log,配合 GoAccess/AWStats 分析。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved