If you are running WordPress on Nginx, PHP and spawn-fcgi, you may have noticed that sometimes PHP/spawn-fcgi crashes and hangs up. So far it happened to me twice in a week. One of the key reasons appears to be warnings and fatal errors in PHP but we can't pinpoint the exact cause of the PHP downtime so far.
As a temporary solution we use "monit" to control the spawn-fcgi auto-restart. It's a dirty hack - trust us - we know. But it sure prevents us from looking stupid when the 502 Gateway problem page shows instead of the site.
A quick fix
$ apt-get install monit
Then some configuration stuff we need to take care off:
edit /etc/default/monit and set the 'startup=1' parameter
then in /etc/monit/monitrc change the parameters to represent the spawn-fcgi setup.
set daemon 60 # check services at 1-minute intervals
check host localhost with address 127.0.0.1
start program = "/etc/init.d/spawn-fcgi start"
stop program = "/etc/init.d/spawn-fcgi stop"
if failed port 53217 then restart
Kill your cgi and wait 60 seconds to see it come up again. If it does, the workaround is in place, if it doesn't... check the configuration files again.
UPDATE: I've replaced spawncgi with php5-fpm package which totally outperforms the spawncgi solutions. This is a lot more stable than the former solution This page is the most popular one of my whole blog, just to give you an idea, switch over to php5-fpm and forget about spawncgi.