How To Set Up A Raspberry Pi Web Server

If you’re into web development, creating your own website serves as a nice way to flex your programming muscles. But let’s say you’ve finally created a functional website using all … Read more

Taylor Bell

Taylor Bell

Published on Apr 22, 2024

How To Set Up A Raspberry Pi Web Server

If you’re into web development, creating your own website serves as a nice way to flex your programming muscles. But let’s say you’ve finally created a functional website using all your coding skills and a ton of patience. The next logical step is to create a web server and host said website.

While you can use a cloud service provider like AWS for server-side administration, nothing beats running your very own web server. You don’t even need a bleeding-edge PC rocking the most expensive components; an ordinary Raspberry Pi is more than enough to set up a web server.

beagleboard sbc attached to drone

Related

Best Raspberry Pi alternatives

The Raspberry Pi is just one of many great single-board computer options available for a variety of projects.

What you’ll need

The choice of the Raspberry Pi model depends entirely on the complexity of your web server. For a simple LAMP server that displays a static HTML web page, you can use any old Raspberry Pi board. However, if you’re planning to render dynamic content on your website, then I suggest going with at least a Raspberry Pi 4 to avoid running into performance issues.

As for the microSD card, you can go for one with at least 8GB of space to install the OS and web server files. I also recommend installing the Raspberry Pi OS (64-bit) as it’s the most optimized operating system for the RPi boards. The overall procedure is the same as how you’d install Ubuntu on the SBC. Just be sure to download and flash the Raspberry Pi OS image instead of the Ubuntu ARM image.

  • A render of the Raspberry Pi 5

    Raspberry Pi 5

  • A render showing the SanDisk Ultra microSDXC card.

    SanDisk 256GB Ultra microSDXC card

Setting up the web server

After you’ve installed the Raspberry Pi OS, you’ll have to set up the necessary packages for this project.

1. Install the Apache web server by executing the following command:

sudo apt install apache2 -y
The command to install Apache

2. Install the MariaDB server with the help of this command:

sudo apt-get install mariadb-server -y
The command to install MariaDB

3. Install the PHP package alongside the Apache PHP module by running this command:

sudo apt install php libapache2-mod-php -y
The command to install PHP

4. Add the PHP-MySQL module to allow the web pages written in PHP to store and retrieve data from the MariaDB records:

sudo apt install php-mysql -y
The command to install PHP-MySQL module

While MariaDB and PHP are optional packages, I suggest installing them if you want your website to be more functional. By default, Apache comes with an index HTML file, but you’ll need to install the PHP packages to display dynamic content on your website. Similarly, you’ll require the MariaDB files if you want to save database records for your RPi web server.

Configuring the web server

With all the packages in place, it’s time to configure the website files inside the Apache web server.

1. Open the File Manager and head to the /var/www/html/ directory.

The location of project files in the Apache web server

2. Grant your current Raspberry Pi user the permission to modify the files in the html folder by running the chown command with these parameters:

sudo chown -R pi_user /var/www/html
The chown command in the Raspberry Pi OS terminal

Replace pi_user with the username you had set during the initial Raspberry Pi OS setup.

3. Paste all your project files inside this folder.

4. Start the Apache server with the help of this command:

sudo service apache2 start
The command to start the Apache server

Connecting to the website hosted on the Raspberry Pi

With Apache set up, you can view the website you just created on any device connected to your local network.

1. Find the IP address of your Raspberry Pi by running the ifconfig command.

ifconfig

The number next to the inet field is the IP address you’re looking for.

2. Switch to your PC and launch your favorite web browser.

3. Type the IP address of your Raspberry Pi.

The default index.HTML page

Alternatively, if you’re still on the Raspberry Pi, you can access the website using this URL instead:

localhost

(Optional) Accessing your web server outside your local network

So far, you can only access your Raspberry Pi-hosted website from your local network. If you want other users to be able to access your website, you’ll need to create port-forwarding rules for your Raspberry Pi. But first, you must set a static IP for your Raspberry Pi. To do so,

1. Open the terminal app once again.

2. Enter the following code to view the the router address:

ip r | grep default
The command to view the router address

3. Note the DNS address from the resolv.conf file.

sudo nano /etc/resolv.conf
The command to view the DNS address

4. Open the dhclient.conf file using the nano text editor:

sudo nano /etc/dhcp/dhclient.conf
The command to open the dhclient.conf file

5. Add the following lines to the file:

interface    

static ip_address=/24

static routers=

static domain_name_servers=

The new parameters in the dhclient.conf file

Be sure to replace all the parameter values with the addresses you discovered earlier. You can set the interface to wlan0 if you’ve connected your Raspberry Pi to Wi-Fi. Alternatively, you should enter eth0 if you’re using the Ethernet cable.

With that, you can enable port forwarding on your Raspberry Pi. The exact steps may vary depending on your ISP and router, but here’s the general procedure:

1. Log in to your router’s configuration page and open the Firewall or Security settings.

2. Inside, you should find the Port Forwarding tab.

The port forwarding settings in a router

3. Enter the public IP address of your Raspberry Pi as the Destination IP.

4. You’ll need to add the port number used for port forwarding.

For an HTTP web server, you’ll need to set port 80while an HTTPS server uses port 443.

You can check whether port forwarding was successful by turning off the Wi-Fi on any device and entering the public IP address of the Raspberry Pi as the URL.

Deploying a full-blown website using your tiny Raspberry Pi

If you followed all the steps properly, your website should be up and running, and even users outside your local network will be able to access it. While it’s a neat little project to test your coding skills, you can even deploy the website on the Internet by giving it a domain name with the help of a DNS provider.

But for those who despise programming, there are plenty of simple Raspberry projects that can be set up without typing a single block of code. So, you might want to check out our guide on turning your Raspberry Pi into an emulation machine or making a makeshift Android device from the SBC, instead.

A lifestyle image of the Raspberry Pi 5

Related

Raspberry Pi 5 review: The holy grail of DIY projects got even better (and rarer)

The Raspberry Pi 5 is one of the most powerful consumer-grade SBCs out there. Sadly, its limited stock means you’ll have a hard time finding one.

Partager cet article

Inscrivez-vous à notre newsletter