How to Set Up a Raspberry Pi Web Server in 2021 [Guide]

Raspberry Pi is not just a small on-board computer, but a DIY board that can do almost anything and everything. Do you want to configure Pi-hole on Raspberry Pi to block ads and trackers from your entire home network? Yes, it does that. Do you want to set up a Raspberry Pi web server for web development and local file transfer? Well, it does too. In fact, using a Raspberry Pi is a great and affordable way to create a personal web server. So if you are interested, follow this simple guide and turn your Raspberry Pi into a web server in no time.

Configure a Raspberry Pi Web Server (2021)

To set up a Raspberry Pi web server, you must first install a web server. There are two popular web servers: Apache and Nginx. But in this tutorial, we will use Apache because it is reliable and easier to use. Apart from that, we will also install PHP so that you can host dynamic web pages on your Raspberry Pi. Now that all that has been said, let’s get to the steps.

  • Install Apache Web Server on Raspberry Pi

1. First, make sure you have flashed Raspberry Pi operating system with desktop UI on SD card. If you’re new to all of this, follow our guide to setting up a remote Raspberry Pi. That said, if you have an external monitor, things will run a lot smoother.

2. Once Raspbian OS is booted, open terminal and run below command to update Raspbian operating system to the latest version.

sudo apt-get update && sudo apt-get upgrade -y

3. Next we need to install apache web server on Raspberry Pi. Run the below command in terminal.

sudo apt install apache2 -y

Configure a Raspberry Pi Web Server (2021)

4. Once installed, the Apache web server will be up and running. Just open the browser on Raspberry Pi and enter localhost Where 127.0.0.1, and press Enter to access the web server. It should load Apache’s HTML page.

Configure a Raspberry Pi Web Server (2021)

5. You can also directly enter the IP address of Raspberry Pi in a browser to access the web server. To run hostname -I in the Terminal, and you will find the IP address of your RPi.

Configure a Raspberry Pi Web Server (2021)

6. Enter the IP address in the browser, and that’s it. In fact, you can use IP address on any device on your local Wi-Fi network and you will be able to access the web server. For example, I can easily access the Raspberry Pi web server from my Chromebook.

Configure a Raspberry Pi Web Server (2021)

7. For your information, all web server files are stored in /var/www/html/ site. So you can change the directory to that location and view all files. Here are the commands to run.

cd /var/www/html/
ls -al

Configure a Raspberry Pi Web Server (2021)

8. As you can see in the screenshot above, the index.html the file belongs to root. So before modifying anything, you need to change the owner to pi (you). Run the command below to change owner.

sudo chown pi: index.html
ls -al

As you can see in the screenshot below, pi is now the owner of the web server. You can also use the same format to change ownership of folders such as html and www.

change owner

9. Now go ahead and open the file via terminal or file manager. I use the file manager for easier access. Move towards “var -> www -> HTML”right-click on the “index.html” file and choose “Text Editor”.

raspbian

10. Now if I make changes to the title and save it, it will be reflected on the web server immediately. You will see the updated title when you open the web server in a browser the window. This step confirms that your Raspberry Pi web server is working fine.

Configure a Raspberry Pi Web Server (2021)

  • Install PHP on Raspberry Pi

If you want to test your web pages on Raspberry Pi, just installing the web server will not be enough. You must also install PHP to have a solution like XAMPP on Raspberry Pi. It will allow you to test dynamic web pages on your Raspberry Pi. Here is how to do it.

1. Run the command below to install the latest version of PHP on your Raspberry Pi.

sudo apt install php libapache2-mod-php -y

Install PHP on raspberry pi

2. Then switch to the same var/www/html/ directory and create a PHP file using a text editor.

Install PHP on raspberry pi

3. Then open the file with a text editor and enter PHP code you want to add. For example, I added PHP code that generates a simple statement.

Install PHP on raspberry pi

  • Test FileZilla on Raspberry Pi

Once we’ve configured the Apache web server and PHP on the Raspberry Pi, it’s finally time to test if we can transfer our existing web resources to the RPi web server with the popular FileZilla FTP client. You can also use other FTP clients like WinSCP if you want. Here are the steps to follow.

1. Install FileZilla (Free) on another PC connected to the same Wi-Fi network.

2. Next, on Raspberry Pi, navigate to the “Start Menu -> Preferences -> Raspberry Pi Configuration” section. Here, switch to “Interfaces” and enable “SSH”.

Test FileZilla on Raspberry Pi

3. On FileZilla, open “File Menu -> Site Manager” and add a “New Site”. Right here, choose “SFTP” as the protocol and enter the IP address of the Raspberry Pi’s web server in the “Host” field. Leave the “Port” field empty. After that, enter the default credentials: pi as username and raspberry as a password. Now click on “Connect”.

To note: If you changed the Raspberry Pi password during setup, enter the new password.

Test FileZilla on Raspberry Pi

4. You will connect to your Raspberry Pi’s web server. Now you can transfer all your HTML, CSS and PHP assets directly on Raspberry Pi and start your web development without any hassle.

Test FileZilla on Raspberry Pi

Turn your Raspberry Pi into a web server in a few easy steps

That’s how you can set up a web server on Raspberry Pi. All the tools needed to run a web server are available on the Debian-based Raspbian operating system, so there’s no problem. Even if you are a beginner, you can follow the step-by-step instructions above and turn the RPi board into a web server in no time. Anyway, it all comes from us. If you have any questions, let us know in the comment section below.