How to Set Up a Raspberry Pi Apache Web Server

In this Raspberry Pi Apache project, we will be showing you how to install and set up an Apache web server on the Raspberry Pi.

Raspberry Pi Apache

By itself, Apache can serve HTML files over the HTTP and HTTPS web protocols. Alongside additional modules such as PHP, Apache is also able to serve dynamic content.

An alternative to Apache is the Nginx web server and is considered to be much faster than Apache at certain tasks. It’s entirely up to you which technology you want to proceed with.

Within this Raspberry Pi Apache tutorial, we will be walking you through the steps to setting up Apache, installing PHP, and also creating your first basic Apache VirtualHost file.

In this tutorial, we touch on all the basics of Apache, however, we do not touch on setting up MYSQL and phpMyAdmin. We also don’t cover setting up WordPress on the Raspberry Pi. These are all topics covered in separate tutorials.

Equipment List

Below are all the bits and pieces that I used for this Raspberry Pi Apache web server tutorial.

Optional

We last tested this tutorial on a Raspberry Pi 5 running the latest version of Raspberry Pi OS Trixie.

Video

The video below touches on all the topics mentioned in this tutorial. I set up the Apache web server, installed PHP, and briefly touched on virtual hosts. It’s the perfect visual way to follow this tutorial. If you prefer text, keep following the tutorial immediately below the video.

How to Install the Apache Web Server on the Raspberry Pi

1. Before we install Apache to our Raspberry Pi, we must first ensure the package list is up to date by running the following two commands.

sudo apt update
sudo apt upgrade -yCopy

2. First, we will need to install the Apache2 package on our Raspberry Pi.

For those who don’t know what Apache is, it is server software that serves HTML files from a computer to the web browser.

To install Apache2 on your Raspberry Pi, enter the following command into the terminal.

sudo apt install apache2 -yCopy

3. With Apache2 installed on our Raspberry Pi, we now have an extremely basic web server up and running. The server will be able to provide non-dynamic content such as HTML files.

In the next section, we will be extending this basic Apache web server by installing PHP to the Raspberry Pi.

To check that Apache is up and running on your Raspberry Pi, you can enter the Raspberry Pi’s IP address into a web browser. The server should return a webpage with some simple text on it.

If you do not know the IP, you can enter the hostname command into the terminal to retrieve it.

hostname -ICopy

4. In a web browser, enter your Raspberry Pi’s IP Address, it should connect and load a page like the one below.

Raspberry Pi Apache Web Server Default Page

5. To be able to make changes to the files within /var/www/html without using sudo, we need to set up some permissions.

The command below will make your user the owner of this folder and everything in it.

sudo chown -R $USER:$USER /var/www/htmlCopy

$USER is automatically replaced with the name of your current user. Apache can still read these files, but it can no longer change them. This helps keep your website safe, as a PHP script with a security hole cannot be used to rewrite your website.

6. You can now make changes to the default web page by running the following command.

This command will use the nano text editor to modify the index.html file.

The web server will serve all files within the /var/www/html/ directory.

nano /var/www/html/index.htmlCopy

Apache is a basic web server and is great if you want to learn HTML, JS, or CSS.

However, if you are after PHP (Used for dynamic web pages) then continue to the next section of our Raspberry Pi Apache tutorial.

Setting up PHP for Apache

1. Please note that before you start this section, you should be running Raspberry Pi OS.

To start this section, we will need to go ahead and install PHP and several other packages on our Raspberry Pi. The additional packages we install are commonly used by PHP applications.

Run the following command to install all the PHP packages on your Raspberry Pi.

sudo apt install php libapache2-mod-php php-mbstring php-mysql php-curl php-gd php-zip -yCopy

This command installs the version of PHP that comes with your operating system, which is PHP 8.4 on Raspberry Pi OS Trixie. You can check which version you have by running “php -v“.

If you want to install a specific PHP version, you will need to add a third-party PHP repository. This repository should give you access to newer and older PHP releases.

2. Now that PHP is installed on our Raspberry Pi, we can test it to make sure it’s working.

We don’t have to worry about modifying any configuration files, as this is automatically done when Apache is detected.

We can test to see that PHP is working by creating a PHP file within the /var/www/html/ directory. Creating a file in this directory will allow it to be processed and displayed when you open it in a web browser.

For our example, we will be creating a PHP file called example.php. We can create this file by issuing the following command.

nano /var/www/html/example.phpCopy

3. In this file, we need to add the following lines of PHP code.

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

The code above is just an incredibly simple PHP script that prints out today’s date retrieved using PHP’s date() function. It’s enough to tell us that PHP is up and running.

4. Now save the file by pressing CTRL + X then pressing Y and hitting ENTER.

5. In your web browser, go to http://<IPADDRESS>/example.php.

Make sure you replace <IPADDRESS> with the IP address of the Raspberry Pi.

Going to the following URL should display something like the following.

Today's date is 2019-06-28 21:30:45Copy

Setting up an Apache Virtual Host

Virtual hosts are an essential part of the way Apache works. Apache uses these VirtualHost files so that it knows how to handle an individual site.

Within this section, we will show you how to create a basic virtual host file on your Raspberry Pi Apache web server. Virtual hosts are Apache’s way of handling multiple websites, with each Virtual Host file setting up and configuring a particular domain.

1. Begin by running the following command to create a basic virtual host file called example.com.conf within the /etc/apache2/sites-available folder.

If you plan on using this for an actual domain name, make sure you swap out example.com with the domain name.

sudo nano /etc/apache2/sites-available/example.com.confCopy

2. Within this file, enter the following text.

We will explain each part of the virtual host file as we go along so you can have an idea on how to set up a very basic virtual host.

<VirtualHost *:80>Copy

This line designates the start of the virtual host and that this virtual host should listen on port 80.

For those who do not know, port 80 is the default port for HTTP. Likewise, the port utilized for HTTPS is 443.

ServerName example.com
ServerAlias www.example.comCopy

Here we add two directives to our virtual host. The first of these directives called ServerName designates the base domain. This server name is used to match the VirtualHost to a domain name.

The second directive, ServerAlias, defines additional domain names that will be matched as if they were the base domain name.

This directive is useful for matching additional names such as a www. subdomain.

DocumentRoot /var/www/example.com/public_htmlCopy

The DocumentRoot directive defines the directory where all the files will be served from by Apache.

ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combinedCopy

In these final two directives ErrorLog and CustomLog we specify the locations where we want those log files to be kept.

</VirtualHost>Copy

Finally, we close off the VirtualHost section.

3. With everything complete, the code should end up looking like what we have below. Of course, using your domain name and not example.com.

<VirtualHost *:80>
      ServerName example.com
      ServerAlias www.example.com
      DocumentRoot /var/www/example.com/public_html
      ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
      CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>Copy

Once done, save the file by pressing CTRL + X followed by Y then ENTER.

4. Next, we will create the folder where we will be storing our website’s files. We will also make our user the owner of this folder, as we did for the default website.

Run the following two commands to create the folder we need and take ownership of it.

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_htmlCopy

5. Now that we have created our VirtualHost and the folder for it, let’s go ahead and activate it by running the following command.

This command creates a symlink for our config file between the /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ directories.

sudo a2ensite example.com.confCopy

6. Finally, for our new virtual host file to be loaded, we need to tell the Apache2 service to reload its configuration.

You can do this by running the command below.

sudo systemctl reload apache2Copy

If this command fails, you most likely have a mistake in your virtual host file, and Apache will keep running with its previous configuration. Run the following command to see which line of the file has the problem.

sudo apache2ctl configtestCopy

If everything is correct, this command will print Syntax OK. You can safely ignore the message about Apache not being able to determine the server’s fully qualified domain name.

You can now point a domain name server (DNS) to the Raspberry Pi’s public IP and have it serve files for the requested domain name. You will need to set up port forwarding for this to work correctly. You could also use Cloudflare Tunnel, Pangolin, or an NGINX proxy to expose your server to the internet.

Port forwarding and exposing the server to the internet does increase the risk of security issues.

Uninstalling Apache & PHP

If you no longer need a web server on your Raspberry Pi, you can remove Apache and PHP by following these steps.

1. First, remove Apache, PHP, and the PHP packages we installed by running the following command. Using purge also removes their configuration files. Please note that this will also remove any software that relies on these packages, such as phpMyAdmin.

sudo apt purge -y apache2 php libapache2-mod-php php-mbstring php-mysql php-curl php-gd php-zipCopy

2. Next, remove the packages that were installed alongside Apache and PHP and are no longer needed by using the following command.

sudo apt autoremove --purge -yCopy

You may see warnings that some folders were not removed because they are not empty. These folders hold your website and virtual host files, which we will delete in the next step.

3. Finally, if you no longer need your website’s files and your virtual host file, delete them by running the following command. If you used your own domain name, replace example.com with it.

sudo rm -rf /var/www/html /var/www/example.com /etc/apache2Copy

Conclusion

By now you should have the Apache web server running on your Raspberry Pi. You should also have a decent idea of how to set up PHP and virtual hosts on Apache.

If you have any feedback, questions, tips, or anything else, then please don’t hesitate to leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

17 Comments

  1. Avatar for Chris N
    Chris N on

    This setup worked perfectly on a Chromebook (Linux container) as well. Thanks!

  2. Avatar for s0me0ne
    s0me0ne on

    Following your instructions made it very simple, Thanks.

  3. Avatar for Ryan Cronin
    Ryan Cronin on

    Running this on a raspberry pi 2B for reference. At point 6 if fails the reload.
    So I put in the command:
    sudo systemctl status apache2.service
    Got this in the result:
    Process: 10380 ExecReload=/user/sbin/apachectl graceful (code=exited, status=1/FAILURE)
    Any ideas?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Ryan,

      Sadly that part of the status isn’t helpful on telling why Apache failed to start on your Raspberry Pi.

      There should be some additional text outputted when running that command, potentially it could contain the actual error that occurred for Apache2 to not launch.

      If possible please provide more information about the issue you are running into.

      Cheers,
      Emmet

  4. Avatar for Gez
    Gez on

    Great instructions but “How to Install the Apache Web Server on the Raspberry Pi” point 6 is wrong – you need to use sudo! Can you update please?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Gez,

      Thank you for the heads up, I have now corrected that part of the tutorial.

      Cheers,
      Emmet

  5. Avatar for gene stamper
    gene stamper on

    Very quick installation, thanks to the very clear instructions.
    Thanks
    Gene

  6. Avatar for Richard
    Richard on

    Best article I have read on this topic. As a senior citizen I enjoy/drive myself mad playing with computers. If I had found this earlier i would have enjoyed myself sooner.
    A model of clarity for an old duffer

  7. Avatar for John Webb
    John Webb on

    Is it possible to put WordPress on this installation

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi John,

      You can definitely use this installation to run WordPress on your Raspberry Pi.

      In fact we even have a guide on doing just that.

      Cheers,
      Emmet

  8. Avatar for Corey
    Corey on

    This is great! Clear, simple straight forward. I’m currently working through the port forwarding and Fail2Ban tutorials now.

    It would be nice to see a full tutorial (or string of tutorials) on how to go from “Pi in a box” to “Hello world displayed on your public website”.

    LOVE this website!

  9. Avatar for Erik
    Erik on

    After entering the ownership commands and rebooting, I still can’t edit the “index.html” file without sudo.

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Erik,

      That is correct, as we are giving ownership of files to the www-data user not the pi user.

      Cheers,
      Emmet

  10. Avatar for peter
    peter on

    Great Tutorial
    Is the double reference to php7.3-curl correct ?

    1. Avatar for Gus
      Gus on
      Editor

      Hi Peter,

      Thanks for pointing that out! It’s not correct, so I have removed the second reference.

  11. Avatar for Dewa
    Dewa on

    Hey, I need to thank you for giving this tutorial it really help me. But I have a problem, I couldn’t run the command “sudo apt install php7.3 php7.3-mbstring php7.3-mysql php7.3-curl php7.3-gd php7.3-curl php7.3-zip -y”. It’s said these information :

    E: Unable to locate package php7.3
    [SNIPPED]
    Could you solve it ?
    Best Regards,
    Dewa

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Dewa,

      Make sure that you are running Raspbian Buster or newer.

      Cheers,
      Emmet