編譯時指定:./configure --with-mpm=prefork|worker|event
查看當前:httpd -l | grep mpm
<IfModule mpm_prefork_module>
ServerLimit 10000 # 突破 256 硬性限制,必須在 MaxRequestWorkers 前
StartServers 5 # 啟動初始進程數
MinSpareServers 5 # 最小閒置進程
MaxSpareServers 10 # 最大閒置進程
MaxRequestWorkers 10000 # 最大並發進程數 (舊名 MaxClients)
MaxConnectionsPerChild 10000 # 進程生命周期請求數,防記憶體洩漏
</IfModule>
MinSpareServers 閒置進程MaxSpareServers 時殺掉多餘進程ps -ylC httpd --sort:rss | awk '{sum+=$8} END {print sum/NR/1024 " MB"}'MaxRequestWorkers = 可用記憶體 / 單進程記憶體ServerLimit = MaxRequestWorkers<IfModule mpm_worker_module>
ServerLimit 25 # 最大子進程數 (預設 16)
ThreadLimit 200 # 每進程最大線程數 (預設 64)
StartServers 3
MaxRequestWorkers 2000 # 總最大並發 = ServerLimit × ThreadsPerChild
MinSpareThreads 50
MaxSpareThreads 200
ThreadsPerChild 100 # 每進程固定線程數
MaxConnectionsPerChild 10000
</IfModule>
MaxRequestWorkers ≤ ServerLimit × ThreadsPerChildMaxRequestWorkers 必須是 ThreadsPerChild 整數倍ThreadLimit 突破 64 限制 (需重編譯或 2.4+ 動態載入)參數同 Worker,但引入 Listener Thread 專責 Keep-Alive 連線管理,釋放 Worker Thread 處理新請求。
強烈推薦 Apache 2.4+ 生產環境使用 Event MPM。
<IfModule mpm_winnt_module>
ThreadsPerChild 500 # 單進程線程數 = 最大並發
MaxConnectionsPerChild 10000
ThreadStackSize 8388608 # 8MB 堆疊,防 PHP 深度遞迴棧溢位
</IfModule>
| MPM/配置 | 並發 | CPU% | 記憶體(MB) | Req/sec |
|---|---|---|---|---|
| Prefork (MaxClients=150) | 100 | 28.8 | 285 | 8434 |
| Prefork (MaxClients=500) | 500 | 24.9 | 417 | 7229 |
| Prefork (MaxClients=1000) | 1000 | 22.5 | 470 | 6570 |
| Worker (ThreadsPerChild=200) | 500 | 19.8 | 209 | 5708 |
| Worker (ThreadsPerChild=100) | 500 | 24.6 | 254 | 6827 |
| Worker (ThreadsPerChild=50) | 500 | 26.0 | 320 | 7141 |
# 連線佇列
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
# TIME_WAIT 快速回收 (高併發短連線必調)
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1 # 注意:NAT 環境慎用
net.ipv4.tcp_fin_timeout = 10
# 緩衝區
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# 其他
net.ipv4.tcp_max_tw_buckets = 200000
net.ipv4.tcp_slow_start_after_idle = 0
# 套用
sysctl -p
# Prefork
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 10000
</IfModule>
# Worker
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 10000
</IfModule>
# Event (推薦)
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 10000
</IfModule>
# WinNT
<IfModule mpm_winnt_module>
ThreadsPerChild 150
MaxConnectionsPerChild 10000
</IfModule>
MaxRequestWorkers > 256 **必須** 設 ServerLimitThreadsPerChild > 64 **必須** 設 ThreadLimita2dismod mpm_prefork && a2enmod mpm_eventab (簡易)、wrk (高效能)、jmeter (功能完整)