Php-cgi constantly recycles every couple of minutes

I run a fairly busy site with a Wordpress blog and vBulletin forums. I
recently dumped Apache and and using nginx for everything (which has
been incredible and I would never go back). I’m using PHP as fastCGI
and have a wrapper script to start the process. We average about 1,000
concurrent users at any given time. Usually anywhere from 800 to 1200+.

This is the script I found to start php-cgi:

#!/bin/sh

PHP=/usr/bin/php-cgi
PHP_PID=/tmp/php.pid
FCGI_BIND_ADDRESS=127.0.0.1:8888
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000

env -i PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN
PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS
$PHP -b $FCGI_BIND_ADDRESS &

echo $! > “$PHP_PID”

This has been working great and PHP hasn’t completely died on me yet.

The problem that I’m having is that while viewing TOP, php-cgi seems to
die and restart about every 2-3 minutes, sometimes as often as every
minute. At least it restarts itself, but that can take a couple of
seconds, causing a delay if you happen to be browsing the site during
this time.

In the above script, I’ve tried increasing the PHP_FCGI_MAX_REQUESTS,
even as high as 1,000,000. I’ve also tried completely omitting that
value. No matter what, php-cgi reacts the same way.

I know it needs to recycle itself to prevent memory leaks, but I don’t
think it should be happening that often. Any ideas on how to fix this?

Posted at Nginx Forum:

On 01/25/2010 07:17 PM, mindfrost82 wrote:

value. No matter what, php-cgi reacts the same way.

I know it needs to recycle itself to prevent memory leaks, but I
don’t think it should be happening that often. Any ideas on how to
fix this?

Maybe posting in php-fpm mailing list can be of more help.

On Mon, Jan 25, 2010 at 2:53 PM, Juan Fco. Giordana
[email protected] wrote:

Maybe posting in php-fpm mailing list can be of more help.

he’s not running php-fpm though :slight_smile: might be able to help, but then
again, it’s people who have moved away from php-cgi and
standalone/etc. and moved to php-fpm.

On Mon, Jan 25, 2010 at 11:17 AM, mindfrost82 [email protected]
wrote:

PHP_FCGI_CHILDREN=4

nginx mailing list
[email protected]
nginx Info Page

PHP_FCGI_MAX_REQUESTS=1000

The children get automatically recycled when using this method, I
believe.

– Merlin

On Thu, Jan 28, 2010 at 8:16 AM, mindfrost82 [email protected]
wrote:

users at any given time. Usually anywhere from

PHP_FCGI_CHILDREN=4
This has been working great and PHP hasn’t
In the above script, I’ve tried increasing the
php-cgi constantly recycles every couple of minutes

I’m not using php-fpm and don’t really want to recompile PHP to switch to it. This is the only issue I have with this setup, so any help is appreciated. I tried taking out the PHP_FCGI_MAX_REQUESTS option altogether and it still recycled itself. I also upped it to 1,000,000 and it didn’t make a difference.

Posted at Nginx Forum: Re: php-cgi constantly recycles every couple of minutes


nginx mailing list
[email protected]
nginx Info Page

What I was saying was that this is the expected behavior, from what I
can tell. Even with FPM children will still be getting started, but
you won’t ever (in theory) run out of children as can sometimes happen
when php-cgi totally crashes.

You should try tuning for more children, 4 is nothing on such a busy
site. Try say 16 (or maybe more) children with 4096 max requests and
see if this improves things at all. If not you’ll need to
horizontally scale out to more fastcgi servers for nginx to talk to.

– Merlin

merlin corey Wrote:

 I’ve also tried completely omitting that value.

this method, I believe.

– Merlin


nginx mailing list
[email protected]
nginx Info Page

I’m not using php-fpm and don’t really want to recompile PHP to switch
to it. This is the only issue I have with this setup, so any help is
appreciated. I tried taking out the PHP_FCGI_MAX_REQUESTS option
altogether and it still recycled itself. I also upped it to 1,000,000
and it didn’t make a difference.

Posted at Nginx Forum:

So I have tried a few more different things and php-cgi is still
recycling way too often in my opinion.

I tried the earlier suggestion of using 16 children (instead of 4) with
4096 max requests. It does appear that the max requests is getting
ignored completely, as Rob said. With 16 children, the site is MUCH
faster, but php-cgi is getting recycled about every minute still. The
reason I think its getting ignored is because I can set it to 1 million
and it will still recycle just as often…unless there’s something else
causing this.

I also tried changing from TCP to a socket with the same result. Which
do you guys suggest anyway?

Whenever php-cgi recycles itself, while using TCP, I’ll get these in the
nginx logs:
recv() failed (104: Connection reset by peer) while reading response
header from upstream
connect() failed (111: Connection refused) while connecting to upstream

I might just have to switch to php-fpm, but if there’s another
underlying cause here, I’m not sure if it’ll matter. Do you still have
to recompile PHP to get php-fpm to work, or is there an easier way?

Posted at Nginx Forum:

I’m not using php-fpm and don’t really want to recompile PHP to switch to it. This is the only issue I have with this setup, so any help is appreciated. I tried taking out the PHP_FCGI_MAX_REQUESTS option altogether and it still recycled itself. I also upped it to 1,000,000 and it didn’t make a difference.
I remember reading that few people have had issues with
PHP_FCGI_MAX_REQUESTS never being honored with using standard PHP
FASTCGI implementation.
http://article.gmane.org/gmane.comp.web.nginx.english/4542/match=php_fcgi_max_requests

Most people just recommend using PHP-FPM as it is a more robust manager.

Rob

Right now you have to compile PHP separately for PHP-FPM. Which distro
are you using? Currently it has finally been merged into Core and may be
released with php 5.3.2 or possible 5.3.3. You can look more at
http://php-fpm.org/download/

I’m running Red Hat Enterprise using PHP 5.2.10 that was compiled using
EasyApache (before the switch to nginx).

Posted at Nginx Forum:

On Fri, Jan 29, 2010 at 9:04 AM, mindfrost82 [email protected]
wrote:

I’m running Red Hat Enterprise using PHP 5.2.10 that was compiled using EasyApache (before the switch to nginx).

Posted at Nginx Forum: Re: php-cgi constantly recycles every couple of minutes


nginx mailing list
[email protected]
nginx Info Page

You could try updating PHP and whatever you’re spawning it with.

– Merlin