Problem Description:
I just solved a strange situation where peu-à-peu the number of php5-cgi processes kept on increasing till all the RAMS were used and the system went to a crawl no more responding to web requests.
Cause:
After the maximum number of requests got reached for a php5-cgi requests the process gets detached from
its parent process and given to the process init (PPID=1). This way the process still runs in the
background but will never be used any more to answer requests. New php5-cgi will be created by the
parent process (fcgid handler) and after 500 requests the same thing will happen again. This detachment
instead of being killed is due to the settings of the directive MaxRequestsPerProcess bein higher than
the environment variable of PHP: PHP_FCGI_MAX_REQUESTS=xxxx
Solution:
make sure both settings of MaxRequestsPerProcess in /etc/apache2/mods-available/fcgid.conf has the same value as the setting of the environment variable set in the fcgid-wrapper PHP_FCGI_MAX_REQUESTS.
The default value of PHP_FCGI_MAX_REQUESTS is 500, so if you didn’t set this variable in your configurations then this setting in /etc/apache2/mods-available/fcgid.conf will do:
MaxRequestsPerProcess 500
Reload your Apache configuration:
/etc/init.d/apache2 reload
kill all php5-cgi processes which have a PPID of ‘1’:
kill $(ps --ppid=1 | grep php5-cgi | awk '{print $1}') 2>/dev/null
and all is ok.
Note: Extract of Apache documentation for more info at:
https://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html#fcgidmaxrequestsperprocess
By default, 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. When that occurs, an error
will be logged and 500 Internal Server Error will be returned to the client. This PHP behavior can be
disabled by setting PHP_FCGI_MAX_REQUESTS to 0, but that can be a problem if the PHP application leaks
resources. Alternatively, PHP_FCGI_MAX_REQUESTS can be set to a much higher value than the default to
reduce the frequency of this problem. FcgidMaxRequestsPerProcess can be set to a value less than or
equal to PHP_FCGI_MAX_REQUESTS to resolve the problem.
Also notice this info which is not on the same subject but relevant to PHP and FCGI.
PHP child process management (PHP_FCGI_CHILDREN) should always be disabled with mod_fcgid, which
will only route one request at a time to application processes it has spawned; thus, any child processes
created by PHP will not be used effectively. (Additionally, the PHP child processes may not be
terminated properly.) By default, and with the environment variable setting PHP_FCGI_CHILDREN=0,
PHP child process management is disabled.