How long do Apache processes stay alive?

When apache forks a process for mod-php, how long does it stay alive? Does the process die as soon as the response is sent, or will it stay alive until the browser receives the full response?


Solution 1:

If you're using mod-php, then you're likely using the prefork MPM, that spawns child processes to handle requests. The number and lifetime of these children as governed by directives in your main apache2.conf (or httpd.conf, depending on your distro) file.

Look for the part that looks like this (your values may vary):

<IfModule mpm_prefork_module>
    StartServers          5
    MinSpareServers       5
    MaxSpareServers      10
    MaxClients          150
    MaxRequestsPerChild   0
</IfModule>

Apache spawns StartServers children automatically. These processes will idle until a request comes in. If children become busy, it will spawn up to MaxClients children to handle the load, trying to maintain MinSpareServers idle children to pick up new requests. Once things calm down, idle children will be killed off until the count is down to MaxSpareServers.

The bit you're asking about is handled by MaxRequestsPerChild. Set at 0, this means that children can live forever, which is the default value in most apache installations. Set at anything else, it means each child process will be forcibly killed and restarted, regardless of current load, once it has handled that number of requests.

More details on the prefork MPM here: http://httpd.apache.org/docs/2.2/mod/prefork.html

Solution 2:

httpd doesn't fork a process for mod_php. It forks a process for itself, which has mod_php embedded in it. The child will stay alive until it has fulfilled MaxRequestsPerChild requests. mod_php itself will keep handling each request for a PHP script until either the script exits or the time limit is exceeded.