A web server is a program that runs on a machine — in this case your Raspberry Pi — and handles incoming requests from web browsers to load web pages. Another term for this would be a web daemon, although this term is used more rarely.
In Unix-like systems like the Raspberry Pi, a daemon refers to a program that runs in the background without an interactive interface. It sits there and does its thing, and that’s exactly what we want here with a Raspberry Pi web server.
There are plenty of web servers out there, but the most common one is Apache HTTP Server. Alternatives you could install include nginx, lighttpd, thttpd, and Tomcat. Each has its own benefits and use cases, but Apache HTTP Server is the de-facto standard for the web, used by over a third of all sites worldwide.
Another term you will often come across is LAMP. It’s an acronym for Linux, Apache, MySQL and PHP. These four components are the four most common elements that you’ll need to run most websites, from basic, one-page sites to complex WordPress installations.
The LAMP stack (as this is called) is made up of:
- Linux: the operating system itself. The Raspberry Pi (previously Raspbian) OS used on many Raspberry Pi's is a Linux-based operating system.
- Apache: the web server itself, as described above.
- MySQL / MariaDB: the most popular database management engine. It stores the dynamic data that websites running WordPress will use.
- PHP: a programming language used to generate dynamic web pages. It is controlled by Apache, and connects to MySQL to fetch or store data.
A Raspberry Pi web server generally requires all four of these components installed to be able to run basic and advanced websites effectively.
For this tutorial, we are going to assume that you have a Raspberry Pi 3 or 4, but even a 2 should work, just a little more slowly. You probably won’t have the system resources available to run a web server on a Raspberry Pi Zero, but there’s nothing to stop you trying.
We'll also assume that your Raspberry Pi is running the latest version of Raspberry Pi OS (previously Raspbian). Installing a Raspberry Pi web server on other Linux distributions may require some tweaking to these steps, so we suggest looking into the documentation for how to install these packages if you run into problems.
You can run these commands in a terminal window on the same Raspberry Pi you are using, or by connecting to it via SSH and installing the packages remotely.
To begin, you’ll want to update the list of available software packages on your Raspberry Pi.
Start by opening a terminal window on your Raspberry Pi (using a keyboard or mouse) or by opening a remote SSH terminal connection. Once the connection is open, type the following to update the list of packages:
sudo apt update
Type your password, if required.
The exact output will vary, but what matters is that no errors show up. You may need to update some system packages, so to fully update your Raspberry Pi OS, type:
sudo apt upgrade
Once this process is complete, you’ll want to install Apache itself. Raspberry Pi OS handles this as a single installation package, so to install Apache, type:
sudo apt install apache2
Confirm by typing Y and pressing the enter key.
As you’ll see, other additional required packages also be installed alongside Apache, so this step may take some time. Apache’s installation should be automatic, with the web server starting automatically once this process has completed.
You can test the web server by opening a web server on your Raspberry Pi and typing http://localhost. If you’re not running a graphical user interface on your Pi (or if you’re connecting remotely), type:
This will give you a list of your Raspberry Pi’s network IP addresses, but note that not all of them will work (that is not an error, and depends on how your network is set up).
If you’re unsure, you can use a smartphone app like Fing to search your network and identify the network IP address for your Pi. Alternatively, you can check your network router for the IP address assigned to your Raspberry Pi instead.
Replace localhost with the relevant IP address once you’ve identified it.
Apache’s installation procedure creates a very important folder called the Document Root directory. This is located at /var/www/html/.
This folder is your web server root directory. Any files shown here are exposed by the web server. For instance, /var/www/html/index.html would be the default page you’d see loaded when you visited http://localhost (or http://ip-address, replacing ip-address for your Pi’s own IP address).
Note that the directory already contains a file called index.html, however. If you want to see if your web server is working, try editing this file using nano or another file editor. Once the file is edited and saved, refresh the page — your web server should show the updated file.
You may need to use
sudo to override the default permissions to edit this file, as the files are protected for safety.
The next step, once Apache is installed, is to install PHP. As a major part of the LAMP stack, Raspberry Pi OS already includes easy-to-install packages that allow you to install and set up PHP with ease on your Pi.
To do this, open your terminal window (or remote SSH connection) and type:
sudo apt install php
This will install PHP, along with automatically configuring Apache once PHP is installed with the most common default options applied (which should be fine for most users). You don’t even need to restart Apache — this is handled for you.
Once you install PHP, all files with a .php extension will go through PHP first (just like Apache handles index.html automatically). A file called index.php, if found, is going to be the default PHP script run when you open the URL without giving a filename.
An important caveat is that, by default, .html files have a higher priority than .php files with the same name, so if you have both index.html and index.php, then index.html is going to be used and the PHP file will never work. This can be changed in the configuration, but a simpler solution is to simply ensure that there’s no index.html file.
This is probably the case if you’re creating more complex web apps, rather than basic websites.
If you want to test this, try creating a file named time.php in your /var/www/html/ directory that contains the following:
<?php echo date(“c”); ?>
Once saved, try opening http://localhost/time.php (or use the IP address, as described above).
If all is well, you should see the current date and time displayed in the browser, updating every time you refresh. This will confirm that PHP is working correctly on your Raspberry Pi.
The last step for our LAMP stack is to install both MySQL / MariaDB, and the PHP module to communicate with it. Raspberry Pi OS again helps us with pre-made packages that will install and configure things automatically.
To do this, open a terminal window (or remote SSH connection) and type the following to begin the installation:
sudo apt install mysql-server php-mysql
A note on terminology: you’ll actually be installing MariaDB here, which is backwards compatible with MySQL. The two projects (derived from the same code base) diverged for license reasons, with MariaDB remaining completely open-source and free for use.
Even though the package installation takes care of setting up MySQL / MariaDB and its PHP module, we need to set up a password for the root (administrator) user within the database itself. It’s good practice to do this, even if your system will never be accessible from the internet.
Once the installation is complete, type:
Follow the on-screen instructions that appear.
You’ll want to set up a new password, disable root access from the network, remove the anonymous account and the test database, and reload the privileges table. This is the final step: you’ll need to restart Apache to ensure that PHP can access your new database. To do this, run the following command:
sudo service apache2 force-reload
You shouldn’t see a response to this command — if you see nothing in the terminal, that means it worked. Head to http://localhost on your Pi (or use your Raspberry Pi’s IP address instead), where you should see your Raspberry Pi web server working and ready to use.
Your little Raspberry Pi is now ready to serve websites with a full LAMP stack. Each web app you use (e.g. WordPress) is a little different, so make sure to read the documentation that comes with any software you install to learn how to properly install it.
Do keep in mind that, in order for your website to be accessible by users outside your network, you may need to set up port forwarding on your router, or use IPv6 addressing if available.
You may need to install a web server to serve local web apps, too. A number of Raspberry Pi projects out there use the LAMP stack to make the Raspberry Pi accessible from a web browser, including OctoPrint for wireless 3D printing.