lighttpd is know to be a light and fast server, but you don't appreciate it until you combine PHP with fastcgi.
This guide is oriented for Gentoo Linux, but it's easy to adapt it to another distros.
Before emerging PHP, make sure that you have enabled the flag fastcgi, -apache and -apache2. Choose the others as you wish or need.
You can enable individual use flags on the file /etc/portage/package.use with the syntax category/package flag1 flag2
dev-lang/php-5.1.4 use flags:
USE="bcmath berkdb calendar cdb cgi cli crypt ctype dbase force-cgi-redirect ftp gd imap ipv6 ldap memlimit mysql mysqli ncurses nls pcntl pcre pdo-external pic posix postgres readline reflection sasl session sharedmem soap sockets spl sqlite ssl truetype xml xmlrpc zlib -adabas -apache -apache2 birdstep bzip2 cjk curl curlwrappers db2 dbmaker debug -discard-path -doc -empress -empress-bcs -esoob -exif -fastbuild -fdftk -filepro -firebird -flatfile -frontbase -gd-external -gdbm -gmp -hardenedphp -hash -hyperwave-api -iconv -informix -inifile -interbase -iodbc -java-external -kerberos -libedit -mcve -mhash -ming -msql -mssql -oci8 -oci8-instant-client -odbc -pdo -qdbm -recode -sapdb -sharedext -simplexml -snmp -solid -spell -sybase -sybase-ct -sysvipc -threads -tidy -tokenizer -unicode -vm-goto -vm-switch -wddx -xmlreader -xmlwriter -xpm -xsl -yaz -zip"
Now emerge it and go get a coffee as it could take a while:
emerge -av dev-lang/php
After PHP compiles successfully, let's edit the lighttpd.conf file. First make sure that the module fastcgi is uncommented at the server modules:
server.modules = ( "mod_fastcgi", "mod_access", "mod_accesslog" )
and then uncomment or include the fastcgi configuration file:
include "mod_fastcgi.conf"
That is just to make the configuration file more clear to the reader. The fastcgi configuration could be included at lighttpd.conf without problems.
Now let's edit the mod_fastcgi.conf file. You could use the following configuration as example:
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"socket" => "/tmp/php-fastcgi.socket",
"bin-path" => "/usr/lib/php5/bin/php-cgi",
"min-procs" => 2,
"max-procs" => 4,
"max-load-per-proc" => 8,
"idle-timeout" => 50,
# Fix PATH_INFO for PHP scripts that rely on it (like Wordpress).
"broken-scriptfilename" => "enable"
)
)
)
It's very important (even more for us that are on a limited memory environment), to control the min and max proc variable, as it will burn more or less memory depending on how many procs we set. On a small site the it could be set at 2 or even 1 without problems, making lighttpd run with very low memory, but if you're going to use more populated applications, like a forum, feel free to edit it with your needs.
Another alternative to starting the fastcgi within lighttpd is using fastcg with a different init script, for that you just need to delete the line of bin-path, and make sure that you start the service /etc/init.d/spawn-fcgi every time you start lighttpd too. Everything else remains the same.
Now you are set. Restart the process and enjoy your fast server:
/etc/init.d/lighttpd restart