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 to your Raspberry Pi.
Before you get started with this section, make sure that you are running Raspbian Buster or newer. If you are still running Raspbian Stretch, you can follow our guide on upgrading to Raspbian Buster.
7. You can run the command below to install PHP7.3 to your Raspberry Pi.
We will also be installing several additional PHP modules that will help you run most PHP scripts without the need to have to install more packages.
sudo apt-get install php7.3-fpm php7.3-mbstring php7.3-mysql php7.3-curl php7.3-gd php7.3-curl php7.3-zip php7.3-xml -y
Once you have finished installing PHP7.3 to your Raspberry Pi, we will have to go ahead and make some configuration changes to Lighttpd so that it knows to make use of it.
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.3-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 below.