An init script to launch PHP the FastCGI daemon and spawn when needed in Debian
Failed to load latest commit information.
php-cgi.sig * Updated the signature. Sep 15, 2010

Debianized Init Script for running php-cgi with Nginx


This is an init script written in a Debian like way for running PHP FastCGI securely and efficiently.

Contrary to an widespread Urban Legend there’s no need for the spawn-fcgi program from Lighttpd for the PHP FastCGI process to spawn new instances when necessary. This script is all you need to run the PHP FastCGI daemon.


  1. Use of an /etc/default/php-cgi file that specifies whether to use UNIX or Internet sockets. The default is UNIX.
  2. PHP CGI process runs as the unprivileged user and group www-data.
  3. start, stop, restart, force-reload and status actions.


  1. Install the default/php-cgi in /etc/default. Note that I’m assuming a Debian like layout of the configuration files.
  2. Install the php-cgi file in /etc/init.d.
  3. Enable the php-cgi init script in the system run level configuration by issuing:

    update-rc.d php-cgi defaults 99.

    This means that php-cgi will be started in run levels 2, 3, 4 and 5 with a priority of 99 when launching all the processes for the specified run level.

  4. Start the PHP FastCGI daemon with the command:

    /etc/init.d/php-cgi start

    Now if you invoke /etc/init.d/php-cgi status the script will tell if the process is running or not. If it is, then a /tmp/php-cgi directory with permissions 700 will appear. This is where the UNIX sockets are created.

  5. Now run your web server or whatever process you want to relay requests to the PHP FastCGI daemon. Every thing should work.
  6. You can tune the FastCGI parameters PHP_FCGI_CHILDREN and PHP_FCGI_MAX_REQUESTS to whatever values work best for you. They’re defined in the /etc/default/php-cgi file.

Monitoring the process

I’ve published a Monit configuration file for php-cgi on github.


If you’re using the PHP FPM (FastCGI Process Manager) the script can be used almost verbatim. Just change the value of DAEMON to php-fpm and you’re done. Note that a lot of niceties like reloading of the configuration are missing.

Security & Trust

The script is signed with my GPG key. Just do gpg --keyserver --recv-keys 4D722217. Verify by issuing gpg --verify php-cgi.sig.


This script steals heavily from the Nginx Wiki PHP FastCGI example.