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 01:18:18 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 01:18:18 2015] [warn] [client 192.168.1.1] (104)Connection reset by peer: mod_fcgid: ap_pass_brigade failed in handle_request_ipc function, 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) 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).

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

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
</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
</IfModule>
</VirtualHost>
Note
Apache mod_fcgid documentation lists out another possibility which the HTTP 500 error may occur, which is due to limit of PHP_FCGI_MAX_REQUESTS and/or FcgidMaxRequestsPerProcess.

PHP FastCGI processes exit after handling 500 requests, and they may exit after this module has already connected to the application and sent the next request, resulting in 500 Internal Server Error.

Thus if above timeout limit change to FcgidBusyTimeout doesn’t fix the issue, try the one of following instead (these directives can be configured in a corresponding wrapper script which is configured via FcgidWrapper directive):

  1. Set PHP_FCGI_MAX_REQUESTS to 0 to disable (may cause problem if the PHP application leaks resources)
  2. Set PHP_FCGI_MAX_REQUESTS can be set to a much higher value than the default to reduce the frequency of recycling of processes.
  3. FcgidMaxRequestsPerProcess can be set to a value less than or equal to PHP_FCGI_MAX_REQUESTS to resolve the problem.

For configuration examples of the PHP_FCGI_MAX_REQUESTS, check out examples in mod_fcgid documentation.