How To Set Up Apache Virtual Hosts on Ubuntu?


The Apache web server is the most popular way of providing web content online. And Virtual Hosts allow the administrator to use one server to host several websites or domains on only one interface or IP by using a matching mechanism. This is applicable to anyone looking to host more than one site on a single VPS.

Each website that is configured will direct visitors to a specific directory having that site’s information, never showing that the same server is also accountable for other websites. This scheme is extended without any software limit as long as your server can handle the load.

You must have installed Apache in order to work through steps to create virtual host. If you have not installed Apache follow this article to install LAMP Stack on Ubuntu.

Let’s follow steps to create Apache Virtual Hosts:

Step 1 — Create the Directory Structure:
The first thing that we are going to take is to create a directory that will keep the website data and files.

For example, for our websites, we’re going to make our directories like this:

Step 2 — Grant Permissions:
Now we have a directory for our files, but they are owned by root user. If we want a regular user to be able to modify files in directories, we can change the ownership by doing this:

The $USER variable will take the value of the currently logged in user.
By doing this, our regular user now owns the public_html subdirectories where we will be saving our content.

We should also change our permissions a little bit to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages work well correctly:

Step 3 — Create New Virtual Host Files
Virtual host files are the files that specify the actual settings of our virtual hosts and determine how the Apache web server will react to various domain requests.

Apache comes with a default virtual host file that is 000-default.conf. We are going copying it over to create a virtual host file for each of our websites.

We will start with one domain, set up it, copy it for our 2nd domain, and then make the few further adjustments needed. The default Ubuntu settings needed that each virtual host file end in .conf.

Create the First Virtual Host File:
Start by copying the file for the first domain:

Open the new file (example.com.conf) in your editor with root privileges:

Now delete all content of the file and past below lines of code:

  • In above code ServerAdmin directive represent to an email that the site administrator can receive emails through.
  • ServerName directive represent the base domain that should match for this virtual host definition. This will most likely be your domain. ServerAlias defines further names that should match as if they were the base name. This is useful for matching hosts you defined, like www.
  • DocumentRoot directive represent the location of the document root for the domain.

Copy First Virtual Host and Customize for Second Domain
Now that we have our 1st virtual host created, we can create our 2nd by duplicating that file and modifying it as needed.

Open the new file with root privileges in your editor:

Now need to change all of the pieces of details to reference 2nd domain. It may look something like this:

Save and close the file.

Step 4 — Enable the New Virtual Host Files
Now that we have created our virtual host files, we must enable them. Apache contains some tools that allow us to do this.

We can use the a2ensite tool to enable each of our sites like this:

When you are finished, you need to restart Apache to make these changes take effect:

Step 5 — Set Up Local Hosts File
To implement this step edit your host file with administrative privileges by typing:

In this file need to add are the public IP address of your VPS server followed by the domain as shown below:

Here in above example I assumed vps ip address is 111.111.111.111 you should replace it with your vps ip address.

For local machine we will add 127.0.0.1 followed by domain name as shown below:

Save and close the file.

Now all is done, You’ve successfully configured two virtual hosts on the same server. Test it out by visiting domains on the browser.

Leave a Reply

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