以下是Debian LAMP中Apache的优化策略:
选择合适MPM模块
worker
或event
模块(需禁用prefork
):sudo a2dismod prefork && sudo a2enmod worker/event && sudo systemctl restart apache2
调整核心参数
/etc/apache2/apache2.conf
,优化以下参数:
StartServers
:启动时的进程数,建议2-5个。Min/MaxSpareServers
:空闲进程数,建议5-10个。MaxRequestWorkers
(或MaxClients
):最大并发连接数,根据服务器内存调整(通常50-500)。MaxConnectionsPerChild
:单个进程处理的最大请求数,设为0(不限制)或适当值(如1000)。KeepAlive
:启用保持连接(On
),MaxKeepAliveRequests
设为100-200,KeepAliveTimeout
设为5-15秒。启用缓存模块
mod_cache
和mod_expires
缓存静态内容(如CSS/JS/图片):<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheEnable disk /static
CacheRoot "/var/cache/apache2/mod_cache_disk"
</IfModule>
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 month"
</IfModule>
优化内存与资源
MemoryLimit
(如128M
)。a2dismod
命令关闭未使用的模块(如cgi
、ldap
等)。网络与内核优化
sudo sysctl -w net.ipv4.tcp_congestion_control=bbr
/etc/sysctl.conf
):增加文件描述符限制、优化TCP缓冲区。监控与调优
htop
或apachetop
监控内存和连接数,动态调整参数。/var/log/apache2/
),排查异常请求。注:修改配置后需重启Apache生效,生产环境建议先在测试环境验证。