| 目錄 | 標準路徑 (RHEL/CentOS/Fedora) | Debian/Ubuntu 路徑 | 用途 |
|---|---|---|---|
| 配置目錄 | /etc/httpd/conf/ | /etc/apache2/ | 主配置 httpd.conf、模組、虛擬主機 |
| 文檔根目錄 | /var/www/html/ | /var/www/html/ | 預設網站檔案存放處 |
關鍵子目錄:
conf.d/ 或 sites-available/ —— 模組化配置片段modules/ —— 動態載入模組 .sologs/ 或 /var/log/httpd/ —— 存取/錯誤日誌| 模式 | 優點 | 缺點 | 適用場景 |
|---|---|---|---|
命令行編輯 httpd.conf | 彈性高、可版控、自動化部署友善 | 需熟悉指令語法、易拼錯 | 生產環境、CI/CD、資深運維 |
| 圖形化工具 (system-config-httpd / Webmin) | 低門檻、視覺化、即時語法檢查 | 依賴桌面環境、難自動化 | 初學者、桌面版測試機 |
建議:生產環境**務必掌握命令行配置**,配合 Git 版控 /etc/httpd/。
# 啟動/停止/重啟/重載配置
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
systemctl reload httpd # 平滑重載,不中斷連線
# 開機自啟
systemctl enable httpd
systemctl disable httpd
# 狀態檢查
systemctl status httpd
systemctl is-enabled httpd
# 即時追蹤日誌
journalctl -u httpd -f
tail -f /var/log/httpd/error_log
舊式 SysV 指令(舊版/CentOS 6):
service httpd start|stop|restart|reload
chkconfig httpd on
chkconfig --list httpd
httpd.conf 關鍵段落解析# 1. 基礎設定
ServerRoot "/etc/httpd"
Listen 80
ServerName www.example.com:80
DocumentRoot "/var/www/html"
# 2. MPM 模組選擇(預載入一種)
# prefork: 穩定、相容舊模組(如 mod_php)
# event/worker: 高併發、低記憶體,需 PHP-FPM
# LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
# LoadModule mpm_event_module modules/mod_mpm_event.so
# 3. 目錄權限模板
Options Indexes FollowSymLinks
AllowOverride All # 允許 .htaccess
Require all granted # 2.4+ 語法
# 4. 虛擬主機引入
IncludeOptional conf.d/*.conf
# 或 Debian 風格
IncludeOptional sites-enabled/*.conf
# 查看已載入模組
httpd -M
apachectl -M
# 安裝常用模組 (CentOS/RHEL)
yum install mod_ssl mod_rewrite mod_proxy mod_headers mod_deflate mod_security
# 啟用/停用模組 (Debian/Ubuntu)
a2enmod rewrite ssl headers proxy deflate
a2dismod autoindex
systemctl reload apache2
# /etc/httpd/conf.d/vhost-example.conf
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public
AllowOverride All
Require all granted
ErrorLog logs/example.com-error_log
CustomLog logs/example.com-access_log combined
# PHP-FPM 代理 (推薦)
SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
# HTTPS 範例
ServerName example.com
DocumentRoot /var/www/example.com/public
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/example.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/example.com.key
# ... SSL 參數優化 ...
ServerTokens Prod、ServerSignature Off 隱藏版本資訊autoindex, status, info 等Options Indexes 防止目錄瀏覽mod_security (WAF) + OWASP CRS 規則集LimitRequestBody、Timeout、MaxRequestWorkersHeader always set Strict-Transport-Security "max-age=31536000"LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined| 參數 | 建議值 | 說明 |
|---|---|---|
| MaxRequestWorkers | RAM(GB) * 100~200 | prefork 最大進程數,防 OOM |
| KeepAlive | On | 啟用持久連線 |
| KeepAliveTimeout | 2~5 | 避免佔用 worker |
| EnableSendfile | On | 核心零拷貝傳輸靜態檔 |
| EnableMMAP | Off | NFS/網路磁碟需關閉 |
| mod_deflate | 啟用 | Gzip 壓縮 text/* application/json |
| mod_expires | 啟用 | 靜態資源長期快取 |
# 語法檢查
httpd -t
apachectl configtest
# 查看編譯參數
httpd -V
# 模擬請求測試
curl -I https://localhost/
ab -n 1000 -c 10 https://localhost/ # 壓測