When Apache uses mod_php with the prefork MPM, each HTTP request spawns a separate child process. These processes don't terminate immediately after sending the response - they remain alive according to Apache's configuration parameters.
# Typical Apache prefork configuration
<IfModule prefork.c>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 10000
</IfModule>
Several configuration directives determine how long processes stay alive:
- MaxRequestsPerChild: Maximum requests a process handles before terminating
- KeepAlive: Whether TCP connection persists for multiple requests
- KeepAliveTimeout: How long to wait for subsequent requests
Consider this scenario with default settings:
// PHP script that runs for 1 second
<?php
sleep(1);
echo "Done";
?>
The process will:
- Start handling the request
- Execute the PHP script (1 second)
- Send response headers
- Stream response body
- Remain alive for future requests until MaxRequestsPerChild is reached
Long-running processes can:
- Consume memory between requests
- Maintain PHP opcache contents
- Potentially leak resources if scripts aren't clean
For high-traffic sites, consider these tuning parameters:
# Optimized configuration for busy PHP applications
MaxRequestsPerChild 500
KeepAlive On
KeepAliveTimeout 2
This balances process reuse with memory management, preventing gradual memory leaks while maintaining performance benefits of process reuse.
In a typical Apache prefork MPM setup with mod_php, child processes are created to handle incoming HTTP requests. The lifespan of these processes is governed by several configuration directives and environmental factors.
Apache child processes handling mod_php requests do not terminate immediately after sending the response. The complete sequence is:
- PHP finishes executing the script
- Apache sends all response data to the client
- The process remains alive until the TCP connection is fully closed
- The process returns to the Apache process pool for reuse
These Apache directives control process behavior:
# In httpd.conf
MaxRequestsPerChild 10000 # Process dies after serving this many requests
KeepAliveTimeout 5 # Seconds to wait for subsequent requests
Timeout 300 # Maximum time to receive the entire request
Consider this PHP script that demonstrates the behavior:
<?php
// Slow response example
header('Content-Type: text/plain');
echo "Starting response\n";
for ($i = 1; $i <= 5; $i++) {
echo "Data chunk $i\n";
ob_flush();
flush();
sleep(1);
}
echo "End of response\n";
You can verify process behavior using these Linux commands:
# Monitor Apache processes
watch -n 1 "ps aux | grep 'apache2\|httpd' | grep -v grep"
# Check established connections
netstat -tulpn | grep httpd
Long-running processes impact server resources. Optimize by:
- Tuning MaxRequestsPerChild based on memory leaks
- Setting appropriate KeepAliveTimeout
- Using opcode caching (OPcache) for PHP
- Implementing process health checks