PHP的Apache – 处理1000个同时请求

从Stackoverflow移动的问题

我有一个处理来自客户端的请求的PHP脚本。 我应该处于处理1000个并发请求(使用apache基准)的情况。 当我现在运行300个并发请求的Apache基准testing时,我能够处理所有的请求。但是,当我超过300的标记,Apache无法处理它。 (它退出与apr_socket_recv:连接重置由同级(104))

这是Apache的错误日志。

[Thu Oct 29 11:20:34.984542 2015] [mpm_prefork:notice] [pid 6244] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.13 configured -- resuming normal operations [Thu Oct 29 11:20:34.984581 2015] [core:notice] [pid 6244] AH00094: Command line: '/usr/sbin/apache2' [Thu Oct 29 11:21:38.638943 2015] [mpm_prefork:error] [pid 6244] AH00161: server reached MaxRequestWorkers setting, consider raising the MaxRequestWorkers setting 

这是mpm preforkconfiguration –

 <IfModule mpm_prefork_module> StartServers 8 MinSpareServers 5 MaxSpareServers 20 MaxRequestWorkers 256 MaxConnectionsPerChild 4 </IfModule> 

从上面的configuration中,我可以同时处理256 * 4> 1000个请求。 我目前正在使用一个8核心机器与16GB的RAM。

我的Apache服务器可以处理1000个并发请求吗?

更多关于我的PHP代码的细节

接收到客户端请求后,我的php脚本依次启动一个python脚本。 这个python脚本,使用python多处理器并行地将50个不同的机器telnet到。 平均来说,完成一个请求需要0.5秒。

编辑

根据shodanshok.I的build议,我将mpm worker conf文件更改为:

  <IfModule mpm_prefork_module> ServerLimit 300 StartServers 50 MinSpareServers 5 MaxSpareServers 10 MaxRequestWorkers 300 MaxConnectionsPerChild 0 </IfModule> 

但基准仍然没有完成,它很早就退出了。 这就是我在apache日志上find的。

 [Thu Oct 29 12:14:42.372184 2015] [mpm_prefork:notice] [pid 29261] AH00169: caught SIGTERM, shutting down [Thu Oct 29 12:14:42.913499 2015] [mpm_prefork:notice] [pid 1397] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.13 configured -- resuming normal operations [Thu Oct 29 12:14:42.913539 2015] [core:notice] [pid 1397] AH00094: Command line: '/usr/sbin/apache2' 

你误解了MaxRequestWorkersMaxConnectionsPerChild做了什么。 从手册页 :

MaxRequestWorkers指令设置将要服务的同时请求数量的限制。 通过MaxRequestWorkers限制的任何连接尝试通常都会排队,直到基于ListenBacklog指令的数字。 一旦subprocess在不同的请求结束时被释放,连接将被服务。

对于非线程服务器(例如,prefork),MaxRequestWorkers将转换为将被启动以处理请求的subprocess的最大数量。 默认值是256; 要增加它,你还必须提高ServerLimit。

对于线程和混合服务器(例如事件或工作者),MaxRequestWorkers限制可用于服务客户端的线程总数。 对于混合MPM,缺省值是16(ServerLimit)乘以25(ThreadsPerChild)的值。 因此,要将MaxRequestWorkers增加到需要超过16个进程的值,还必须提高ServerLimit。

MaxConnectionsPerChild指令设置单个子服务器进程将处理的连接数的限制。 在MaxConnectionsPerChild连接之后,subprocess将会死亡。 如果MaxConnectionsPerChild为0,则该过程将永不过期。

总之,在prefork模式下, MaxRequestWorkers是允许的并发连接总数MaxConnectionsPerChild只是在这么多的连接之后才告诉孩子死亡。

换句话说:提高您的MaxRequestWorkers设置将解决您的问题。