In this segment, I will show you how to install and configure Apache, a widely-used open-source HTTP server designed to serve web content over the internet. Although modern alternatives like Nginx can offer superior performance and enhanced security features in certain scenarios, Apache remains a robust and reliable choice due to its straightforward configuration, extensive documentation, and broad community support. For the purposes of our future project—Deploying a cost-effective, fully functional WordPress website for business use—Apache provides a stable foundation with support for the necessary modules and easy integration into our setup. So lets make it happen!
Required Materials:
- A Raspberry Pi running Raspbian 32/64 Standard or Lite
Installing Apache and PHP
To get started lets update our packages. Open a terminal and enter the following commands:
$ sudo apt update
$ sudo apt upgrade
Once we have our packages up to date, run the following command to install Apache.
$ sudo apt install Apache2
Once Aptitude finishes the installation, we will proceed with installing PHP as well. PHP is a Hypertext Preprocessor that will provide Apache with more flexibility and allow it to integrate with MySQL later on. Run the command:
$ sudo apt install php
Once the installation is complete, we need to set the correct permissions and ownership so Apache has proper control and access to our default web directory, /var/www/html.
Note: By default, Apache treats /var/www/html as the root directory for the web server.
Assigning a User, Setting Permissions, and Managing Ownership
As is the case with most things in Linux, there are actually a few ways we can go about this. I will list my preferred method below, but feel free to use whichever option you like.
First, we need to assign our user to the www-data group. This will allow us to make changes to the files in the /var/www/html directory without needing root access. That way, we can directly modify our pages using nano or any basic html editor. To do that, run the following command in a terminal window.
$ sudo usermod -a -G www-data $USER
Explanation: sudo grants the command root permission. usermod is the command used to add or modify a user. The -a flag tells the command to append (add) the user to the new group without removing them from any existing groups. The -G option is used to specify the supplemental user group, in this case www-data. $USER is a variable that means the currently logged-in user.
This will add the current user to www-data group.
Once that’s taken care of, issue the following command to put us in the /var/www/html directory.
$ cd /var/www/html
Once we are in the correct directory, we need to run three commands: one to set the ownership of the directory, and the other two, find commands incorporating arguments to set the directory and file permissions correctly. Run the following command to set ownership.
$ sudo chown -R www-data:www-data .
Explanation: sudo grants root privileges to the command, chown means change owner, -R makes the command recursive, meaning it will also apply to all files and directories within the target directory. www-data:www-data assigns the correct user and group, ensuring Apache has ownership. The . tells it to target current directory.
Next, we will use the find command to set the permissions correctly. Type the following commands into the terminal, one after the other.
$ sudo find . -type d -exec chmod 0755 {} \;
$ sudo find . -type f -exec chmod 0644 {} \;
Explanation: sudo grants root privileges, find is a command used to search for files and directories within a specified location, the . tells find to work in the current directory, -type d and -type f tell find to search for directories and files, respectively. -exec chmod tells find to apply chmod to any matches, setting their permissions to the specified level. {} is a placeholder for the current directory, and \; tells find that this is the end of the command for each match found.
The reason we use find instead of just a recursive chmod is so that we can properly set directories to 755 and files to 644. If we did sudo chmod -R 755, let’s say, it would set both directories and files to 755.
Once all that’s taken care of, let’s $ sudo reboot and perform some tests to verify everything is working.
Testing and Virtual Host configuration
Alright lets test a couple things out and make sure we did everything correctly. The the first thing we need to do is make sure Apache is working. We can do this by simply pointing a browser on our local server at the local IP address of the pi we installed it on. The easiest way to grab the local IP of said Pi is to run the following command in a terminal on it .
hostname -I
Example formatting: “http://your.ip.address/” or just “your.ip.address.here/”

If everything went well, you should be presented with a page just like this. Read over it; it has some useful information. This means that your Apache server is live. The file we are looking at is the default index.html in your /var/www/html directory. And yes, if you just asked yourself, ‘Can I directly modify that file and have a website?’ the answer is yes, yes you can.
But wait! We installed PHP too, didn’t we? Let’s test it out as well. It will allow us a lot more flexibility in our site design and is required by most CMS (Content Management Software—WordPress is a CMS for example).
To test out php we need to make a test file in the /var/www/html directory and point our browser at it. To do this we will issue the following command to open up a test file in nano, in the correct directory.
$ nano /var/www/html/test.php
Copy and paste the following code into the file.
<?php
echo “Congratulations! It worked!”;
?.>
Then press CTRL+X to save, Y to confirm, and enter to exit nano.
Point your browser back to your pi’s IP but add /test.php
Example format : “http://your.ip.address/test.php” or “your.ip.address.here/test.php”
You should be directed to a simple page displaying the text “Congratulations! it worked!”. If you are then, as the browser said, Congratulations! it worked!.
Virtual Host Configuration
At this point, Apache will handle any HTTP or HTTPS traffic in the default way. This means that any traffic coming across on port 80, whether it comes from a domain name or an IP address, will be directed to the index.html file within /var/www/html. The virtual host file is used for finer control over how Apache handles and directs HTTP and HTTPS requests within the local network.
For a better understanding and a solid framework, I always copy the default virtual host file to a new file and work from there. To do that, issue the following command:
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/yoursite.com.conf
Replace “yoursite.com” with either your IP address, or if you have a .com resreved that your setting this all up for, your “mysite.com” address. (Example: this sites virtual host file is hazeytec.com.conf)
Once we have copied the default config file over we can edit it with the following command:
$ sudo nano /etc/apache2/sites-available/yoursite.com.conf
Virtual Host offers a lot of possibilities, but its main function is to tell Apache how to handle requests from specific web addresses and route them correctly on our local host. In this guide, I’m going to keep things as simple as possible, so I won’t be covering everything you can do with Virtual Hosts. Maybe in a future article—but for now, we’ll stick to the basics.
By using Virtual Hosts, we can set up different configurations for each site we manage. For example, Apache can route requests from hazeytec.com to one directory, and requests from yettobedetermined.com to another directory on the same local host. This effectively creates separate sites.
The main commands used for this in the server host file are ServerName and ServerAlias. ServerName is the actual domain name of the server—so in my case, it’s hazeytec.com. ServerAlias lets me handle alternative names or subdomains. I have an alias set up for www.hazeytec.com which allows Apache to redirect any requests from either www.hazeytec.com or hazeytec.com to the correct folder containing the site’s index.html.
This allows you to host multiple sites using the same IP address.

Above is a screen grab of the default local host file.

Here is the modified version for this website. In my actual virtual host file, I also have some extra settings for Cloudflare and SSL, but that’s beyond the scope of this article.
One important thing to mention is that my DocumentRoot is set to /var/www/html because hazeytec.com is the default site on this Raspberry Pi. DocumentRoot sets the default directory for your sites files. Another thing to note is the line that starts with <VirtualHost *:80>. This tells Apache to listen for traffic on port 80 and handle it using the settings that follow. Port 80 is the default for regular HTTP traffic.
If I wanted to add a new site, like snazzydomainname.com, I would create a new virtual host file for it, make a new folder within /var/www/html called snazzydomainname.com, and configure the virtual host file so that all traffic from snazzydomainname.com points to that folder. That folder would then serve as the root for the snazzydomainname.com website and any index.html I placed in the folder would load when a request came in from snazzydomainname.com after I enable the file using the a2 command.
The reason I’ve explained it this way is because your virtual host file really needs to be customized to fit your specific use case. If you only plan to run one site, you could almost leave it with the default settings and allow all HTTP traffic to flow through to your site. However, this can create some security issues. So, it’s best to understand how to use virtual hosts properly. If you are simply testing and not running this off an actual domain name. You can just replace hazeytec.com with your Pi’s local IP address, for example.
Once you have your virtual host configuration set properly, you can CTRL-X to save, Y to confirm, and enter to exit. We now need to enable this specific virtual host file. To do that we will use the command a2.
To enable a specific virtual host file, enter the following command:
$ sudo a2ensite yourdamin.com.conf
Example formatting: $ sudo a2ensite hazeytec.com.conf would enable the virtual host file I have created for hazeytec.com.
We can also disable any virtual host file with the following command:
$ sudo a2dissite yourdomain.com.conf
In most cases you will want to disable the default configuration after you have set up your own virtual host using the following command:
$ sudo a2dissite 000-default.conf
This will prevent any conflicts that may arise between the two files. Once you are finished. Issue a $ sudo reboot command in a terminal for good measure and test it all out!
In Conclusion
In future articles, I plan to go deeper into security. But for now, we have a working HTTP server that can serve files to any browser that connects to it. There’s still a lot to cover, like securing the site with end-to-end encryption and setting up a proxy server to hide our actual IP address when using a domain name to reach the server. We’ll also likely need to set up port forwarding to allow web requests to pass through your home network or router if you want the world to have access to your site.
Security is important if we want to do this the right way. But for now, at its core, you have a functioning website. I hope you found this article helpful. See you next launch, Spaceman ^~