Debian Apache 网站加载速度优化清单
一 基础与连接优化
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css \
application/javascript application/json application/x-javascript image/svg+xml
</IfModule>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Timeout 30
sudo systemctl restart apache2 使配置生效。二 并发与 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>
三 缓存与静态资源
<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 与传输层优化
# a2enmod http2
# 在虚拟主机启用:Protocols h2 http/1.1
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
五 应用层与运维实践
; php.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.validate_timestamps=0 ; 生产环境建议关闭,配合部署流程刷新
net.ipv4.tcp_tw_reuse=1、net.core.somaxconn=4096),并监控系统指标与日志,持续迭代。