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.gzyum install gcc gcc-c++ make pcre-devel openssl-devel expat-devel -y/usr/local/src
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
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-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。
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。
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
https://IP 顯示 "It works!"。/usr/local/apache24/bin/httpd -M。/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 found | APR 未安裝或路徑錯誤 | 確認 --with-apr 指向正確前綴,apr-1-config 可執行 |
| pcre_version.h not found | PCRE 開發包缺失 | 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 |
make && make install,配置檔不變。/usr/local/apache24/conf、/usr/local/apache24/logs。error_log、access_log,配合 GoAccess/AWStats 分析。