Host a web server on a Raspberry Pi

There are many reasons why it can be an advantage to host a website on your own server. For instance, you might only want to try a few designs or start out with a small page and are not ready to commit to monthly payments to a commercial host. Or perhaps you’re working on several web applications at once and have noticed that it is impossible to develop them all without having to shell out a substantial monthly fee. You might also not have the required infrastructure readily available to you.

Another reason is of course the dependence on external providers. You have to be willing to hand over your data to them, often with limited insights into security, data storage and privacy policies on their platforms.

A word on security.

Self-hosting fixes these issues but comes with its own risks, depending on the experience of the administrator. Security is certainly a big consideration. Your webserver might be exposed and more vulnerable to external attacks. I will only cover basic security considerations in this post, as there are many helpful guides out there that can teach you how to harden your system for more professional endeavours.

Please keep in mind that it is highly recommended to keep your server and associated software up to date and secure it wherever possible (some suggestions below). If you are just starting out and only planning to run a development platform or a small website, the tutorial below should be helpful for you.

In the first part of this guide, we will install a web server using Raspberry Pi and Apache as the web server. Alternatively, you can also install nginx instead of Apache (nginx usually provides better performance for high-traffic websites).

The second part of this post will explain how to install WordPress on your server, connect it to a domain name and get a basic website up and running.

Why using Raspberry Pi?

The main advantage of the Raspberry Pi is that it is low-cost, both in terms of purchase costs as well as electricity demands. Users on reddit have calculated that running a Raspberry Pi on full performance 24/7 only comes to about 10 pounds a year. This estimate certainly depends on local electricity costs, but overall should be pretty doable.

At the same time, your web server will be able to provide adequate performance for a standard, low-traffic website. A blog should certainly run on it without performance issues.

Finally, as you will see below, web server and website are fairly easy to set up, as long as you are not afraid of using the command line.

What you need

For my development web server I am using a Raspberry Pi 4 with 8 GB RAM and an SSD drive instead of an SD card. But it is certainly possible to run the server on lower specs and from an SD card if you’re not planning on hosting something too demanding.

  • Raspberry Pi, choose the latest version for best performance
  • SD card with Raspbian installed and SSH active (how to enable SSH)
  • Modem/router and an Ethernet connection

Step 1: Access and update the Raspberry Pi

In this guide, we will be using the Raspberry Pi headless. This means that we don’t need to connect it to a monitor. We will instead access it via SSH from a different computer and set everything up remotely.

If you prefer to connect your RPi to a monitor and use the user interface, ensure that a full version of Raspbian is installed and skip the instructions to access SSH.

Boot the Raspberry Pi and ensure it is connected to the router with the Ethernet cable. Access your router admin panel to find the IP assigned to your Raspberry Pi – the admin page can usually be found by typing 192.168.0.1 or your router’s IP into a browser. Should you have trouble finding the IP, the following command typed into your Raspberry Pi’s terminal will also show it:

sudo ifconfig

From the terminal of the second computer, access the Raspberry Pi using SSH and its IP. I am going to use 192.168.0.555 as an example. On Linux and Macs SSH is already installed, you can use Putty on Windows.

ssh pi@192.168.0.555

You will be prompted for a password, which is by default “raspberry”. At this point it is highly recommended to change it into something more secure.

Next, update the system and reboot.

sudo apt-get update
sudo apt-get upgrade
sudo reboot

Step 2: Install a LAMP (Linux, Apache, MySQL, PHP) stack

A LAMP stack is the most common configuration for self-hosted web servers. It is reasonably easy to set up, the components are readily available and give you enough flexibility and functionality to run a basic website.

Apache

Install Apache with the following commands.

sudo apt-get install apache2 -y
sudo service apache2 restart

Once the installation is complete and Apache has been restarted as a service, check whether it is running by typing the command below. You should get a message that the service is active (running) in green.

sudo service apache2 status

If everything is running as expected, you should now be able to access your web server locally from your computer’s browser. Type the IP address of your Raspberry Pi (or http://localhost) into your browser and you should see a basic website with the title “It Works!”. This website comes pre-installed with the Apache software and is only for test purposes.

Ensure to enter your IP address following a http:// as we do not have a secure HTTPS connection set up yet.

http:// 192.167.0.555

We now need to configure Apache with a few more lines of code. The a2enmod command will enable the mod_rewrite module for Apache, which we will need later. The following lines set the appropriate user permissions for the main content folder. We end with another restart of the apache2 server.

sudo a2enmod rewrite
sudo systemctl restart apache2
sudo chown -R pi:www-data /var/www/html/
sudo chmod -R 770 /var/www/html/
sudo service apache2 restart

PHP

After this step we can install PHP, which is comparably easy and completed with the following command:

sudo apt-get install php libapache2-mod-php -y

MariaDB

Now we are ready to install the MySQL database – or rather MariaDB, which is an open-source fork of MySQL and available for the Raspberry Pi.

The following commands will install MariaDB for you, after which you should restart Apache. You can then proceed with the secure installation.

sudo apt install mysql-server php-mysql -y
sudo service apache2 restart
sudo mysql_secure_installation

You will now be asked to enter the current password for root (default is blank): press Enter. Set a new root password and write it down somewhere so that you don’t forget.

All following questions should be answered with Yes to ensure the highest level of security. After you’re done, a welcome message will thank you for using MariaDB.

You can now access MariaDB with the following command. Enter the root password you specified above.

sudo mysql -uroot -p

Next create the database you are going to use for your website. This step is required for the installation of WordPress, for example. If you are only setting up a simple html page you can skip this step.

create database YOURDATABASENAME;

Grant the appropriate privileges to your database and user and confirm.

GRANT ALL PRIVILEGES ON YOURDATABASENAME.* TO 'root'@'localhost' IDENTIFIED BY 'YOURROOTPASSWORD';
FLUSH PRIVILEGES;

Congratulations, you are now done with the LAMP setup.

For now your website is only accessible on your local network. Follow the instructions below to get access to the index.html front page and edit it as needed.

Step 3: Change the default webpage

The default website is located in the folder: /var/www/html/. This folder is the standard Apache web directory specified during the install.

Navigate to the html folder and edit the index.html file as you see fit. In the post that explains how to setup WordPress we will use a different location for the blog files.

sudo nano /var/www/html/index.html

So far so good. Enjoy your first self-hosted website!

When you are ready for the next step, read this post here. It describes how to setup a WordPress page on your web server.

3 thoughts on “Host a web server on a Raspberry Pi”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: