Debian 上 Apache2 优化用户体验的实操清单
一 基础连接与传输优化
# 启用模块
a2enmod deflate http2
# /etc/apache2/apache2.conf 或 ports.conf 的 <IfModule mpm_*> 之外
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Timeout 30
# 虚拟主机启用 HTTP/2
<VirtualHost *:443>
Protocols h2 http/1.1
# ... 其它 SSL 与站点配置
</VirtualHost>
验证要点:响应头出现 Content-Encoding: gzip;开发者工具 Network 显示 h2;连接为 keep-alive。二 MPM 并发与内存调优
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
a2dismod mpm_prefork && a2enmod mpm_event && systemctl restart apache2)。三 缓存策略与静态资源
<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>
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/mod_cache_disk
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
四 HTTPS 与传输层优化
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"
SSLUseStapling On
SSLStaplingCache "shmcb:/var/run/stapling-cache(150000)"
SSLSessionCache "shmcb:/var/run/ssl_scache(512000)"
SSLSessionCacheTimeout 300
SSLCompression off
# 优先 ECDHE 套件与 TLS 1.2/1.3
验证要点:访问站点显示锁标;TLS 版本为 1.2/1.3;响应头出现 staple-ocsp。五 应用层、运维与验证
; /etc/php/*/apache2/php.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.validate_timestamps=0 ; 生产
# 安装与查看
sudo apt install goaccess
goaccess /var/log/apache2/access.log --log-format=COMBINED
net.ipv4.tcp_tw_reuse=1、net.core.somaxconn=4096);每次变更先在测试环境验证,再灰度上线并回看监控与日志。