Debian Apache2 网站提速实操清单
一 基础优化
# 启用压缩
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json application/x-javascript
</IfModule>
# 启用浏览器缓存
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 hour"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
</IfModule>
# 开启 KeepAlive
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
# 精简访问日志(按需)
# CustomLog ${APACHE_LOG_DIR}/access.log common
LogLevel warn
完成后执行:sudo systemctl restart apache2 使配置生效。
二 并发与 MPM 调优
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
sudo systemctl restart apache2。三 启用 HTTP/2 与 TLS 优化
sudo a2enmod http2Protocols h2 http/1.1sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"
SSLUseStapling On
SSLStaplingCache "shmcb:/var/run/stapling-cache(150000)"
SSLSessionCache "shmcb:/var/run/ssl_scache(512000)"
SSLSessionCacheTimeout 300
sudo apt install certbot python3-certbot-apache && sudo certbot --apache -d yourdomain.comsudo systemctl restart apache2。四 页面与后端加速
zend_extension = opcache.so
opcache.enable = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 4000
opcache.revalidate_freq = 60
opcache.fast_shutdown = 1
五 验证与持续监控
curl -I 查看响应头 Content-Encoding: gzip/deflate。openssl s_client -connect yourdomain:443 -servername yourdomain -status 查看 OCSP response。top/htop/dstat 观察 CPU、内存、I/O 与连接数。tail -f /var/log/apache2/{access,error}.logsudo apache2ctl configtest 校验,再 reload/restart,并保留回滚方案。