Running php-fcgi independently from Lighttpd
Posted: June 9, 2009 | By: TJ | In Technology | No comments yet
As of Lighttpd 1.4.x, the spawn-fcgi binary is included in the package when you install Lighttpd. When Lighttpd starts, it will launch php-cgi processes under Lighttpd defined by the fastcgi.server value in the lighttpd.conf. The problem is that when you need to restart Lighttpd, the FastCGI processes don’t die. This also makes it difficult to monitor php-cgi as a service because it does not have a pid file. The solution is to remove spawn-fcgi and proxy all requests to a php-fastcgi daemon to keep it independent of Lighttpd altogether.
The first step was to create a php-fcgi default file, /etc/default/php-fcgi:
START=yes EXEC_AS_USER=www-data FCGI_HOST=localhost FCGI_PORT=9000 PHP_FCGI_CHILDREN=2 PHP_FCGI_MAX_REQUESTS=1000
Once this file has been created, now we want to make sure that php-fcgi starts when the server starts:
$ update-rc.d php-fcgi defaults
Now, edit the lighttpd.conf to proxy all php requests to port 9000:
fastcgi.server = ( ".php" => ( ( "host" => "127.0.0.1", "port" => 9000, "max-procs" => 1, "idle-timeout" => 15, "broken-scriptfilename" => "enable" )))
You’ll then need to create an init script so you can control the daemon. I’ve included mine to view.
Make the script executable:
$ chmod +x /etc/init.d/php-fcgi
Force reload the new Lighttpd configuration and kill any running php processes running from the prior http configuration:
$ /etc/init.d/lighttpd force-reload
Now, start php-fcgi:
$ /etc/init.d/php-fcgi






Nice post, TJ. I’ve been meaning to give Varnish a shot, and the Munin plugin looks like the icing on the cake