主伺服器設定為預設值,虛擬主機可個別覆蓋。部分指令 (如 User/Group) 在虛擬主機中必須重新定義。
Port 80
inetd/xinetd 模式由 inetd.conf 定義埠| 場景 | 埠號 | 權限需求 | 說明 |
|---|---|---|---|
| Root 啟動 → 切換用戶 | 80 (或 <1024) | Root 權限啟動 | 標準部署,Apache 自動 setuid 降權 |
| 普通用戶啟動 | >1024 (如 8080, 8000) | 無需 Root | 共享主機、開發環境、無 Root 權限 |
安全觀點: Root 開啟 80 埠後立即降權為 nobody/nogroup,風險極低。普通用戶無法綁定 <1024 埠。
User nobody
Group nogroup
setuid()/setgid() 切換身份www-data/www-data (Debian/Ubuntu)、apache/apache (RHEL/CentOS)/sbin/nologin)、無家目錄、最小權限ServerAdmin webmaster@example.com
webmaster@domain,配合郵件別名轉發給真人alerts@ops.example.com)ServerRoot "/etc/httpd"
PidFile run/httpd.pid
Timeout 60
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
# Prefork MPM
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 4000
</IfModule>
# Worker MPM
<IfModule worker.c>
StartServers 4
MaxClients 300
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
User www-data
Group www-data
ServerAdmin webmaster@example.com
ServerName www.example.com:80
UseCanonicalName On
| MPM | 架構 | 適用場景 | 記憶體 | 並發 |
|---|---|---|---|---|
| prefork | 多進程、無執行緒 | 相容非執行緒安全模組 (舊 PHP)、穩定性優先 | 高 | 中 |
| worker | 多進程+多執行緒 | 高並發、記憶體效率好 | 中 | 高 |
| event | Worker 改良、非阻塞 I/O | 極高並發、長連線 (KeepAlive、WebSocket) | 低 | 極高 |
PHP 7+ 配合 php-fpm (FastCGI) 通常使用 event MPM 效能最佳。
ServerTokens Prod, ServerSignature OffLoadModuleLimitRequestBody 10485760 (10MB)Header always append X-Frame-Options SAMEORIGINHeader set X-Content-Type-Options nosniff