How to Turn Your Raspberry Pi into a Personal Web Server

Apache Web Server Raspberry Pi

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.

Also read: How to Turn Your Raspberry Pi into a Wireless Access Point

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:

sudo apt-get update
sudo apt-get upgrade

If Raspbian does install one or more updates, reboot your Raspberry Pi by running the following command:

reboot

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:

sudo apt install apache2 -y

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:

hostname -I

This will return your Raspberry Pi’s IP address; simply enter this address into any web browser. You should see the following page.

Enter your Raspberry Pi's IP address and you should see Apache's default webpage.

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.

cd /var/www/html

Now, run the following command:

ls -al

The Terminal will now display some text explaining that the “index.html” file is owned by the “root” user.

By default, you don't have permission to edit the server's index.html file.

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:

sudo chown pi: index.html

If you re-run the ls -al command, you should see that “pi” now has permission to edit this file.

We've added "Pi" as the owner of this directory.

HTML: Customize Apache’s Webpage

You can now open the “It works” page for editing by running the following Terminal command:

nano index.html

This launches the “index.html” file in Raspbian’s Nano text editor.

You can edit the default HTML page 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.

You can edit any part of this page's HTML, including adding and removing text, and other UI elements.

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.

Reload the webpage; your changes should now be visible.

Also read: How to Configure Apache and PHP for High Traffic Websites on Linux Server

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:

sudo apt install php libapache2-mod-php -y

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:

sudo nano /var/www/html/mywebpage.php

The “mywebpage.php” file opens automatically in Nano. In the Nano text editor, type the following PHP script:

<?php
echo "Today is " . date('Y-m-d H:i:s');

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 190.100.1.100, then you’d need to enter the following URL:

http://190.100.1.100/mywebpage.php

If the PHP file is being served correctly, then your browser should display something like the following image.

Now we've setup PHP, our server can display dynamic content.

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.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Jessica Thornsby

Jessica Thornsby is a technical writer based in Derbyshire, UK. When she isn’t obsessing over all things tech, she enjoys researching her family tree, and spending far too much time with her house rabbits.