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

Apache HTTP Server 完整入門:歷史、安裝、模組架構、虛擬主機、效能調校

編輯:系統常見問題解答

Apache HTTP Server (httpd) 仍是全球市佔率最高的 Web 伺服器。以 Red Hat 系 (RHEL/CentOS/AlmaLinux/Rocky) 為例,完整部署指南:

一、歷史與版本選擇

  • 1995 NCSA HTTPd → 核心開發者離去 → Apache Group 成立 → 1.3 長期穩定 → 2.0 多重處理模組 (MPM) → 2.4 事件驅動、非同步 I/O、HTTP/2 原生支援。
  • 生產環境優先選 2.4.x 長期支援版,套件管理器通常提供 httpd-2.4.57-5.el9 等。

二、安裝方式比較

方式優點缺點適用場景
套件管理器 (dnf/yum/apt)依賴自動解析、安全更新一鍵、SELinux 政策整合版本較舊、編譯選項固定標準生產環境、合規要求
原始碼編譯最新版本、自訂 MPM/模組、最佳化編譯參數 (-march=native)手動管理更新、需自行處理 systemd/SELinux/Logrotate高效能需求、特殊模組、容器映像建構

三、核心設定檔架構 (RHEL 系預設路徑)

/etc/httpd/
├── conf/
│   ├── httpd.conf          # 主設定檔
│   └── magic
├── conf.d/                 # 模組/虛擬主機片段 (IncludeOptional conf.d/*.conf)
│   ├── ssl.conf
│   ├── php.conf
│   ├── vhost-*.conf
│   └── ...
├── modules/                # .so 動態模組
├── logs/ -> /var/log/httpd/
└── run/ -> /run/httpd/

四、MPM 選擇與效能調校

MPM特性適用場景關鍵參數 (httpd.conf / conf.d/mpm.conf)
prefork單執行緒多進程、相容非執行緒安全模組 (mod_php)舊版 PHP、非執行緒安全應用StartServers, MinSpareServers, MaxSpareServers, MaxRequestWorkers (≤ RAM / 單進程記憶體)
worker多執行緒多進程、較省記憶體一般動態內容、Proxy 後端ThreadsPerChild, MaxRequestWorkers, ServerLimit
event (預設 2.4+)非同步事件驅動、Keep-Alive 釋放執行緒、最高併發現代 Web、高併發、HTTP/2、反向代理ThreadsPerChild, MaxRequestWorkers, ServerLimit, AsyncRequestWorkerFactor

記憶體估算:ps -ylC httpd --sort:rss 觀察 RSS,單進程約 20~40 MB (event) / 40~80 MB (prefork+mod_php)。
MaxRequestWorkers = (Total RAM - OS/DB 保留) / 單進程 RSS。

五、虛擬主機與 SSL/TLS

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog logs/example.com-error_log
    CustomLog logs/example.com-access_log combined
    # 導向 HTTPS
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example.com
    SSLEngine on
    SSLCertificateFile /etc/pki/tls/certs/example.com.crt
    SSLCertificateKeyFile /etc/pki/tls/private/example.com.key
    SSLCertificateChainFile /etc/pki/tls/certs/ca-bundle.crt
    # TLS 1.2/1.3 only
    SSLProtocol -all +TLSv1.2 +TLSv1.3
    SSLCipherSuite HIGH:!aNULL:!MD5:!3DES
    # HSTS
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
</VirtualHost>

六、常用模組與安全強化

  • mod_security (WAF) + OWASP CRS 3.4+ 規則集
  • mod_evasive / mod_ratelimit 防暴力/DoS
  • mod_headers 設定 CSP、X-Frame-Options、Referrer-Policy
  • mod_ssl OCSP Stapling、Session Cache
  • mod_http2 啟用 HTTP/2 (需 TLS 1.2+)
  • mod_proxy + mod_proxy_fcgi 連接 PHP-FPM / Java / Go 後端

七、效能基準測試

# ab (ApacheBench)
ab -n 10000 -c 200 -k https://example.com/

# wrk (現代、支援 HTTP/2)
wrk -t4 -c200 -d30s --latency https://example.com/

# h2load (HTTP/2 專用)
h2load -n 10000 -c 200 -m 10 https://example.com/

關鍵指標:RPS、P50/P95/P99 延遲、錯誤率、CPU/記憶體/網路飽和度。調整 MPM 參數後重測,找到甜蜜點。

八、日誌管理與輪替

/etc/logrotate.d/httpd
/var/log/httpd/*log {
    daily
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
        /bin/systemctl reload httpd > /dev/null 2>&1 || true
    endscript
}

掌握上述架構,Apache 部署、調校、維運即有完整框架。

Copyright © Windows教程網 All Rights Reserved