How long do Apache processes stay alive?

11,681

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.

Share:
11,681
Jamie Clinton
Author by

Jamie Clinton

Updated on September 18, 2022

Comments

  • Jamie Clinton
    Jamie Clinton almost 2 years

    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?

  • Jamie Clinton
    Jamie Clinton about 13 years
    Thanks, that gives me a better understanding of Apache. My specific question is: when the response is sent back to the client, what does the child process do? Does it sit idle waiting on the client to acknowledge or can it immediately handle a new request?
  • SmallClanger
    SmallClanger about 13 years
    Once a response is sent and the client acknowledges it, the TCP connection stays open for a further (I think) 15 seconds, by default. During that time the child will be first to receive any further HTTP requests from the same client. If none are received then the TCP connection is closed and the child is dealt with according to the above rules. Typically it stays alive and goes back in to the idle pool. This behaviour can be modified by both the client and server. (It may, for instance, be preferable to close the connection immediately)
  • SmallClanger
    SmallClanger about 13 years
    Also, if you haven't used it already, wireshark.org is a great tool for analysing traffic between client and server. It'll give you huge insight into what's happening under the hood.