Dave Hall Consulting logo

Making it Easier to Spawn php-cgi on Debian and Ubuntu

Apache is a great web server, but sometimes I need something a bit more lightweight. I already have a bunch of sites using lighttpd, but I'm planning on switching them to nginx. Both nginx and lighttpd use FastCGI for running php. Getting FastCGI up and running on Ubuntu (or Debian) involves a bit of manual work which can slow down deployment.

The normal process to get nginx and php-cgi up and running is to install the spawn-fcgi package, create a shell script such as /usr/local/bin/php-fastcgi to launch it, then a custom init script, after making both of these executable you need to run update-rc.d then finally should be right to go. Each of these manual steps increases the likelihood of mistakes being made.

Instead, I created a deb contains a configurable init script. It is pretty simple, the init script calls spawn-fcgi with the appropriate arguments. All of the configuration is handled in /etc/default/php-fastcgi. The main config options are:

  • ENABLED - Enable (or disable) the init script. default: 0 (disabled)
  • ADDRESS - The IP address to bind to. default:
  • PORT - The TCP Port to bind to. default: 9000
  • USER - The user the php scripts will be excuted as. default: www-data
  • GROUP - The group the php scripts will be executed as. default: www-data
  • PHP_CGI - The full path to the php-cgi binary. default: /usr/bin/php5-cgi

The last 3 variables are not in the defaults file as I didn't think many users would want/need to change them, feel free to add them in if you need them.

Once you have set ENABLED to 1, launch the init scipt by executing sudo /etc/init.d/php-fastcgi start. To check that it is running run sudo netstat -nplt | grep 9000 and you should see /usr/bin/php5-cgi listed. Now you can continue to configure your webserver.

The package depends on php5-cgi and spawn-fcgi, which is available in Debian testing/squeeze, unstable/sid, along with Ubuntu karmic and lucid. For earlier versions of ubuntu you can change the dependency in debian/control from spawn-fcgi to lighttpd and disable lighttpd once it is installed so you can get spawn-fcgi . I haven't tested this approach and wouldn't recommend it.

You can grab the http://davehall.com.au/sites/davehall.com.au/files/php-fastcgi_0.1-1_all.deb">binary package and install it using dpkg or build it yourself from the source tarball.

For more information on setting up nginx using php-cgi I recommend the linode howto - just skip the "Configure spawn-fcgi" step :)

php-fastcgi.tar.gz3.75 KB
php-fastcgi_0.1-1_all.deb4.12 KB

David could you submit your

John Ferlito wrote:

David could you submit your changes as a patch to Debian? I think just about everyone does exactly what you did in one form or another. Would be great if Debian just did it

Added Thu, 2010-03-18 08:50

RE: David could you submit your

Dave wrote:

I don't really see how to submit this as a patch for Debian (or Ubuntu). I considered trying to patch an existing package, but couldn't find which one to make it fit.

  • spawn-fcgi would probably be the best place for it, except lighttpd has its own built in configuration for spawning the FastCGI process. lighttpd also depends on spawn-fcgi.
  • nginx can be used for more than PHP, and in some cases without PHP. I now have web server meta packages and just make my php-fastcgi package a dependency for nginx flavour
  • php5-cgi can be used by other (Fast)CGI implementations

I have no problem if someone wants to use this as the basis for an official Debian package. It's is GPLd after all. Sorry, but given my time commitments, I won't be becoming a Debian Developer any time soon.

Added Thu, 2010-03-18 20:16


Outolumo wrote:

Thanks, you just saved my day :)

Added Thu, 2010-06-10 06:37

cant install it it doesnt

bilal wrote:

cant install it it doesnt work. I compiled php 5.3.6 from source and spawn-fcgi from source as well, so when I try to install it from ur .deb package : dpkg -i fastcgi....deb it says cant find dependencies php5-fastcgi , spawn-fcgi not installed, even though I have then installed.

and I cant compile it from source, there isnt any ./configure file ... please fix it

Added Fri, 2011-03-18 14:10