5067 comments
2220 subscribers
4799 on Twitter
Subscribe! Feed reader E-mail

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.

Short URL: http://sachachua.com/blog/p/7227

8 Responses to “Using supervisord for Nginx+FastCGI+PHP”

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

  2. 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.

  3. 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.

  4. Thanks for sharing!

  5. 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 ?

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

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

  8. 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.

Comment, share a thought, ask a question...

Please comment as you, not your organization.





 

On This Day...

  • 2011: Portal 2 — So we bought Portal 2 for the PS3, because you can unlock a PC version if you link your Playstation [...]
  • 2007: Expertise is more than meets the eye — Last Wednesday, I shared this amazing anecdote from a research paper on expert levels of performance (Ericsson, 1998) about the Italian violinist, [...]
  • 2006: A little too optimistic — Looks like the painkillers and other things just took a while to get out of my system. Ouch! Apparently, also still [...]
  • 2006: First taste of the Canadian medical system — Wednesday was an interesting time for me. I am deeply, deeply grateful to Leigh Honeywell and Jed Smith, who took me [...]
  • 2004: MVC — I thought about using the php.MVC framework for my webapps, but it seems like too much hassle. I think I’ll go [...]
  • 2004: Reflections on class — Today was an excellent class day! CS21A: Dr. Sarmenta went through half of Java Basics. I did too. Students nodding. Assigned bank [...]
  • 2004: A problem with my album photo? — A photo you added to your Tickle Networking profile on Tuesday, June 22 does not meet our photo approval guidelines and [...]