Apache2 Ubuntu 高并发处理实践
一 核心思路
二 启用并配置 MPM
apache2ctl -M | grep mpmsudo a2dismod mpm_prefork && sudo a2enmod mpm_event && sudo systemctl restart apache2<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 1000
</IfModule>
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
sudo a2dismod php7.2 && sudo a2enmod mpm_event && sudo apt-get install php7.2-fpm libapache2-mod-fcgid && sudo a2enconf php7.2-fpm && sudo systemctl restart apache2),以线程安全与资源效率。三 关键运行时参数
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 2
Timeout 60
四 静态资源减负与内容优化
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript
</IfModule>
sudo a2enmod cache cache_disk expires
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot "/var/cache/apache2/mod_cache_disk"
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
五 系统层面与监控调优
sudo sysctl -w fs.file-max=100000
echo "fs.file-max = 100000" | sudo tee -a /etc/sysctl.conf
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from localhost
ExtendedStatus On
lynx http://localhost/server-statuscurl -L http://apache2buddy.pl/ | perl(给出 MaxRequestWorkers 等建议)sudo systemctl restart apache2、压测对比(如 ab/siege)、观察 CPU/内存/连接数/队列,再迭代参数。