在CentOS上优化Apache2服务器的性能是一个多方面的过程,涉及配置调整、模块启用、硬件优化等多个层面。以下是一些关键的优化步骤和建议:
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
prefork
。你可以编辑Apache配置文件中的MPM配置:<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 2000
</IfModule>
mod_expires
模块来实现静态文件缓存。在Apache配置文件中添加以下行:<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 30 days"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType application/javascript "access plus 30 days"
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>
CustomLog /var/log/apache2/access_log common
ErrorLog /var/log/apache2/error.log
net.ipv4.tcp_tw_reuse 1
net.ipv4.tcp_tw_recycle 1
net.ipv4.tcp_fin_timeout 30
net.ipv4.tcp_keepalive_time 1200
net.ipv4.ip_local_port_range "1024 65535"
net.ipv4.tcp_max_syn_backlog 8192
net.core.somaxconn 1024
net.core.netdev_max_backlog 2000
net.ipv4.tcp_max_orphans 32768
net.ipv4.tcp_syncookies 1
mod_cache
和mod_disk_cache
,将热点数据缓存到本地磁盘,减少对后端服务的访问,提高响应速度。mod_deflate
和mod_gzip
,对静态资源进行压缩,减少数据传输量,提高响应速度。sysstat
、sar
、top
等,帮助监控系统性能,并及时发现并解决性能问题。在进行任何优化操作之前,请确保备份重要数据,并在测试环境中验证优化效果。