httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Montague <>
Subject Re: [users@httpd] Apache forking a forked child process -- is that possible?
Date Fri, 27 Jan 2012 14:29:15 GMT
On January 27, 2012 3:47 , =?UTF-8?Q?K=C4=81rlis_Repsons?= 
<> wrote:
> CGI? If I'm having a PHP extension forked along with an apache child 
> process, that extension has initially not had a database connection, 
> but it's established when processing requests,,, that connection can 
> get forked along with a child process? (In other words and repeating 
> myself a bit: apache forks it's child processes after they have 
> already been processing requests?) Note that I'm considering prefork 
> MPM and PHP scripts. 

Yes, if you are running PHP as a CGI -- that is, *not* via mod_php -- 
with the prefork MPM, then the Apache HTTP Server child that is handling 
the request will fork itself in order to execute the PHP script in a 
"grandchild" process.  However, this has nothing to do with database 
connections, and, since the PHP interpreter is not persistent -- a new 
PHP interpreter is started up for each CGI request and exits afterward 
-- there are no persistent database connections either; each PHP CGI 
will need to open its own database connection, if it needs one.   The 
order of events is:  An Apache HTTP Server child process receives a 
request for a PHP CGI; the child process forks itself, creating a 
"grandchild"; the grandchild Apache process immediately invokes the PHP 
interpreter to replace itself (Apache is no longer running in the 
grandchild at this point); the PHP interpreter in the grandchild process 
interprets the PHP script, sending its output to the parent process (the 
Apache child that is handling the request); the grandchild process 
terminates itself (exits) as soon as the PHP script has finished running.

But, if you are running PHP inside of Apache HTTP Server by using 
mod_php and the prefork MPM, the Apache HTTP Server child process that 
is handling the request will not fork itself.  Instead, the PHP script 
will get run directly inside the Apache process that is handling the 
request.  In this scenario, you can have persistent (cross-request) 
database connections.  The only way the Apache child process will get 
forked is if a request is made for a non-mod_php CGI (that is, a request 
that uses mod_cgid) or if a PHP script itself does something that forks 
the process (such as calling one of the exec() family of functions, 
system(), or something similar) -- in this case, the the entire Apache 
HTTP server child process will be (temporarily) duplicated, including 
the PHP interpreter and any already-established database connections.

These are mostly academic points in response to the question "are there 
ever circumstances in which an Apache HTTP Server child process will 
fork itself."  If you are using mod_php, don't worry about this.  If you 
are using mod_cgid, don't worry about it either except to keep in mind 
that forking is a relatively expensive operation, which is one reason 
why running conventional, external CGIs is slower than using an embedded 
interpreter or a persistent interpreter in an external daemon (e.g., 

   Mark Montague

The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:> for more info.
To unsubscribe, e-mail:
   "   from the digest:
For additional commands, e-mail:

View raw message