First, my solution is only applicable to the Apache Web Server.
I am working on a script meant to act as a csv download script for a report against a very very large db, and I encountered this problem too. Am NOT using php, but instead my script is written in some obscure language called heitml ;-)
The request timeout proble does occur in my scenario like this:
[Wed Sep 19 20:29:01 2012] [warn] [client ::1] Timeout waiting for output from CGI script /var/www/cgi-bin/heitml
[Wed Sep 19 20:29:01 2012] [error] [client ::1] (70007)The timeout specified has expired: ap_content_length_filter: apr_bucket_read() failed
And the only serious solution I can currently adapt to is using this official timeout config extension here : mod_reqtimeout. It allows adjustment of timeout params like for example:
Allow 10 seconds to receive the request including the headers and 30 seconds for receiving the request body:
RequestReadTimeout header=10 body=30
Allow at least 10 seconds to receive the request body. If the client sends data, increase the timeout by 1 second for every 1000 bytes received, with no upper limit for the timeout (exept for the limit given indirectly by LimitRequestBody):
RequestReadTimeout body=10,MinRate=1000
Allow at least 10 seconds to receive the request including the headers. If the client sends data, increase the timeout by 1 second for every 500 bytes received. But do not allow more than 30 seconds for the request including the headers:
RequestReadTimeout header=10-30,MinRate=500
Usually, a server should have both header and body timeouts configured. If a common configuration is used for http and https virtual hosts, the timeouts should not be set too low:
RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500
Am yet to find out whether there's a better solution offered by Apache that doesn't require me to use an this module (assuming it's not installed by default -- though it's included in all versions 2.2.15 and later).