The Apache HTTP Web server is a powerful, free and open source web server that has been, and remains the leading web server platform ahead of others such as Nginx and Microsoft IIS.
According to Opensource.com Apache enjoys a market share of 45.9%, and powers popular sites such as PayPal, Apple, Craigslist, Adobe, and BBC to mention just but a few.
Table of Contents
What is Virtual Host?
Apache web server allows us to create individual units which can be configured and customized independently to serve multiple sites on the same server. This is known as virtual hosting.
This happens when multiple domain names, each handled separately, are hosted on a single server. The server is thereby able to share its resources such as disk space and memory utilization.
Each domain is configured to direct traffic to a unique directory containing information specific to that domain name.
A widely used application of virtual hosting is in shared web hosting where multiple customers are hosted on a single server. This is a cheaper alternative than dedicated hosting which is far more expensive.
In this guide, we will delve in setting up and configuration of Apache Virtual hosts on Ubuntu 18.04 LTS.
Installing Apache Web Server
Before installing Apache, it is a good idea to update the package repository. You can do this by running the following commands:
# sudo apt update
Thereafter, install the Apache web server.
# sudo apt install apache2
The two commands mark the first of a very important process. For illustration purposes, we will use
debian.edu as our virtual hosts.
Following this guide will also take you through testing both domains and see if the configurations are working.
Creating directory structures for virtual hosts
We need a directory that will hold the data that can be accessed by users. Apache usually has a root document directory that serves users seeking information from it. Content within the root directory is assigned to individual users under the
Within each of these directories, we need a folder public folder (public_html) to hold all the uploaded files.
Run the following commands to create the two directories:
# sudo mkdir -p /var/www/ubuntu.biz/public_html # sudo mkdir -p /var/www/debian.edu/public_html
Now that we have the directory structures in place, we need to assign ownership to regular users so that they can modify files contained in the directories. To accomplish this, we need to run the commands as shown.
# sudo chown -R $USER:$USER /var/www/ubuntu.biz/public_html # sudo chown -R $USER:$USER /var/www/debian.edu/public_html
$USER variable takes the value of the currently logged on user upon pressing ‘Enter’. This allows the regular user to own the subdirectories contained in
public_html which will host our content.
To serve the webpages of the virtual hosts properly, we need to assign read permissions to the general www directory which will recursively assign read permissions to the files and folders therein. To achieve this, run
# sudo chmod -R 755 /var/www
At this point, our web server should have the requisite permissions to serve the content and users should also be able to create their own content within the respective directories.
Create new virtual host files
We need to copy the file to the first domain:
# sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ubuntu.biz.conf
Let us open the new file using nano as shown:
# sudo nano /etc/apache2/sites-available/ubuntu.biz.conf
You can either choose to leave it the way it is or make it more presentable for the first timer in VPS installation (note that the original file has # which denotes comments explaining what every line stands for).
From the file above the virtual host will process any request made on port 80 with the default protocol used here is the HTTP.
We need to assign a working email to the ServerAdmin attribute. In this case, we will use email@example.com
We will then add two lines:
- ServerName, which matches the base domain for the particular virtual host.
- ServerAlias that defines any other name that should closely match the base domain name. In this case, we will use www.
DocumentRoot attribute, change the path to reflect the virtual host file path.
All the three changes can be summarized by editing the virtual host file to look like this:
Save the changes and exit.
Let’s copy the first Virtual host and customize for
debian.edu (the other domain)
We copy it by:
# sudo cp /etc/apache2/sites-available/ubuntu.biz.conf /etc/apache2/sites-available/debian.edu.conf
Open and edit the file as shown:
# sudo nano /etc/apache2/sites-available/debian.edu.conf
Open the configuration file and make the corresponding changes where needed
Save and exit the configuration file.
Enabling newly created hosts files
Apache has some inbuilt tools that enable us to “activate” files. You can use the
a2ensite tool to accomplish this as shown:
# sudo a2ensite ubuntu.biz.conf # sudo a2ensite debian.edu.conf
Next, restart the Apache web server for the changes to take effect.
# sudo systemctl restart apache2
To test the functionality of your VPS, you need to modify the hosts file on the local computer. This helps re-direct domain requests to your VPS servers. This works the same way DNS systems operate when registering new domains.
The following steps should be performed on the local workstation and not the VPS server. You need to log in as the root for this to work. Edit the
/etc/hosts file as shown.
# sudo vim /etc/hosts
On this file, you need to add the public address (IP) of your VPS server then the domain name used to access the VPS. In my case, my VPS address is 192.168.43.195; consequently, I will edit my hosts’ file as shown:
Save and exit the text editor.
The above changes mean that any visitor’s request to
debian.edu will be directed through 192.168.43.195. This is how domains are redirected on Virtual Hosts.
Testing the virtual servers
Now that we are done with configuring of our virtual hosts it’s time to verify if all went all. For a start, you can check the accessibility of the two virtual hosts on the terminal using the ping command
Wonderful! We can reach the virtual host. You can now proceed and open your browser and visit the URL.
Great! Our first virtual host is working. Let’s try pinging our second virtual host
Similarly, open your browser and visit the virtual host’s URL
Apache HTTP web server can be easily configured to serve multiple websites from a single server. In this tutorial, we learned how to set up multiple virtual hosts configuration files in the apache web server.