Nginx – How to restart php-cgi automatically with spawn-fcgi

nginxphp-cgispawn-fcgi

I'm running nginx with php as fcgi. It's working just fine, however, php-cgi keeps on exit()ing after serving 500 requests. I tried increasing that value (PHP_FCGI_MAX_REQUESTS), and that worked, but that seems to be a workaround. Then I set it to 0, and it didn't exit() yet. But I think there's a reason why php-cgi should be restarted. At the moment, I'm running php-cgi with spawn-fcgi: when the php process exits, spawn-fcgi exits, too. Now, is there a way to automatically restart php (without dirty hacks like while [ 1 ]; do spawn-fcgi; done etc)?

Best Answer

Is there any reason you can't switch over to PHP-FPM? It comes with PHP >= 5.3, and there is a patch to enable it for 5.2. I've been using it for around a year now, and it's been perfectly stable with no issues. This would make your process spawning issue disappear, as PHP-FPM would transparently handle killing the old processes and restarting new ones for you.

The reason for PHP_FCGI_MAX_REQUESTS is that PHP will randomly stop responding to requests after a certain number of them. It seems that this bug may have been partially fixed in newer versions, but during my testing of 5.2.x I noticed it happened multiple times.