So you've got your very own Lighttpd server up and running as per this tutorial, but you want to serve a little more than just static content.
PHP is the obvious choice here - it is by far the most popular solution for dynamic content and is almost ubiquitous in the FOSS world. So how do we get Lighttpd to serve PHP content?
In this tutorial I'm going to show you how to compile PHP yourself and set it up using Lighttpd's FastCGI wrapper.
Before we get started, I should point out that you can find a quicker tutorial of this process on the official Lighttpd site. My tutorial is going to expand on that process a little and explain it some more as we go through.
First things first, let's grab the PHP source code from the PHP website so we can compile it for our purposes. At the time of writing, the latest version is PHP 5.2.4, so let's grab that.
Once that's downloaded, let's extract:
$ tar xjvf php-5.2.4.tar.bz2
The next step is to configure the PHP distribution for use. If you have custom compile options you want to enable, feel free to add them in here. If not, just follow this command:
$ ./configure --enable-fastcgi --enable-discard-path --enable-force-cgi-redirect
After that, it's the usual PHP install process:
$ make
$ su -c "make install"
(or sudo make install)
You should now have installed a FastCGI-ready PHP binary in /usr/local/bin/php-cgi. Now we need to edit our configuration file to tell Lighttpd to let this PHP handle all .php files. Sounds easy enough right?
So you now need to open up your Lighttpd configuration file in a text editor. If you followed the previous Lighttpd tutorial, that will be /etc/lighttpd/lighttpd.conf.
You need to add these lines:
server.modules = (
"mod_fastcgi",
) fastcgi.server = ( ".php" => ((
"bin-path" => "/usr/local/bin/php-cgi",
"socket" => "/tmp/php.socket"
)))
Save the configuration file. Once you restart Lighttpd, you should be able to successfully run PHP scripts. To test that, make a file called phpinfo.php. In that file, write this:
<?php phpinfo();?>
Save that, place it in /var/www, and then browse to http://localhost/phpinfo.php. If you see something like this:
Then you have successfully got PHP up and running - congratulations!