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. Would you like 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, he does too. In fact, using a Raspberry Pi is a great and affordable way to build 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 be using 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 of that has been said, let’s move on to the steps.
Install Apache web server on Raspberry Pi
1. First of all, make sure you have flashed Raspberry Pi OS with Desktop Computer User interface on SD card. If you’re new to all of this, follow our guide on how to set up Raspberry Pi remotely. That said, if you have an external monitor, things will be a lot smoother.
2. Once the Raspbian operating system has started, open the terminal and run the command below to update Raspbian operating system to the latest version.
sudo apt-get update && sudo apt-get upgrade -y
3. Then we have to install the Apache web server on Raspberry Pi. Run the command below in the terminal.
sudo apt install apache2 -y
4. Once installed, the Apache web server will be operational. Simply open the browser on Raspberry Pi and enter
127.0.0.1, and press Enter to access the web server. It should load the Apache HTML page.
5. You can also directly enter the IP address of the 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.
6. Enter the IP address in the browser, and that’s it. In fact, you can use the 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.
7. For your information, all web server files are saved in
/var/www/html/ site. So you can change the directory to this location and view all files. Here are the commands to execute.
cd /var/www/html/ ls -al
8. As you can see in the screenshot above, the
index.html the file belongs to
root. So before you change 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 the ownership of folders such as
9. Now go ahead and open the file through terminal or file manager. I am using the file manager for easier access. Move towards “var -> www -> HTML”, right click on the âindex.htmlâ file and choose âText Editorâ.
10. Now if I make any changes to the title and save it, they 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.
Install PHP on Raspberry Pi
If you want to test your web pages on Raspberry Pi, installing only the web server will not shut it down. 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 go about 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
2. Then go to the same
var/www/html/ directory and create a php file using a text editor.
3. Then open the file with a text editor and enter PHP code you want to add. For example, I added the PHP code which generates a simple instruction.
Test FileZilla on Raspberry Pi
Once we have configured the Apache and PHP web server on Raspberry Pi, it is finally time to test if we can transfer our existing web assets 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 (To free) on another PC connected to the same Wi-Fi network.
2. Next, on Raspberry Pi, go to the âStart Menu -> Preferences -> Raspberry Pi Configurationâ section. Here go to “Interfaces” and activate “SSH”.
3. On FileZilla, open “File Menu -> Site Manager” and add a “New Site”. Here, choose “SFTP” as the protocol and enter the IP address of the Raspberry Pi web server in the “Host” field. Leave the “Port” field empty. After that enter the default credentials:
pi as username and
raspberry as password. Now click on “Connect”.
To note: If you changed the Raspberry Pi password during setup, enter the new password.
4. You will connect to the web server of your Raspberry Pi. Now you can transfer all your HTML, CSS and PHP assets directly on Raspberry Pi and launch your web development without any problem.
Turn your Raspberry Pi into a web server in a few easy steps
This is how you can set up a web server on Raspberry Pi. All the tools required to run a web server are available on the Debian based Raspbian operating system, so there is no problem. Even if you are a beginner, you can follow the step by step instructions above and turn the RPi card into a web server in no time. Anyway, it all comes from us. If you have any questions, let us know in the comments section below.