LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

How to Setup Apache Virtual Hosts on Ubuntu 16.04

What is Apache Virtual Host

Apache virtual host helps to run multiple websites on the same server. There are two types of virtual host configuration that apache supports

  1. IP-based virtual hosts
  2. Name-based virtual hosts

IP based virtual host configuration is used to host multiple websites on different IP addresses on the same physical server. This is not commonly used while comparing with the name based virtual host.

The name based virtual host configuration is the commonly used configuration for serving multiple websites from the same physical server. In this configuration, when apache web server receives a request, it will look for the virtual host file configuration and deliver the content from the configured directory.

This process will not show the end user that the same server is responsible for serving of other websites. There is no limit on the number of websites you can host on your server using the virtual host configuration as far as your server can bear the load.

In this article, we will be guiding through how to configure apache virtual hosts on Ubuntu 16.04.

Prerequisites

Get the following things ready with you before we start configuring the virtual host file on Ubuntu 16.04

– A machine with Ubuntu 16.04 installed
– A user account with sudo privileges
– Putty to connect to server

Note:
Throughout this article, we will be referring domain names as “example1.com” and “example2.com”. Replace these example domain names with your actual domain name wherever required.

Did you get a chance to read our previous article How to Install Apache, MySQL, PHP (LAMP) Stack on Ubuntu 16.04? This article explains the installation procedure of Apache, MySQL, PHP on Ubuntu. It is the common configuration required to run most of the web applications.

Setup Apache Virtual Hosts on Ubuntu 16.04

This tutorial explains all the steps of virtual host configuration and testing of it for successful configuration. The steps covered in this article are listed below.

  1. Update system repositories
  2. Installing Apache on ubuntu
  3. Creating directory for websites
  4. Create test pages for each website
  5. Create Virtual hosts file
  6. Enable the newly virtual host files
  7. Testing of the virtual host files

1. Update System Repositories

Before we start with any installation or configuration, make sure that the system repositories are updated. Please run the following command on the Putty box to make the system up to date

sudo apt-get update

update Ubuntu 16.04 system

2. Installing Apache on Ubuntu

If you have already installed apache on the server, please move on to the next step. If not, go ahead with the following command to install apache on the server.

sudo apt-get install apache2

installing apache on ubuntu 16.04

The apache status can be checked by running either of the following commands

sudo service apache2 status

or

sudo systemctl status apache2

If the apache is running on your server, you can move ahead with the further steps listed below

3. Create directory for websites

We need to create a separate directory for each domain name to store the website files. By default, the document root will be /var/www/html. We are going to create separate directories for the domain names example1.com and example2.com in the www directory.

that is, the directory structure will look like as below.

/var/www/example1.com and /var/www/example2.com

For a better directory structure and convenience, we will host our website files in the public_html folder which is created inside each domain directory.

Just use the following command to change your current directory to /var/www/

cd /var/www

Confirm the present directory by issuing the following command

pwd

chnaging current directory

Just release the following commands in the putty box to create directories for example1.com and example2.com websites

sudo mkdir -p example1.com/public_html

sudo mkdir -p example2.com/public_html

Use the following command to list the contents of current directory

ls -lrt

creating virtual host directory on ubuntu

4. Create test pages for website

We will create test pages for both of these example URLs in the directory. This will be used to test our virtual host file after configuration.

We will place index pages on both of the domain’s public_html folders. This is a test file for our demo, if you need to use your website files, you can upload it now or later by removing this index file.

cd /var/www/example1.com/public_html

sudo vi index.html

Paste the following HTML code into the file and press Esc:wq and hit enter to save and return to the command window

<html>
<body>
<h1>Howdy! The example1.com virtual host is working Fine!</h1>
</body>
</html>

Now, we will create a similar index for example2.com, If you need only one virtual host to be created for testing, just skip this step

cd /var/www/example2.com/public_html

sudo vi index.html

Copy paste the following code into the text editor and exit by pressing the keys Esc:wq and Enter

<html>
<body>
<h1>Howdy! The example2.com virtual host is working Fine!</h1>
</body>
</html>

Now you have successfully created the index files for both the domain names. We will move on to the next step for creating virtual hosts files for both these websites.

creating files on ubuntu linux

5. Creating virtual host files

In this step, we are going to create virtual host files for each of our domain names. This virtual host files will tell the web server to process the correct files for each website request.

By default, there will be a virtual hosts file named “000-default.conf” which helps to load the default Apache web server page on request.

We will copy this default configuration for our website and will make some changes to work with our domain example1.com

cd /etc/apache2/sites-available/

sudo cp 000-default.conf example1.com.conf

The above command will copy the contents of 000-default.conf to example1.com.conf. Now we will modify example1.com.conf to accept the request for the domain name example1.com

sudo vi example1.com.conf

The example file will look like as below

modify virtual hosts file to add new domain name

We have only a few lines to customise in it.All the lines starting with # is a comment.

The following line of the virtual host file will match the request that comes on the port 80

<VirtualHost *:80>

We need to modify the server admin email in the next line

ServerAdmin webmaster@example1.com

We need to add two more directives after this line to process the domain request. The ServerName and ServerAlias which will match the domain name to process the request.

ServerName is the domain name. ie, example1.com

ServerAlias is the additional matching conditions that need to be processed. ie, www.example1.com

Add these lines to the virtual host file

ServerName example1.com
ServerAlias www.example1.com

The most important and final thing we need to change in the default virtual host file is the document root. As mentioned, the default document root will be /var/www/html. However, we have created different directory for our websites which needs to be mentioned here

DocumentRoot /var/www/example1.com/public_html

You can save and close the opened virtual host file after making these changes.

In short, the virtual host file will have the following contents after removing the comments from it

<VirtualHost *:80>

ServerAdmin webmaster@example1.com
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/example1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

apache example virtual hosts file

You have successfully created virtual hosts file for example1.com. Create the virtual host file for example2.com by just repeating the steps by changing example1.com to example2.com

6. Enable new virtual host files and disable the default

In this step, we will enable the newly created virtual host files and disable the default file created by apache.

Just run the following command in the putty box to enable example1.com.conf and example2.com.conf using a2ensite tool

sudo a2ensite example1.com.conf

sudo a2ensite example2.com.conf

enable virtual host on apache

Now disable the default virtual host file

sudo a2dissite 000-default.conf

You need to restart apache to take these changes effect. Supply any of the following commands to restart apache

sudo systemctl restart apache2

or

sudo service apache2 restart

Set up Apache virtual hosts file on Ubuntu has been completed. Now we can go ahead with testing of the virtual host file.

7. Testing virtual hosts file configuration

If you have used a real domain name that is pointed to your VPS, you can just load that domain from the web browser. Otherwise, you need to edit the local host file to redirect the dummy domain name to your web server’s IP address

For Mac and Linux computer, edit the following file

/etc/hosts

For Windows, edit the below hosts file to redirect the domain request to your server IP

C:\Windows\System32\drivers\etc\hosts

Add the server IP address and the dummy domain name in the following format

modify local hosts file to redirect request

Save the hosts file on your local machine and try to access the example domain name from any web browser. If you are receiving the following result, you have successfully configured virtual hosts on Ubuntu.

apache virtual host set up completed successfully

 

Wrapping up…

If you have correctly followed our tutorial, you might have ended up configuring two virtual hosts file on ubuntu for running two websites on a single server.

You can run multiple websites on the same server by adding more virtual host files to the configuration by repeating the steps mentioned in this article.

 

Frank

15 Comments

  1. Ahmed:

    What about for CENTOS???

    April 15, 2017 @ 2:35 pm | Reply
  2. gg

    May 15, 2017 @ 10:30 am | Reply
  3. er68:

    Thank you very much

    September 16, 2017 @ 5:10 pm | Reply
  4. Alfredo:

    Thank you very much, excellent :)

    September 21, 2017 @ 3:01 pm | Reply
  5. Tapas Barik:

    It worked! Thank you

    October 2, 2017 @ 3:41 pm | Reply
  6. AKMorris:

    Thanks for this, some other notes, add this inside the tag if you are using something like Laravel that uses a .htaccess file
    Require all granted
    AllowOverride All

    “Require all granted” means all IP Addresses can access the site (I assume you can also block IP’s here?)
    “AllowOverride All” means use the .htaccess file located in the Directory.

    I just had to add this to get Laravel 5.4 working with Apache 2.4 because routes were not working after deployment to production. Happy I found your site as it helped set everything else up :)

    October 24, 2017 @ 12:40 am | Reply
  7. david souto de souza:

    Please, this is running if you load the browse of the same local machine where the virtual hosts is, but do not for the others addresses in the same local network. How do it for the others machine in the local network (the mask is the same)?

    November 18, 2017 @ 5:46 pm | Reply
  8. Ariel Cuenca:

    Abdu, thank you very much for this tutorial. It has helped me a lot.
    Greetings from Mexico!

    December 4, 2017 @ 2:51 pm | Reply
  9. Juan Antonio:

    Thank you very much it was very helpful.

    March 4, 2018 @ 10:57 am | Reply
  10. Kemal Gencay:

    Every thing is fine but at the final step when I type deneme01.com I get this error msg and my site does not show:

    Your connection is not private
    Attackers might be trying to steal your information from deneme01.com (for example, passwords, messages, or credit cards). Learn more
    NET::ERR_CERT_COMMON_NAME_INVALID

    April 12, 2018 @ 1:46 am | Reply
  11. Terry deSouza:

    Thank you for the help everything is working fine we need more people like you on the web to help other people ho want to learn more about Linux

    *****

    May 5, 2018 @ 12:29 pm | Reply
  12. samar:

    not working for me ,,,,

    May 7, 2018 @ 3:58 am | Reply
  13. Kouassi Boris Berenger:

    Hello, tks all works
    But how can I run Let’s encrypted https for all virtual host.

    May 27, 2018 @ 3:28 pm | Reply
  14. kamal deol:

    perfect

    July 27, 2018 @ 12:48 am | Reply
  15. Shoaib Akhtar:

    Simple but super duper tutorial, thanks buddy.

    December 4, 2018 @ 1:20 am | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

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