前几天买了阿里云主机后,配置了基本的web环境,apache性能没有做优化;导致今天在公布opms系统的时候,访问太慢,本身的云主机配置是低配,自己玩的。具体环境配置请看《再谈centOS7.2 LAMP源码安装及注意要点》。
现把apache性能优化上做一下配置:
一. deflate和expires
我在安装apache的时候,已经自动静态编译了deflate和expires模块,所以可能在配置文件里直接添加相关指令:
#deflate gzip启用 可以在主机配置文件httpd.conf或虚拟主机vhost下添加下面指令
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE image/gif image/png image/jpe image/swf image/jpeg image/bmp
#expires 缓存模块,这里配置了1天的时间,后面的单位可以months,years,weeks,hours,seconds,minutes
#可以在主机配置文件httpd.conf或虚拟主机vhost下添加下面指令
ExpiresActive on
ExpiresDefault "access plus 1 days”
ExpiresByType text/html "access plus 1 days"
ExpiresByType text/css "access plus 1 days”
ExpiresByType image/gif "access plus 1 days”
ExpiresByType image/jpeg "access plus 1 days”
ExpiresByType image/jpg "access plus 1 days”
ExpiresByType image/png "access plus 1 days”
EXpiresByType application/x-shockwave-flash "access plus 1 days"
EXpiresByType application/x-javascript "access plus 1 days"
ExpiresByType video/x-flv "access plus 1 days"
二. 隐藏服务器版本信息
打开httpd-default.conf, 同时刻记得把httpd.conf中“#”去掉
ServerTokens Prod
ServerSignature Off
三. 启用worker模式,提升并发数(可以达到2000-5000)
StartServers 3
MaxClients 2000
ServerLimit 25
MinSpareThreads 50
MaxSpareThreads 200
ThreadLimit 200
ThreadsPerChild 100
MaxRequestsPerChild 0
四. 删除掉cgi
在httpd.conf删除自带的cgi指令
五. 其它
如防盗链等
六. centos
vim /etc/sysctl.conf
net.ipv4.ip_local_port_range = 1024 65535
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_window_scaling = 0
net.ipv4.tcp_sack = 0
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_no_metrics_save=1
net.core.somaxconn = 4096
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
生效:
/sbin/sysctl -p /etc/sysctl.conf
/sbin/sysctl -w net.ipv4.route.flush=1