When running web app coding in PHP programming language, and which is assigned to the handler fcgid-script for execution through FastCGI protocol provided by mod_fcgid, the PHP script may run a while, and then suddenly return HTTP status code of 500 Internal Server Error.

Internet Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

500 Internal Server Error

Upon checking the Apache HTTPD web server error log, the following entries are logged:

[Mon Mar 11 08:08:28 2015] [warn] [client 192.168.1.1] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server, referer: https://techjourney.net/
[Mon Mar 11 08:08:28 2015] [error] [client 192.168.1.1] Premature end of script headers: backup.php, referer: https://techjourney.net/

The error can happen on any PHP script, but normally involves PHP script which takes a long time to complete its process, such as backup, import, export, deletion, replace and etc.

The error is normally related to FcgidBusyTimeout (old name BusyTimeout) and/or FcgidIOTimeout (old name IPCCommTimeout) directive which defines the maximum time limit for request handling. If a FastCGI request does not complete within FcgidBusyTimeout seconds, it will be terminated and killed. The purpose of this directive is to terminate hung applications.

By default, FcgidBusyTimeout directive has the value of 300 seconds. So a FastCGI application will be terminated anytime between 300 and 420 seconds (the app may run for longer period of time because the check is performed at the interval defined by FcgidBusyScanInterval, which is 120 seconds by default).

FcgidIOTimeout directive defines the maximum period of time the module will wait while trying to read from or write to a FastCGI application. By default, FcgidIOTimeout has 40 seconds communication timeout for FastCGI app to generate response, such as waiting for upload or download to complete.

So if you have a PHP script handled by FastCGI that takes longer time to complete or process, increase the timeout of FcgidBusyTimeout and/or FcgidIOTimeout directives.

To do so, modify the Apache web server httpd.conf or apache.conf. You can apply the settings globally on server for all virtual hosts or virtual servers (all websites and web apps hosted), or individually on each virtual hosts or virtual servers.

To apply the FastCGI directives globally, insert the following code before the virtual hosts declaration in httpd.conf or apache.conf. Sometimes, the <IfModule mod_fcgid.c> section is already existed, so you just need to add in the appropriate lines. Note that some Apache configuration may use another config file such as fcgid.conf located in conf.d, extra or includes directory to define module-specific parameters.

Note
In cPanel with WHM, you can use the Include Editor to easily add the directives. To access Include Editor, go to Service Configuration -> Apache Configuration -> Include Editor, and choose Pre Virtual Host Include.
<IfModule mod_fcgid.c>
# Other fcgid directives, if any, may also appears here.
FcgidBusyTimeout 3600
FcgidIOTimeout   3600
</IfModule>

If you decide to apply the increased timeouts for a particular virtual host (website) only, add the fcgid directives to within the virtual host declaration. For example:

<VirtualHost *:80>
# Other virtual host directives such as ServerName, DocumentRoot, ErrorLog, CustomLog and etc.
<IfModule mod_fcgid.c>
# Other fcgid directives, if any, may also appears here.
FcgidBusyTimeout 3600
FcgidIOTimeout   3600
</IfModule>
</VirtualHost>