Want to build your own website but don't need large-scale server-side processing? Wouldn't it be easier to create, host and maintain a website right on your Raspberry Pi? Here we'll show you how to turn a Raspberry Pi into your own personal web server.ContentsWhat is Apache Web Server?What You'll NeedUpdate your Raspbian Install Apache Web ServerGet Permission:Edit Apache HTML FileHTML:Customize Apache Web PageMake Your Website Dynamic:Install PHP 7Test your dynamic PHP
By the end of this tutorial, you will have learned how to install the popular Apache web server on your Raspberry Pi, configure PHP, and create a simple web page that anyone can access through your local network.
Apache is one of the most popular web server applications in the world, accounting for nearly 40 percent of all web servers at the time of writing.
Once you've configured Apache on your Raspberry Pi, you can use it to serve various files to anyone on the local network.
To complete this tutorial you will need:
If you haven't already, connect your external keyboard, monitor, and other peripherals to your Raspberry Pi, then connect it to a power source.
Before you start, it's best to make sure your Raspbian is the latest version. Launch a Terminal window by clicking on the small "Terminal" icon in the toolbar. Type the following commands in the terminal:
sudo apt-get updatesudo apt-get upgrade
If Raspbian installs one or more updates, restart your Raspberry Pi by running the following command:
Once your Raspberry Pi has restarted, it will run the latest version of Raspbian.
You are ready to install the Apache2 package on our Raspberry Pi. In Terminal, type the following command:
sudo apt install apache2 -y
There you go: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; just enter this address in any web browser. You should see the following page.
Before you can edit this file, you must take ownership of it. You can change the property using a Terminal command. The following example assumes you are using the Raspbian username "pi"; if you changed it manually, make sure it is reflected in your Terminal command:
sudo chown pi:index.html
If you run the
ls -al again command, you should see that "pi" now has permission to modify this file.
You can now open the "It's Working" page for editing by running the following Terminal command:
This launches the "index.html" file in Raspbian's Nano text editor.
You can modify every part of the code on this page, but to make things easier, the text displayed in its header has been modified in this example.
Once you have made your changes, save the file by pressing Ctrl + O , followed by Ctrl + X .
Now load your Raspberry Pi's IP address into your web browser and you should see your changes.
By default, the Apache web server is limited to static content, so your pages will not react to information provided by users. If you want to make your content dynamic, you will need to install the latest version of PHP, which was PHP 7.4 at the time of writing.
In this section, you will 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 correctly configured, you will 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, enter the following PHP script:
This simple script retrieves today's date and displays it as part of a web page.
To save your script, press Ctrl + O , followed by Ctrl + X .
Test your dynamic PHP
To test that this PHP file is served correctly, enter your Raspberry Pi's IP address into your web browser, followed by "/mywebpage.php". For example, if your IP address was 18.104.22.168, you would enter the following URL:
If the PHP file is served correctly, your browser should display something like the following image.
As you can see, it's easy to turn your Raspberry Pi into a web server, although you'll need to set up a dynamic IP address to be able to connect to your web server from a public network.