I am using php-cgi on ubuntu(with nginx), and the command to start it is:

spawn-fcgi -a -p 9100 -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid

but sometimes the php-cgi process will exit automatically, and I need to start it manually. Is there any way that can make it restart automatically, eg) write init script?

Please suggest, thanks.

2 Answers 2


You should really use PHP-FPM instead of spawn-fcgi. It's a fastcgi process manager which was written to overcome many of the problems with php-cgi (and thus with spawn-fcgi by relation).

Since the 5.2.x line of PHP is end of life'd you should also be on PHP 5.3.x which means you most likely already have php-fpm and it's just a matter of making sure your package manager installs that version (see if it's called php-fpm or similar). If you install from source then it's just a compile time flag to enable it. (--with-fpm)

If you're on 5.2.x and you're too enterprise for your boss to allow you to upgrade then you'll have to monitor it with something like Supervisord or Monit


I currently use upstart (initctl).


I'm running CentOS 6, but I think the process is similar for Ubuntu. For CentOS, create the file /etc/init/php-cgi.conf containing:

# Upstart event
# nginx_start - start nginx web server / proxy

start on runlevel 3
stop on runlevel [016]
console output

exec /usr/bin/php-cgi -b


respawn limit 10 90

Then you can control it with initctl:

initctl start php-cgi
initctl status php-cgi
initctl stop php-cgi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.