Raspberry Pi is not only a small board computer but a tinker card that can do almost anything and everything. Do you want to set up 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 that 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.
Set Up a Raspberry Pi Web Server (2021)
To set up a Raspberry Pi web server, you first need to install a web server. There are two popular web servers: Apache and Nginx. But in this tutorial, we will use Apache because it’s reliable and easier to use. Apart from that, we will also install PHP so that you can host dynamic webpages on your Raspberry Pi. Now having said all of that, let’s jump to the steps.
Install Apache Web Server on Raspberry Pi
1. First off, make sure you have flashed Raspberry Pi OS with desktop UI on the SD card. If you are new to all this, follow our guide on how to set up Raspberry Pi remotely. That said, if you have an external monitor, things will be much smoother.
2. Once Raspbian OS boots up, open the Terminal and run the below command to update Raspbian OS to the latest version.
sudo apt-get update && sudo apt-get upgrade -y
3. Next, we need to install the Apache web server on Raspberry Pi. Run the below command in the Terminal.
sudo apt install apache2 -y
4. Once installed, the Apache web server will be live. Just open the browser on Raspberry Pi and enter
127.0.0.1, and hit enter to access the web server. It should load Apache’s HTML page.
5. You can also directly enter the IP address of Raspberry Pi in a browser to access the web server. 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 there you have 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 am able to access Raspberry Pi’s web server from my Chromebook with ease.
7. For your information, all web server files are saved in
/var/www/html/ location. So you can change the directory to this location and view all the files. Here are the commands to run.
cd /var/www/html/ ls -al
8. As you can see in the screenshot above, the
index.html file is owned by
root. So before modifying anything, you need to change the owner to
pi (you). Run the below command to change the ownership.
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 the Terminal or File Manager. I am using the File Manager for easier access. Navigate to “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, then they will reflect on the web server right away. You will see the updated title when you open the web server in a browser window. This step confirms that your Raspberry Pi web server is working well.
Install PHP on Raspberry Pi
If you want to test your web pages on Raspberry Pi, installing just the webserver won’t cut it. You also need to 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 below command to install the latest version of PHP on your Raspberry Pi.
sudo apt install php libapache2-mod-php -y
2. Next, move to the same
var/www/html/ directory and create a PHP file using a text editor.
3. Next, open the file with a text editor and enter the PHP code you want to add. For example, I have added the PHP code that outputs a simple statement.
Testing FileZilla on Raspberry Pi
Once we have set up Apache web server and PHP on Raspberry Pi, it’s finally time to test if we can transfer our existing web assets to the RPi web server with the popular FTP client FileZilla. You can also use other FTP clients like WinSCP if you wish. 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, move to “Interfaces” and enable “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 Raspberry Pi’s web server in the “Host” field. Keep the “Port” field blank. After that, enter the default credentials:
pi as the username and
raspberry as the password. Now, click on “Connect”.
Note: If you changed the password of Raspberry Pi during the setup, then enter the new password.
4. You will connect to your Raspberry Pi’s web server. Now, you can transfer all your HTML, CSS, and PHP assets directly to Raspberry Pi and get your web development underway without any hassle.
Turn Your Raspberry Pi into a Web Server in a Few Easy Steps
So that is how you can set up a web server on Raspberry Pi. All the tools required to get a web server running are available on the Debian-based Raspbian OS, so there is no problem at all. 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, that is all from us. If you have any questions, let us know in the comment section below.