Centos – Prevent CGI Timeout in Apache with PHP

apache-2.2centoscgifastcgiPHP

I have a PHP process which the user runs in the browser, but tends to take a fairly long time – upwards of 5 minutes. We just switched to a new server, and when we try to run the script now, it consistently times out at 4 minutes exactly, sending to a 500 Internal Server Error page and outputting the following errors in the error log:

Timeout waiting for output from CGI
script

Premature end of script headers:
cgi_wrapper

What's interesting is that the PHP process does seem to complete in the background. If we navigate away from the 500 Error page and wait a bit, the desired changes from the script do get made.

Is there any way to unhinge this CGI limit which seems to have been placed on us? I have read about FastCGI, but am not sure how to install this on our server (Apache, centOS, PHP 5.3).

Best Answer

That's not the way to solve the problem - HTTP is not intended to handle operations taking a very long time - while it sounds like your current PHP config is allowing the program to run for a long time this implies that you've misconfigured it - it should know when the webserver drops the conenction and respond appropriately. It might be possible to fudge the webserver config, and even to get a browser to stay connected for long enough, and maybe you don't have to worry about proxies in between - but you're using each of the tools in a way they were not designed to be used.

The right way to solve the problem is to start it from a web page, but running is a seperate process group and add in code to the long running process to write the status somewhere which can then be polled by a script triggered from the browser.