How do I configure Apache 2.2 to Run Background PHP Processes on Win 2003?

Posted by Captain Obvious on Server Fault See other posts from Server Fault or by Captain Obvious
Published on 2010-03-17T03:37:47Z Indexed on 2010/03/18 15:11 UTC
Read the original article Hit count: 336

I have a script, testforeground.php, that kicks off a background script, testbackground.php, then returns while the background script continues to run until it's finished.

Both the foreground and background scripts write to the output file correctly when I run the foreground script from the command line using php-cgi:

C:\>php-cgi testforeground.php

The above command starts a php-cgi.exe process, then a php-win.exe process, then closes the php-cgi.exe almost immediately, while the php-win.exe continues until it's finished.

The same script runs correctly but does not have permission to write to the output file when I run it from the command line using plain php:

C:\>php testforeground.php

AND when I run the same script from the browser, instead of php-cgi.exe, a single cmd.exe process opens and closes almost instantly, only the foreground script writes to the output file, and it doesn't appear that the 2nd process starts:

http://XXX/testforeground.php

Here is the server info:

OS: Win 2003 32-bit
HTTP: Apache 2.2.11 
PHP: 5.2.13
Loaded Modules:  core mod_win32 mpm_winnt http_core mod_so mod_actions mod_alias mod_asis mod_auth_basic mod_authn_default mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_dir mod_env mod_include mod_isapi mod_log_config mod_mime mod_negotiation mod_setenvif mod_userdir mod_php5

Here's the foreground script:

<?php

ini_set("display_errors",1);
error_reporting(E_ALL);

echo "<pre>loading page</pre>";

function run_background_process()
{
 file_put_contents("0testprocesses.txt","foreground start time = " . time() . "\n");
 echo "<pre>  foreground start time = " . time() . "</pre>";

 $command = "start /B \"{$_SERVER['CMS_PHP_HOMEPATH']}\php-cgi.exe\" {$_SERVER['CMS_HOMEPATH']}/testbackground.php";

 $rp = popen($command, 'r');
 if(isset($rp))
 {
  pclose($rp);
 }

 echo "<pre>  foreground end time = " . time() . "</pre>";
 file_put_contents("0testprocesses.txt","foreground end time = " . time() . "\n", FILE_APPEND);
 return true;
}

echo "<pre>calling run_background_process</pre>";

$output = run_background_process();

echo "<pre>output = $output</pre>";

echo "<pre>end of page</pre>";
?>

And the background script:

<?php
$start = "background start time = " . time() . "\n";
file_put_contents("0testprocesses.txt",$start, FILE_APPEND);

sleep(10);

$end = "background end time = " . time() . "\n";
file_put_contents("0testprocesses.txt", $end, FILE_APPEND);
?>

I've confirmed that the above scripts work correctly using Apache 2.2.3 on Linux.

I'm sure I just need to change some Apache and/or PHP config settings, but I'm not sure which ones. I've been muddling over this for too long already, so any help would be appreciated.

© Server Fault or respective owner

Related posts about apache2.2

Related posts about Windows