How to configure your Raspberry Pi as a web server

The Raspberry Pi series of barebones computers are impressive, near-perfect little beasts if you want to build your own server at home to serve web pages on the Internet or host your own sets of sites and services for your own use.

However, if you’re not sure where to start with your single board computer, read below as you’ll learn how to prepare your Raspberry Pi as a versatile server that can handle anything you throw at it.

What you need to configure your Raspberry Pi as a server:

To complete this project, you will need the following items:

How to Install Raspberry Pi OS for a Server

Many distributions are available for the Raspberry Pi, including Ubuntu, Manjaro, Apertis, and RetroPi. When setting up your Pi to stream content over the internet, we recommend Raspberry Pi OS Lite (64-bit), which is a port of Debian Bullseye, but without the desktop and unnecessary frills. There’s no need for a desktop computer because you won’t be using a monitor.

First, insert your SD card into your desktop or laptop, or if you’re using a USB SSD, plug it in now. Now download the Raspberry Pi Imager tool and install it, then open it from desktop or command line.

Imager will ask you to choose the operating system and storage. Click on Choose the operating systemthen Raspberry Pi operating system (other)then Raspberry Pi OS Lite (64 bit).

When you click Choose storage, a list of all storage devices connected to your PC will be displayed. Select the drive where you want to install the operating system and you will be returned to the main imager screen.

Click on the cog in the lower right corner of the screen to open a configuration menu. You will now set the options needed to connect to your Pi via SSH.

Check the boxes for Enable SSH, Set username and passwordand Set Regional Settings. Fill in your preferred username and password, and set the locale to your timezone and keyboard layout (although you’re not using a keyboard directly connected to the Pi).

Hit to safeguard so what To write. Raspberry Pi OS will now be written to your storage medium of choice, this may take some time.

Turn on the Raspberry Pi and find it on your local network

Insert the SD card into your Raspberry Pi’s SD card slot or, if using USB storage, plug it into one of the available USB ports. Connect the Raspberry Pi to a power source, and via an Ethernet cable, to the router.

To connect to your Raspberry Pi, you need to know its IP address, open a browser on a machine that’s on the same local network, and navigate to your router’s admin page. You can usually do this by typing into your browser’s address bar. Consult your router’s instruction manual for details if this does not work.

Your router’s admin page should show devices connected via Wi-Fi separately from those connected via Ethernet cable. Your Raspberry Pi’s IP address should be displayed nearby. If not, hovering over the IP address label should produce a tooltip revealing the address – write it down.

One of the advantages of using a wired connection to your router rather than a Wi-Fi connection is that the local IP address will not change. You can shut down the Raspberry Pi, reboot the router, then go on vacation for a week. When you return, it will still have the same IP address.

Connect to your Raspberry Pi via SSH

Now that you know the local IP address of your Raspberry Pi, you can connect to it via Secure Shell (SSH) using PuTTY on Windows and macOS or via terminal on Linux.

ssh user@local.pi.ip.address

When you connect for the first time, you will receive a warning “The authenticity of the host cannot be established” and ask you if you want to continue connecting. Type the word yes and press return.

You are now connected to your Raspberry Pi and have full control over the system.

Port forwarding to expose your Raspberry Pi to the internet

If you want your Raspberry Pi to become a web server, you need to make sure you can access it from the internet.

Open your router’s administration page and find a section titled either Port forwarding, Port mappingWhere Port managementthen create two new entries.

The first is for HTTP traffic (not secure). Set the local and public port to 80and the local IP address to your Raspberry Pi’s IP address.

The second is for HTTPS traffic (secure). Set the local and public port to 443while keeping the IP address local to your Raspberry Pi’s IP address.

Essential server software for your Raspberry Pi

Your Raspberry Pi needs to be able to handle all of the server-related software you want to deploy, and to do that you’ll need to install some essential software first.

The software tools you will need to install to ensure everything is working properly in the future include the following:

  • Apache: a web server and a reverse proxy.
  • MariaDB: a MySQL database.
  • PHP: a web-oriented scripting language.
  • Docker: an open-source containerization platform.
  • docker-compose: a tool to simplify the management of Docker containers.
  • Certbot: handles the retrieval and installation of Let’s Encrypt SSL keys and certificates.

First, update and upgrade the packages

sudo apt update
sudo apt upgrade

Install Apache by typing the following:

sudo apt install apache2

Now start and activate Apache with the following command:

sudo systemctl start apache2
sudo systemctl enable apache2

Visit your public IP address in a browser and you should see the default Apache install page:

This means that requests to your router on port 80 are successfully forwarded to your Raspberry Pi and Apache is working as expected.

Install PHP by typing the line of code below:

sudo apt install php

Next, install MariaDB using the command line below:

sudo apt install mariadb-server

Now type the following:

sudo mysql_secure_installation

Press return when prompted for a root password and choose Nope when asked if you want to “switch to unix_socket authentication”.

Again, choose Nope when prompted to “change root password” and yes to “remove anonymous users”.

Also, choose yes to “disallow remote root login” and yes to “delete the test database and access it”.

Now reload the privilege tables when prompted, and the secure install will complete with a success message.

You can access MariaDB with the following command:

sudo mariadb

Now install Docker by typing the following:

sudo apt install

Start and activate Docker:

sudo systemctl start docker
sudo systemctl enable docker

Install software-properties-common, update, then add repository for docker-compose

sudo apt install software-properties-common
sudo apt update
sudo add-apt-repository ppa:certbot/certbot

Now install Certbot:

sudo apt-get install python3-certbot-apache

Your Raspberry Pi is now ready to act as a server!

Congratulations, you have installed all prerequisite software to enable your Raspberry Pi to safely display almost any type of content, regardless of deployment method. Moreover, you can easily access it from the Internet.

You are in the enviable position of being able to host everything from a simple static page to a WordPress site, a streaming media server or an online office suite. So spend some time thinking about the sites and services you want to run from your Raspberry Pi.