[8 Minutes] Apache Virtual Host Ubuntu Setup Tutorial for Beginners


When you buy a normal hosting package, you will have limitation for everything. Since, you dont know the management, you have to go with their plans and pay them frequently.


If you have little knowledge about handling Ubuntu Server, then you are lucky enough to save more than 900% of money that you are spending away. There are a lot of micro management needed to handle this.

However, they are not such a heavy tasks.

Today, Here in this article, i am going to show you how to use apache virtual host on Ubuntu to host multiple sites at the same time.

Let me tell you about the benefits of this one.

A basic single server from DO or Vultr can install more than 7000 visitors in real time (for static sites only). And that too for 5$.

Also, they give you 1TB bandwidth, it is enough to serve minimum of 5,00,000 Users.

You can take advantage of this and host multiple sites on a single server to handle the traffic efficiently.

You can do this easily with Apache web server. It is a simple web server. Incase if you dont know the basics of Apache, learn here about How to install Apache on Ubuntu server.

Apache is easy to configure and there is functionality called virtual hosts.

It will help you to host multiple sites on Ubuntu server.


Install Apache on Ubuntu 16.04

You can install the Apache directly from Ubuntu repositories using the apt-get utility. First, update the package index.

$ sudo apt-get update

Then, install the apache web server using the below command.

$ sudo apt-get install apache2

Once you are done with the above step, then next step is to setup two virtual hosts files with example.com and example1.com.

Here, you have to use your own domain name to work on further. Point two of the domains to your server.

If you are using a CDN, make sure that your settings are correct and domains are properly pointing to the server IP address.

Make directories for Virtual Hosts

Here, in this step, we are going to create a directories under the document root of Apache web server.

The document root is /var/www/. We will create two files under this folder to hold the virtual host files.

Basically Apache is configured to serve web content from the Document Root. you can change this in apache configuration file.

Inside this directory, we will create two files and inside that, we will create public_html file.

Use the below command to create the directories.

sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/example2.com/public_html

You have to use your actual domain names here.

Once you have created the files, the next step would be granting the permission for it.

Change File Permission to Normal User

The file which you have created is owned by root user. Now, you have to make the regular user access it. For that execute the below command.

Considering that, you have logged into other account, execute this command.

sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example2.com/public_html

The above command will give permission to the normal user.

Now, the public_html page is accessible by the regular user.

The one more step here is verify the user has permissions to read files of web root. For that, use the below command.

$ sudo chmod -R 755 /var/www

After that, web server will have access to the content to serve and then your user account will be able add content to the web root directory.

Make a Test Page for Virtual Host

Now, we have to create index.html file for both virtual host files. For that, use the below command.

$ nano /var/www/example.com/public_html/index.html

The nano editor will open and add the code below to that file.

    <title>Welcome to Example.com!</title>
    <h1>Success!  The example.com virtual host is working!</h1>

Then, save and close the file. Instead of creating a new file, let us just copy the first file to the second file by executing the below command.

$ cp /var/www/example.com/public_html/index.html /var/www/test.com/public_html/index.html

Now, open the second file and make changes to match the output for second domain.

$ nano /var/www/test.com/public_html/index.html

Make changes in the file as mentioned below. It is easy. We are just going to replace the domain name.

    <title>Welcome to example2.com!</title>
  <body> <h1>Success!  The example2.com virtual host is working!</h1>

Creating Virtual Host Files

After creating the sample pages, now, it is time to create the configuration file for two virtual hosts.

By default, Apache has a virtual Host file called 000-default.conf.

Since, we have two virtual host files, we have to create two configuration files to notify apache how to work with both of those files.

First, let us copy the default configuration file to the first domain configuration file.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Now, you have to edit the copied file to make modification.

$ sudo nano /etc/apache2/sites-available/example.com.conf

You can see the below information in the file.

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Here, in the above file, we are going to create two additional directives and make changes to the one directive.

The two additional directories are

ServerName example.com
ServerAlias www.example.com

The server name refers to the domain. All the request from port 80 will be accepted for this domain. And the server alias refers to another form of domain representation.

Here, it means both example.com and www.example.com are same.

In the ServerAdmin directive, we have to add our email to send mail.

I will change that here as

ServerAdmin [email protected]

We also need to change the document root to our newly created document root file for the example.com domain.

DocumentRoot /var/www/example.com/public_html

That should look like this. After you make the changes, the file will look like below.

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Then, Save and close the file.

Now, you have to create the configuration file for the second domain also. For that, you just need to copy the example.com configuration file and then edit it.

$ sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/example2.com.conf

Now, open the second configuration file using nano editor.

Make modification  to the code to look like below.

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example2.com
    ServerAlias www.example2.com
    DocumentRoot /var/www/example2.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

After that, save and close the file.

Enable the Virtual Host Configurations

Once you have create the new virtual Host files, now its time to enable them.

$ sudo a2ensite example.com.conf
$ sudo a2ensite test.com.conf

When you enable the newly created configuration file, you have to disable the default configuration file.

$ sudo a2dissite 000-default.conf

To make the changes to work, restart the apache web server.

$ sudo systemctl restart apache2

Now, it is time to test what we have done.

Go to the browser and type example.com


You should see the below output.

Success! The example.com virtual host is working

Now, put the example2.com in browser.


Then, you can see the new output as mentioned here.

Success! The example2.com virtual host is working

That’s all. Now, you have configured the apache virtual hosts.


Here, in this article you have learned how to setup apache virtual hosts to host multiple site under same web server.

Subscribe for our news letter to get notified for our next tutorial.

Comment here, if you have any doubts.


Share on facebook
Share on twitter
Share on linkedin

Leave a Comment