前置依賴安裝
yum install -y gcc gcc-c++ make expat-devel pcre-devel openssl-devel
1. APR (Apache Portable Runtime)
wget https://dlcdn.apache.org/apr/apr-1.7.4.tar.gz
tar -zxf apr-1.7.4.tar.gz && cd apr-1.7.4
./configure --prefix=/usr/local/apr
make && make install
2. APR-Util
wget https://dlcdn.apache.org/apr/apr-util-1.6.3.tar.gz
tar -zxf apr-util-1.6.3.tar.gz && cd apr-util-1.6.3
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
make && make install
3. PCRE (Perl Compatible Regular Expressions)
wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz
tar -zxf pcre-8.45.tar.gz && cd pcre-8.45
./configure --prefix=/usr/local/pcre
make && make install
4. Apache HTTPD 2.4 編譯安裝
wget https://dlcdn.apache.org/httpd/httpd-2.4.62.tar.gz
tar -zxf httpd-2.4.62.tar.gz && cd httpd-2.4.62
./configure \
--prefix=/usr/local/apache2 \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-pcre=/usr/local/pcre \
--enable-so \
--enable-ssl \
--enable-rewrite \
--enable-mods-shared=most
make && make install
關鍵 configure 參數說明
| 參數 | 作用 |
| --prefix | 安裝根目錄 |
| --with-apr/apr-util/pcre | 指定依賴安裝路徑 |
| --enable-so | 啟用 DSO 動態模組載入 (必須) |
| --enable-ssl | 編譯 mod_ssl (HTTPS) |
| --enable-rewrite | 編譯 mod_rewrite (URL 重寫) |
| --enable-mods-shared=most | 大多數模組編譯為共享 .so |
安裝後驗證與管理
# 檢查版本與編譯參數
/usr/local/apache2/bin/httpd -V
# 啟動/停止/重啟
/usr/local/apache2/bin/apachectl start
/usr/local/apache2/bin/apachectl stop
/usr/local/apache2/bin/apachectl restart
# 設定開機自啟 (systemd)
cat > /etc/systemd/system/httpd.service <<'EOF'
[Unit]
Description=Apache HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl start
ExecStop=/usr/local/apache2/bin/apachectl stop
ExecReload=/usr/local/apache2/bin/apachectl graceful
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable httpd
systemctl start httpd
目錄佈局概覽
/usr/local/apache2/
├── bin/ # httpd, apachectl, ab, htdigest ...
├── conf/ # httpd.conf, mime.types, extra/
├── logs/ # access_log, error_log
├── modules/ # mod_*.so
└── htdocs/ # 預設網站根目錄