Do you want to create your own website but don’t need large-scale server-side processing? Wouldn’t it be easier if you could create, host and maintain a website directly on your Raspberry Pi? Here we’ll show you how to turn a Raspberry Pi into your own personal web server.
By the end of this tutorial, you will have learned how to install the popular Apache web server on your Raspberry Pi, set up PHP, and create a simple webpage that anyone can access over your local network.
What Is Apache Web Server?
Apache is one of the most popular web server applications in the world, accounting for almost 40 percent of all web servers at the time of writing.
Once you’ve set up Apache on your Raspberry Pi, you can use it to serve various files to anyone on the local network.
What you’ll need
To complete this tutorial, you’ll need:
- Raspberry Pi running Raspbian. If you don’t already have Raspbian, you can grab the latest version and flash it using Etcher.
- Power cable that’s compatible with your Raspberry Pi
- External keyboard and a way to attach it to your Raspberry Pi
- HDMI or micro HDMI cable, depending on your model of Raspberry Pi
- External monitor
- Ethernet cable or Wi-Fi connection
Update Your Raspbian
If you haven’t already, attach your external keyboard, monitor and any other peripherals to your Raspberry Pi, and then attach it to a power source.
Before we start, it is best to make sure your Raspbian is the latest version. Launch a Terminal window by clicking the little “Terminal” icon in the toolbar. Type the following commands into the Terminal:
If Raspbian does install one or more updates, reboot your Raspberry Pi by running the following command:
Once your Raspberry Pi reboots, it’ll be running the latest version of Raspbian.
Install the Apache Web Server
You’re ready to install the Apache2 package on our Raspberry Pi. In the Terminal, type the following command:
And that’s it: Your Raspberry Pi is now functioning as a basic web server!
To see your Apache web server in action, you’ll need to enter your Raspberry Pi’s IP address into a web browser. To retrieve this IP address, run the following command in the Terminal window:
This will return your Raspberry Pi’s IP address; simply enter this address into any web browser. You should see the following page.
Congratulations, you’ve just created your own web server!
Get Permission: Editing Apache’s HTML File
The “It works!” webpage is actually an HTML file that’s located in your Raspberry Pi’s “/var/www/html” folder.
To take a look at this file, open Raspbian’s File Manager app (by clicking the file icon in the toolbar) and then navigate to “/var/www/html.” This folder contains an “index.html” file, which is the page you’re seeing in your web browser.
In the following sections, you’ll make some simple changes to this file, and then create an additional HTML file that Apache will serve to anyone on your local network.
In a Terminal window, change directory (“cd”) so that it’s pointing at the ”index.html” file.
Now, run the following command:
The Terminal will now display some text explaining that the “index.html” file is owned by the “root” user.
Before you can edit this file, you’ll need to assume ownership. You can change ownership using a Terminal command. The following example assumes you’re using Raspbian’s “pi” username; if you manually changed it, make sure this is reflected in your Terminal command:
If you re-run the
ls -al command, you should see that “pi” now has permission to edit this file.
HTML: Customize Apache’s Webpage
You can now open the “It works” page for editing by running the following Terminal command:
This launches the “index.html” file in Raspbian’s Nano text editor.
You can change every part of this page’s code, but to keep things simple, the text that’s displayed as part of its heading has been changed in this example.
Once you’ve made your changes, save the file by pressing Ctrl + O, followed by Ctrl + X.
Now, load your Raspberry Pi’s IP address in your web browser, and you should see your changes.
Make Your Website Dynamic: Installing PHP 7
By default, the Apache web server is limited to static content, so your pages won’t react to any information that’s provided by users. If you want to make your content dynamic, you’ll need to install the latest version of PHP, which was PHP 7.4 at the time of writing.
In this section, you’ll install the latest version of PHP and the PHP module for Apache:
To test that PHP is set up correctly, you’ll create a PHP file in the “/var/www/html/” directory, then check that this file appears in our web browser.
To create a PHP file called “mywebpage.php,” run the following command in the Terminal window:
The “mywebpage.php” file opens automatically in Nano. In the Nano text editor, type the following PHP script:
This simple script retrieves today’s date and displays it as part of a webpage.
To save your script, press Ctrl + O, followed by Ctrl + X.
Test Your Dynamic PHP
To test that this PHP file is being served correctly, enter your Raspberry Pi’s IP address in your web browser, followed by “/mywebpage.php.” For example, if your IP address was 188.8.131.52, then you’d need to enter the following URL:
If the PHP file is being served correctly, then your browser should display something like the following image.
As you can see, it is easy to turn your Raspberry Pi into a web server, though you will need to set up a dynamic IP to be able to connect to your web server from a public network.