以下是优化CentOS Apache2网站加载速度的关键方法:
启用KeepAlive
减少连接建立开销,在/etc/httpd/conf/httpd.conf
中添加:
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
调整MPM模块
根据服务器资源修改/etc/httpd/conf.modules.d/00-mpm.conf
(如prefork示例):
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
配置静态文件缓存
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
</IfModule>
启用压缩技术
mod_deflate
减少传输数据量。<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
优化内核参数
调整/etc/sysctl.conf
,如增加TCP连接数、优化缓冲区:
net.ipv4.tcp_max_syn_backlog = 4096
vm.swappiness = 10
禁用无用模块
通过a2dismod
命令禁用非必要模块(如mod_rewrite
若未使用),减少资源占用。
使用高性能硬件和文件系统
noatime
选项减少磁盘访问。监控与调优
使用top
、htop
等工具监控资源使用,根据负载调整配置参数。
注意:修改配置后需重启Apache服务生效:
sudo systemctl restart httpd