We have you covered for everything from the whole setup process to using your Raspberry Pi as a web server.
Amazingly powerful tiny machines, the Raspberry Pi series of barebone computers are practically ideal if you want to build your own home server to run your own networks of websites and services or serve web pages to the public internet.
Read on to discover how to set up your Raspberry Pi as an all-purpose server that can handle everything if you don't know where to start with your single-board computer.
What You Need to Set Up Your Raspberry Pi as a Server:
You will need the following items to complete this project:
- A Raspberry Pi—preferably a model 4B
- Another PC
- An SD card or SSD
- An Ethernet cable
- A domain name
- A static IP address
How to Install Raspberry Pi OS for a Server
For the Raspberry Pi, a variety of distributions are available, including Ubuntu, Manjaro, Apertis, and RetroPi. We advise using Raspberry Pi OS Lite (64-bit), which is a port of Debian Bullseye but devoid of a desktop and other superfluous extras when setting up your Pi to serve content to the internet. You won't be using a display, so you don't need a desktop.
Then, put your SD card into your laptop or desktop computer. Or, if you're using a USB SSD, plug it in right away. Install the Raspberry Pi Imager tool after downloading it, and then launch it from the desktop or command prompt.
You will be prompted by Imager to choose your operating system and storage. Select OS, then Raspberry Pi OS (other), and finally Raspberry Pi OS Lite (64-bit).
When you click “Select storage“, a list of all the storage devices connected to your computer will appear. You will be sent back to the imager's home screen after choosing the drive on which you wish to install the OS.
To access the setup menu, click the cog in the lower-right corner of the screen. You will now configure the required settings to connect to your Raspberry Pi using SSH.
Choose the checkboxes next to “Enable SSH“, “Set username and password”, and “Set locale settings“. While you won't be using a keyboard that is physically connected to the Raspberry Pi, enter your desired username and password and set the locale to your time zone and keyboard layout.
Click Save, then click Write. The Raspberry Pi OS will now be written to your preferred storage medium; this process might take some time.
Power Up the Raspberry Pi and Find It on Your Local Network
If using USB storage, place the SD card in one of the USB ports on your Raspberry Pi or insert it into the SD card slot. Attach the Ethernet wire to the router and the Raspberry Pi to a power source.
You must be aware of your Raspberry Pi's IP address to connect to it. Go to your router's admin page via a browser on a device connected to the same local network. Typically, you may accomplish this by entering 192.168.1.1 into the address bar of your browser. If this doesn't work, look for further information in your router's instruction manual.
Devices connected through Wi-Fi and those connected via Ethernet cable should be shown separately on your router's admin page. The IP address of your Raspberry Pi should be displayed nearby. If not, moving your mouse pointer over the IP address label should display a tooltip with the address; note it down.
The local IP address won't change if you connect to your router through a wired connection as opposed to a wireless one, which is one benefit. You can turn off the Raspberry Pi, restart your router, and then take a weeklong holiday. It will still have the same IP address when you come back.
Connect to Your Raspberry Pi Over SSH
Knowing your Raspberry Pi's local IP address allows you to over a Secure Shell (SSH) connection with it using PuTTY on Windows, macOS, or a terminal on Linux.
You will be prompted to continue connecting after receiving a warning that “the authenticity of the host can't be determined” during your first connection. Just enter “yes” and press “return“.
You are in full command of the Raspberry Pi at this point because you are logged in.
Port Forwarding to Expose Your Raspberry Pi to the Internet
You must make sure you can access your Raspberry Pi from the internet if you want it to function as a web server.
Find a section titled either Port Forwarding, Port Mapping, or Port Management on the admin page of your router, then add two new entries there.
The first is for HTTP traffic, which is not secure. Set the local IP address to the IP address of your Raspberry Pi, and the public and local ports to 80.
For HTTPS (secure) traffic, use the second. Keep the local IP address set to the IP address of your Raspberry Pi while setting the local and public ports to 443 each.
Essential Server Software for Your Raspberry Pi
Your Raspberry Pi must be able to handle any server-related software you choose to deploy, and for it to do so, you must first install the certain necessary software.
The following software tools will need to be installed to ensure that everything runs smoothly in the future:
- 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.
To begin, you must update and upgrade the packages.
sudo apt update
sudo apt upgrade
Install Apache by typing the following:
sudo apt install apache2
Now, run the following command to start and enable Apache:
sudo systemctl start apache2
sudo systemctl enable apache2
With a browser, go to your public IP address, and you should see the default Apache installation page:
This indicates that Apache is working correctly and that requests to your router on port 80 are being forwarded to your Raspberry Pi.
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:
When asked for a root password, press return. When prompted to “switch to unix_socket authentication”, choose “no“.
When prompted to “change the root password” and “remove anonymous users,” once again choose “no“.
Selecting “yes” will also erase the test database and all access to it as well as “disallow root login remotely”.
Now, the secure installation will finish successfully and display a success message once you reload the privilege tables when prompted.
The following command will let you access MariaDB:
Now, install Docker by typing the following:
sudo apt install docker.io
Start and enable docker:
sudo systemctl start docker
sudo systemctl enable docker
Install software-properties-common, then update before adding the Docker-compose repository.
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! Regardless of the deployment method, you have all the necessary software installed on your Raspberry Pi to enable secure viewing of almost any kind of information. Also, it is easily accessible through the internet.
Being able to host anything, from a simple static page to a WordPress site, streaming video server, or online office suite, is an admirable position to be in. Hence, take some time to consider the websites and services that you want to operate on your Raspberry Pi.