Installing & configuring Lighttpd with PHP on FreeBSD

Recently, I had to install Lighttpd on a FreeBSD server. Now generally I install Apache as a web server, but this time because of the gains that Lighttpd has over Apache, I decided to give it a go. The following instructions have been primarily taken from http://trac.lighttpd.net/trac/wiki/TutorialInstallation, some changes primarily being for the path to more suit FreeBSD defaults.

Install lighttpd from ports (Update the ports tree before this)

cd /usr/ports/www/lighttpd
make install clean

Make sure you have enabled FastCGI support in PHP.

Enable Lighttpd in rc.conf and edit it’s configuration:

echo lighttpd_enable="YES" >> /etc/rc.conf
cd /usr/local/etc/
cp lighttpd.conf.sample lighttpd.conf

Create some directories and files:

mkdir /usr/local/www/lighttpd
mkdir /usr/local/www/lighttpd/log
mkdir /usr/local/www/lighttpd/data

touch /usr/local/www/lighttpd/log/lighttpd.error.log
touch /usr/local/www/lighttpd/log/lighttpd.access.log

Make your new directories and files accessible by the user and group “www” that Lighttpd operates as:

chown -R www:www /usr/local/www/lighttpd

Edit lighttpd.conf

vi /usr/local/etc/lighttpd.conf

Change the values for directories and files as follows:

server.document-root = "/usr/local/www/lighttpd/data" 
server.errorlog = "/usr/local/www/lighttpd/log/lighttpd.error.log" 
accesslog.filename = "/usr/local/www/lighttpd/log/lighttpd.access.log"

Save and exit from the editor.

Test to make sure Lighttpd starts up properly:

/usr/local/etc/rc.d/lighttpd start

If you need to, you may shutdown lighttpd this way:

 /usr/local/etc/rc.d/lighttpd stop

And then restart it with this:

/usr/local/etc/rc.d/lighttpd restart

If you get an error that says “permission denied”, check to make sure that the files and directories mentioned exist AND that user www has permission to operate on them. Also be sure that lighttpd.conf has been corrected from the non-working default version. If you get no errors, put an HTML file in /usr/local/www/lighttpd/data and try to load it up in your browser. If everything works, now enable PHP.

Edit lighttpd.conf once again

# vi /usr/local/etc/lighttpd.conf
  • In section “server.modules” uncomment the line “mod_fastcgi”
  • Uncomment the entire section “fastcgi.server”
  • Add the following lines under “socket” in the “fastcgi.server” section:
		"bin-path" => "/usr/local/bin/php-cgi",
		"broken-scriptfilename" => "enable"

Now make some more files and directories:

mkdir /var/run/lighttpd
touch /var/run/lighttpd/php-fastcgi.socket

Make your new directories and files accessible by the user and group “www” that Lighttpd operates as:

chown -R www:www /var/run/lighttpd