物理环境

VPC,64C,8G,SSD

安装环境

centos7

apache2.4

php5.4

均使用YUM安装。

默认配置。

创建phpinfo

echo"<?phpphpinfo();?>">>/var/www/html/index.php

使用ab,phpinfo进行测试

出现socket错误继续进行(-r Don't exit on socket receive errors.)

使用HTTP KeepAlive功能(-k ,避免apr_pollset_poll: The timeout specified has expired (70007)错误)


使用ulimit -n增加文件打开数量

ulimit-n65535

第一次测试

ab-r-n20000-c100'http://127.0.0.1/index.php'

测试期间,新建一个会话,统计httpd进程数。

ps-ef|grephttpd|wc-l259

最多的时候是259个进程,基本符合默认配置最多256个httpd进程



查看apache2.4 mpm默认配置

cat/etc/httpd/conf.modules.d/00-mpm.conf

#SelecttheMPMmodulewhichshouldbeusedbyuncommentingexactly#oneofthefollowingLoadModulelines:#preforkMPM:Implementsanon-threaded,pre-forkingwebserver#See:http://httpd.apache.org/docs/2.4/mod/prefork.htmlLoadModulempm_prefork_modulemodules/mod_mpm_prefork.so#workerMPM:Multi-ProcessingModuleimplementingahybrid#multi-threadedmulti-processwebserver#See:http://httpd.apache.org/docs/2.4/mod/worker.html##LoadModulempm_worker_modulemodules/mod_mpm_worker.so#eventMPM:AvariantoftheworkerMPMwiththegoalofconsuming#threadsonlyforconnectionswithactiveprocessing#See:http://httpd.apache.org/docs/2.4/mod/event.html##LoadModulempm_event_modulemodules/mod_mpm_event.so

apache2.4 默认使用prefork模式,默认设置这里没有显示,需在官网查询

打开查询网址

http://httpd.apache.org/docs/2.4/mod/prefork.html

发现prefork模式有以下配置

StartServers(启动时进程数,默认5)MinSpareServers(最小空闲进程数,默认5)MaxSpareServers(最大空闲进程数,默认10)MaxRequestWorkers(最大进程数,不可大于serverlimit,默认256)MaxConnectionsPerChild(每个进程处理任务数,达到后重启进程,默认0,不重启)ServerLimit(程序最大进程数,默认256)ListenBacklog(超过MaxRequestWorkers最大服务请求,将连接进行排队,默认队列长度511)

参数调整逻辑,将以下参数

StartServersMinSpareServersMaxSpareServersMaxRequestWorkersServerLimit

设置成同一数值,避免httpd进程数出现变化,造成性能不稳定。


不重启进程,节约cpu。生产环境需设置一个较大数值,避免内存泄漏。

MaxConnectionsPerChild0

将队列调高,将来不及处理的链接都进行排队

ListenBacklog10000


ab测试的时候加上-r -k参数,避免出现小错误时候停止测试.

-n 代表总测试次数,-c 代表每秒测试次数

ab-k-r-n1000000-c512'http://127.0.0.1/index.php'

新建会话使用top(这里推荐htop)观察内存,cpu占用情况,如出现cpu或内存超过80%负荷(在保证最高负荷时的稳定性,一般不推荐超过80%负荷,这是个人经验,无测试依据)情况,同步降低所有参数。


其实一般情况下默认的参数就够用的,如果你的设备性能非常好,才需要慢慢调整配置。

测试一次,调整一次,避免负优化。