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?
2 Answers
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
- 8,947
- 1
- 31
- 45
-
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? – Jamie Clinton Jun 01 '11 at 18:11
-
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 Jun 01 '11 at 22:04
-
Also, if you haven't used it already, http://www.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. – SmallClanger Jun 01 '11 at 22:11
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.
- 45,019
- 5
- 78
- 84