Apache 2.4 將所有配置集中於 httpd.conf (或 apache2.conf),採用模組化設計。理解核心指令與 MPM 選擇是效能調優關鍵。
# 全域環境
ServerRoot "/usr/local/apache2"
ServerAdmin admin@example.com
ServerName www.example.com:80
Listen 80
Listen 443
# MPM 模組 (僅能啟用一種)
# LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
# LoadModule mpm_worker_module modules/mod_mpm_worker.so
LoadModule mpm_event_module modules/mod_mpm_event.so # 推薦 2.4+
# 模組載入 (動態共享物件 DSO)
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so
LoadModule http2_module modules/mod_http2.so
# 主伺服器配置
User www-data
Group www-data
DocumentRoot "/var/www/html"
AllowOverride None
Require all denied
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
# 虛擬主機
IncludeOptional sites-enabled/*.conf
| MPM | 架構 | 適用場景 | 關鍵參數 |
|---|---|---|---|
| prefork | 多進程、單執行緒 | 非執行緒安全模組 (舊 PHP mod_php) | StartServers, Min/MaxSpareServers, MaxRequestWorkers |
| worker | 多進程、多執行緒 | 高併發、執行緒安全應用 | ThreadsPerChild, MaxRequestWorkers |
| event | 多進程、多執行緒 + 非同步事件 | 現代 Web、Keep-Alive 密集、HTTP/2 | AsyncRequestWorkerFactor, ThreadsPerChild |
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 400
MaxConnectionsPerChild 10000
AsyncRequestWorkerFactor 2
ServerLimit 16
KeepAlive On + MaxKeepAliveRequests 100 + KeepAliveTimeout 5EnableSendfile On、EnableMMAP On (NFS 掛載需關閉)HostnameLookups Off (避免 DNS 反查延遲)DirectoryIndex index.html index.phpFileETag MTime Size (叢集需一致)
# GZIP 壓縮
AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json application/xml
DeflateCompressionLevel 6
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|woff2?)$ no-gzip
# 瀏覽器快取
ExpiresActive On
ExpiresByType image/* "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
# 安全標頭
Header always set X-Content-Type-Options nosniff
Header always set X-Frame-Options SAMEORIGIN
Header always set Referrer-Policy strict-origin-when-cross-origin
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';"
ServerName www.example.com
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/private/example.com.key
SSLCertificateChainFile /etc/ssl/certs/ca-bundle.crt
# Mozilla Intermediate 相容配置
SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384
SSLHonorCipherOrder off
SSLSessionTickets off
SSLUseStapling on
SSLStaplingCache "shmcb:logs/ssl_stapling(32768)"
# HSTS
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
ServerTokens Prod、ServerSignature OffOptions -IndexesLimitRequestBody 10485760 (10MB)SecRuleEngine On Require all denied
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D" combinedio
CustomLog logs/access_log combinedio
ErrorLog logs/error_log
LogLevel warn
# 慢查詢日誌 (需 mod_log_config)
LogFormat "%h %l %u %t \"%r\" %>s %b %D" slow
SetEnvIf Request_URI "^/api/slow" slowlog
CustomLog logs/slow_log slow env=slowlog
掌握 httpd.conf 結構、MPM 參數計算、壓縮快取策略、TLS 現代配置與安全標頭,即可構建高效能、高安全性的 Apache 生產環境。