The Raspberry Pi series of bare-bones computers are awe-inspiring little beasts and almost perfect if you want to create your own server at home to serve web pages to the internet or host your own sets of sites and services for your own use.
However, if you're unsure where to start with your single-board computer, read on, as you will learn how to prepare your Raspberry Pi as an all-purpose server that can handle anything you throw at it.
What You Need to Set Up Your Raspberry Pi as a Server:
To complete this project, you will need the following items:
- A Raspberry Pi—preferably a model 4B
- A microSD card or SSD
- An Ethernet cable
- A static IP address
- A domain name—If you don't already have one, here are some useful tips on choosing a domain name
- Another PC
How to Install Raspberry Pi OS for a Server
Many distros are available for the Raspberry Pi, including Ubuntu, Manjaro, Apertis, and RetroPie. When setting your Pi up to serve content to the internet, we recommend Raspberry Pi OS Lite (64-bit), which is a port of Debian Bullseye, but without a desktop or any unnecessary frivolities. There's no need for a desktop because you won't be using a monitor.
First, insert your SD card into your desktop PC or laptop, or if you are using a USB SSD, plug it in now. Now, download the Raspberry Pi Imager tool and install it, then open it from the desktop or command line.
Imager will ask you to choose the operating system and storage. Click on Choose OS, then Raspberry Pi OS (other), then Raspberry Pi OS Lite (64-bit).
When you click on Choose Storage, you will be presented with a list of all storage devices attached to your PC. Select the drive where you want to install the OS, and you will return to imager's main screen.
Click on the cog at the lower-right corner of the screen to open a configuration menu. You will now set the necessary options to connect to your Pi over SSH.
Check the boxes for Enable SSH, Set username and password, and Set locale settings. Fill in your preferred username and password, and set the locale to your time zone and keyboard layout (although you won't be using a keyboard directly attached to the Pi).
Hit Save and then Write. Raspberry Pi OS will now be written to your storage medium of choice—this may take a while.
Power Up 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 192.168.1.1 into your browser's address bar. Check your router's instruction manual for details if this doesn't work.
Your router admin page should show devices connected via Wi-Fi separately from those connected by Ethernet cable. The IP address of your Raspberry Pi should be shown nearby. If it isn't, 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 the Raspberry Pi down, reboot the router, and then go on vacation for a week. When you return, it will still have the same IP address.
Connect to Your Raspberry Pi Over SSH
Now that you know your Raspberry Pi's local IP address, you can connect to it over Secure Shell (SSH) using PuTTY on Windows and macOS or through a terminal on Linux.
ssh user@local.pi.ip.address
On your first connection, you will get a warning that "The authenticity of the host can't be established" and asked if you want to continue connecting. Type the word yes and hit Return.
You are now logged into your Raspberry Pi and have complete 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 ensure you can access it from the internet. Open up your router's admin page and find a section titled either Port Forwarding, Port Mapping, or Port Management, then create two new entries.
The first is for HTTP (insecure) traffic. Set both the local and public port to 80, and the local IP address to the IP address of your Raspberry Pi.
The second is for HTTPS (secure) traffic. Set both the local and public port to 443, while keeping the local IP address to the IP address of your Raspberry Pi.
Essential Server Software for Your Raspberry Pi
Your Raspberry Pi needs to be able to handle any server-related software you care to deploy, and for it to do so, you will need to install some essential software first.
The software tools you will need to install to make sure everything runs smoothly in the future include the following:
- Apache: a web server and reverse proxy.
- MariaDB: a MySQL database.
- PHP: a scripting language geared toward the web.
- Docker: an open-source containerization platform.
- Docker-compose: a tool to simplify managing Docker containers.
- Certbot: handles retrieving and installing SSL keys and certificates from Let's Encrypt.
First, update and upgrade the packages:
sudo apt update
sudo apt upgrade
Install Apache by typing the following:
sudo apt install apache2
Modules extend Apache's functionality. You can install some common and useful ones with:
sudo a2enmod rewrite http2 proxy proxy_http proxy_http2 proxy_wstunnel
Now, start and enable 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 installation page:
This means requests to your router on port 80 are being successfully forwarded to your Raspberry Pi and Apache is running as intended.
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
Hit Return when prompted for a root password, and choose no when asked if you want to "switch to unix_socket authentication."
Again, choose no when prompted to "change the root password" and yes to "remove anonymous users." Also, choose yes to "disallow root login remotely" and yes to "remove test database and access to it."
Now, reload privilege tables when prompted, and the secure installation will complete with a success message.
You will be able to access MariaDB with the following command:
sudo mariadb
Now, install Docker by typing the following:
sudoaptinstalldocker.io
Start and enable Docker:
sudo systemctl start docker
sudo systemctl enable docker
Add your user to the Docker group:
sudo usermod -aG docker your_username
You will need to log out and back in again for your membership to take effect.
Docker Compose makes it easy to orchestrate your Docker containers, and you'll find it useful in many Raspberry Pi server projects. While Docker Compose is available in the default repositories, this version is rarely up-to-date.
Visit the Docker Compose releases page, and copy the link for docker-compose-linux-aarch64. Back in the terminal, use the wget command to download the binary. For instance:
wget https://github.com/docker/compose/releases/download/v2.19.1/docker-compose-linux-aarch64
Rename the downloaded file, move it into a location in your PATH, and make it executable:
sudo mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Finally, install software-properties-common, update, then add the repository for Certbot:
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 the prerequisite software to allow your Raspberry Pi to securely display almost any kind of content, regardless of the deployment method. Plus, you can conveniently 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, streaming media server, or online office suite. So, spend some time thinking about what sites and services you want to run from your Raspberry Pi.