LowEndBox

Hosting Websites on Bare Minimum VPS/Dedicated Servers

How to Setup Apache Virtual Hosts on Ubuntu 16.04

Tags: , Date/Time: April 14, 2017 @ 3:57 pm, by Abdu ssamad mk

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.

 

2 Comments

  1. Ahmed:

    What about for CENTOS???

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

    May 15, 2017 @ 10:30 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. Quoting webhostingtalk.com URL seems to get binned consistently here, but I do peek into the spam box frequently to publish those comments.
  • 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 *