In this project, we will be exploring how to set up a Lighttpd Web Server on your Raspberry Pi. Lighttpd like NGINX is a fast and memory efficient alternative to Apache that is designed to have a relatively low memory footprint and a small CPU load, both of which are perfect qualities for the Raspberry Pi. One of the largest users of Lighttpd is the Wikimedia Foundation.
We will be exploring how to set up a basic Lighttpd server running off the default configuration, while also exploring on how to setup Lighttpd to work with PHP-FPM, as this does not work straight off the bat with the Raspbian version of Lighttpd.
There are a ton of other server projects for the Pi that you’re able to do. Each of them has their own pros and cons, but the Pi makes for a great little device to test them all.
Below are all the bits and pieces that I used for this Raspberry Pi Lighttpd tutorial, you will need an internet connection to be able to complete this tutorial.
Raspberry Pi 2 or 3
Setting up Lighttpd on the Raspberry Pi
1. Now before we go to install Lighttpd, we need to update the Raspberry Pi to ensure we are running the latest software. Run the following two commands to update your Raspberry Pi.
sudo apt-get update sudo apt-get upgrade
2. Now with the Pi up to date, we will need to remove Apache2. This required removal is since Raspbian includes it by default on most installations. If you are certain, you don’t have Apache2 installed you can skip this step.
sudo apt-get remove apache2
3. With the Raspberry Pi now up to date and Apache removed, we can get on with installing Lighttpd. Luckily for us, Lighttpd is provided in the package repository. Install it by running the following command.
sudo apt-get install lighttpd
4. Once Lighttpd has been installed, we can now check if is running, browse to your Raspberry Pi’s local IP Address from any web browser. If you don’t know your Raspberry Pi’s local IP address, type in the following command to get it.
On a successful connection, you should see a screen somewhat like what is displayed below. If you were one of those people that had to uninstall Apache2 first, then you may be greeted with an Apache default page, don’t be alarmed, as this still confirms that Lighttpd is up and running.
5. If you would like to make edits to this placeholder page, you can run the following command. All the files that are loaded are kept in /var/www/html.
sudo nano /var/www/html/index.html
Installing Lighttpd PHP
6. For this Raspberry Pi Lighttpd tutorial, we will have to make use of PHP-FPM. We will show you how to install PHP7. The reason we choose to use PHP 7 over its predecessor PHP 5 is that it’s more modern and provides performance and memory improvements.
7. Sadly PHP 7 is not available in Raspbian Jessie or earlier so we will have to do some modifications to grab PHP 7 from the newer Raspbian Stretch repository.
Raspbian Stretch and later
sudo apt-get install php7.0-fpm
Raspbian Jessie and earlier
Now to get PHP 7.0 to install on Raspbian Jessie and earlier we must modify the sources list to add the newer Stretch repository.
a. Let’s edit the apt sources list by entering the following command into the terminal.
sudo nano /etc/apt/sources.list
b. Add the following line to the bottom of this file.
deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi
Once done, save & exit by pressing CTRL +X and then pressing y.
c. Now we will need to edit the preferences file to ensure that Jessie packages are still chosen over Stretch. It will ensure we don’t end up with a weird combination of packages.
sudo nano /etc/apt/preferences
d. To set the priority, enter the following lines into this file.
Package: * Pin: release n=jessie Pin-Priority: 600
Once done we can save & exit by pressing CTRL +X and then pressing y.
e. Now that we have added our new repository and have set our preferences we need to run an update so that the Raspbian OS will read in all the new packages.
sudo apt-get update
f. With that all done we can finally install PHP7.0-FPM by utilizing the following command.
sudo apt-get install -t stretch php7.0-fpm
8. Now that we have installed PHP-FPM there are a few things we will have to do, to start off we will first need to run the following two commands, and this creates two symlinks so that Lighttpd will start reading the configuration files that set it up for PHP.
sudo lighttpd-enable-mod fastcgi sudo lighttpd-enable-mod fastcgi-php
9. Now before we can go reloading Lighttpd, we will have to make changes to its configuration. By default, it is configured to work with PHP-CGI and not PHP-FPM, luckily it is easy to make these changes.
Let’s begin by running the following command to start editing the file.
sudo nano /etc/lighttpd/conf-available/15-fastcgi-php.conf
10. Change the file so that it looks like below. Basically, this makes it, so it just utilizes the PHP-FPM socket file. Besides the definition of the socket to use and the broken-scriptfilename setting, we discard the other options.
# -*- depends: fastcgi -*- # /usr/share/doc/lighttpd/fastcgi.txt.gz # http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi ## Start an FastCGI server for php (needs the php5-cgi package) fastcgi.server += ( ".php" => (( "socket" => "/var/run/php/php7.0-fpm.sock", "broken-scriptfilename" => "enable" )) )
Simply save & exit by pressing CTRL +X and then pressing y. Lastly, press enter.
11. Finally, with that all done, we can now force Lighttpd to reload by typing the following command into the terminal.
sudo service lighttpd force-reload
12. With that done, let’s make a PHP file to test whether our new setup is working, run the following command to begin writing a new PHP file in our /var/www/html folder.
sudo nano /var/www/html/index.php
13. Within this file, type in the following lines, we use phpinfo() as it is one of the best indicators that php is working correctly.
<?php phpinfo() ?>
Now you should be done, and you can save & exit by pressing CTRL +X and then pressing y.
14. Finally, we can now go to our Raspberry Pi’s local IP address in a web browser like we did earlier. We should see a screen showcasing all our PHP information. If this screen is displayed, then you have successfully set up a Lighttpd web server on your Raspberry Pi that can also utilize PHP.
I hope this tutorial on Raspberry Pi Lighttpd has been able to help you with getting your web server setup. If you have any questions, queries, thoughts, or anything else, then be sure to leave a comment over at our forum.