Using supervisord for Nginx+FastCGI+PHP

I was having problems with spawn-fcgi-standalone occasionally resulting in dead PHP processes, which caused 502 Bad Gateway errors on my site. Crontabbing an /etc/init.d/init-fastcgi start didn’t help much, so I looked for other ways to do it. Supervisord looked promising.

Here’s how to get Supervisord:

apt-get install python-setuptools
easy_install supervisor

 

Here’s what to add to /etc/supervisord.conf:

[fcgi-program:php5-cgi]
socket=tcp://127.0.0.1:9000
command=/usr/bin/php5-cgi
numprocs=5
priority=999
process_name=%(program_name)s_%(process_num)02d
user=www-data
autorestart=true
autostart=true
startsecs=1
startretries=3
stopsignal=QUIT
stopwaitsecs=10
redirect_stderr=true
stdout_logfile=/var/log/php5-cgi.log
stdout_logfile_maxbytes=10MB

So far, so good. When I kill the php process, supervisord starts it back up. Progress!

supervisord doesn’t come with an init.d script, but you can get one for Ubuntu.

  • doddy

    But…what is the root of the problem? Why is PHP dying? Can you sleep at night without knowing this?

  • http://sachachua.com Sacha Chua

    I came across some webpages that suggested php-fcgi wasn’t the most reliable way to do it (long pages would die, etc.), and that fpm or supervisord might be better. This way, I can keep the extraneous bad gateway errors from my error logs, and focus on what’s causing the occasional process to die.

  • perusio

    Have you heard of Monit? I use for supervising both nginx and php-cgi. It’s written in C and not in Python. http://mmonit.com

    I’ve published by monit configuration for supervising MariaDB (MySQL), nginx, and php-cgi, among other things on github: http://github.com/perusio/monit-miscellaneous just in case you’re interested in checking out monit.

  • http://sachachua.com Sacha Chua

    Thanks for sharing!

  • http://new.evite.com warren

    Supervisord has been great to work with. However, sometimes it seems to not want to log to the specified stdout_logfile . Even tho there is absolutely std out.

    Have you ever had issues with supervisord logging to stdout_logfile ?

  • http://www.talentescalator.com Divyesh Ramani

    Thanks. very helpful.. Finally configured it for my blog with auto start. No worries about crashing php5-cgi process.

  • http://espercorp.com Juan

    @warren: Yeah! i’m seeing the same thing. Ever figure it out?

  • http://new.evite.com warren

    Yea, supervisord only logs in bursts when there is enough data for the particular process. In my case it was only writing when there was ~1K worth of data (and it was a timed job, so it appeared to be related to time – but that was not the case).

    I have also seen some strange behavior with the log rotation. For example, if you are logging to test.log, sometimes you will find it is logging to test.log.5

    A ls -lart helps with that :)

    All things considered, supervisord has been doing a great job. We have been using it in production for a couple months.