在 Ubuntu 上加速 Apache2,可以从**系统层面、Apache 配置、缓存、网络、PHP(如有)**几个方向来做。下面按「实用程度」给你一套方案。
sudo nano /etc/apache2/apache2.conf
确保:
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
sudo a2dismod autoindex status cgi
sudo systemctl restart apache2
查看当前 MPM:
apache2ctl -V | grep MPM
event(高并发)sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
sudo systemctl restart apache2
编辑配置:
sudo nano /etc/apache2/mods-enabled/mpm_event.conf
示例(2核4G):
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
sudo a2enmod expires headers cache cache_disk
sudo systemctl restart apache2
sudo nano /etc/apache2/conf-available/cache.conf
内容:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>
启用:
sudo a2enconf cache
sudo systemctl restart apache2
sudo a2enmod deflate
sudo nano /etc/apache2/mods-enabled/deflate.conf
确保包含:
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
重启:
sudo systemctl restart apache2
sudo apt install php-fpm
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.1-fpm # 按你的 PHP 版本
sudo systemctl restart apache2
sudo apt install php-opcache
php.ini 中:
opcache.enable=1
opcache.memory_consumption=128
opcache.revalidate_freq=60
ulimit -n 65535
Apache 很吃 I/O。
ab -n 1000 -c 100 http://your-site.com/
mpm_event + PHP-FPM如果你愿意,可以告诉我:
我可以给你一套精准配置。