Search code examples
phpapachenonblockinghttp-request

How to configure PHP or Apache for non blocking http requests


I have apache as http server and php, i sent about 5 simultaneous requests from browser to this php script which has a for loop, which takes long time to complete, I saw all the concurrent requests are blocked and served sequentially.

How to configure apache or php for non blocking behavior of http request serving instead of sequentially serving behavior?

    I know that Tomcat server http connector can tune using following server parameters

    – Threads (maxThreads)
    – Keep alive requests (maxKeepAliveRequests)
    – TCP Backlog (acceptCount)
    – connectionTimeout
    – Socket buffers
    - Use different connectors (nio, apr, bio)

etc...

the php code snippet which was tested for 5 concurrent requests, but served sequentially by the web server (apache 2.2, php 5.3)

<?php
for ($i = 1; $i < 500000; $i++) { //do some processing which takes some time
    $sq = sqrt($i); 
    $val += $sq;
}
echo $val;
?>

Solution

  • Use following directives in apache.conf

    StartServers          3
    MinSpareServers       5
    MaxSpareServers       7
    

    It will spawn additional processes waiting for concurrent connections to serve them instantly. Adjust the numbers per your needs.