Virtual Hosts in Ubuntu 14.04. This is a beginner level tutorial in which we will try to Install VHosts.

Let’s start with a little introduction about Virtual Hosts.

 

Virtual Host

The term Virtual Host refers to the practice of running more than one web site (such as company1.example.com and company2.example.com) on a single machine.

Virtual Hosts can be of two types… IP-Based Virtual Hosts & Name Based Virtual Host, In this tutorial we will try to Setup Name Based Virtual Hosts. If you want to know more about Virtual Hosts see the link.

Virtual Hosts in Ubuntu 14.04

Virtual Hosts in Ubuntu 14.04

 

 

Requirnments

The Basic Requirnment for Virtual Hosts in Ubuntu is LAMP.

  • LAMP (Linux, Apache, MySQL & PHP)

 

Virtual Hosts in Ubuntu 14.04

We will create two Virtual Hosts in this tutorial which are myvirtualhost1.com & myvirtualhost2.com. It is clear from the names of the Virtual Hosts that these are not the qualified Domain Names so we cannot access them publically by entering the name of the website in a browser but instead we have to add host entries for both of these Virtual Hosts to access them on our computer.

Now let’s start working on Virtual Hosts.

Step 1 (Directories):

First of all we will create two directories for our Virtual Hosts & those directories will contain all the data related to the each Virtual Host. Like Document Root & other files.

sudo mkdir -p /var/www/myvirtualhost1.com/public_html
sudo mkdir -p /var/www/myvirtualhost2.com/public_html

The above two commands will just create two new directories in /var/www named myvirtualhost1.com & myvirtualhost2.com. The -p flag will create all the parents of the directory.

Step 2 (Permissions):

Now we will grant the permission on the newly created directories for Virtual Hosts.

sudo chown -R $USER:$USER /var/www/myvirtualhost1.com/public_html
sudo chown -R $USER:$USER /var/www/myvirtualhost2.com/public_html

The variable $USER will make the user with which you are accessing the shell right now the owner of the file public_html. If you want to make someone else owner just change $USER:$USER with the Name of the account.

 

Now we will modify the permissions of the directory /var/www so that everyone has the read access to /var/www & all it’s subdirectories & files.

sudo chmod -R 755 /var/www

 

Step 3 (Sample File):

Now we will create sample files in both the virtual hosts. The files will be visible to us upon entering name of the Virtual Host in a browser (of course after entering Host Enteries in our local system.)

sudo vim /var/www/myvirtualhost1.com/public_html/index.html

This will crate a new file named index.html. We will just add a single line in this file for the sake of simplicity.

Welcome to myvirtualhost1.com!

Now repeat the above step for 2nd Virtual Host.

 

Step 4 (Vhost in Apache):

Now we will create a Virtual Host file in Apache.

sudo vim /etc/apache2/sites-available/myvirtualhost1.com.conf

This command will create a new file Apache & we need to add the below lines in the file.

<VirtualHost *:80>
ServerAdmin admin@ucovertech.com
ServerName myvirtualhost1.com
ServerAlias www.myvirtualhost1.com
DocumentRoot /var/www/myvirtualhost1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

The First line will match any requests made on the default HTTP Port 80 & the email address in ServerAdmin section is important if at later stage you are going to install Mail Server, But at this point it is necessary to add it in the Virtual Host File.

Third & Fourth lines contain the Server Name & its Alias.

Fifth & the most important line in the Virtual Host file is the Document Root, Make sure you enter the right path here.

The Next lines are for Apache Error Logs & Access Logs. If at some stage you face an error in Apache these are the files you are going to look at.

 

Now just repeat the above step for 2nd Virtual Host.

 

Step 5 (Restart Apache):

Enable the Newly Created Virtual Hosts in Apache. We will simple enable the Virtual Hosts with the command a2ensite.

sudo a2ensite myvirtualhost1.com.conf
sudo a2ensite myvirtualhost2.com.conf

Now the second last step is to Restart Apache so the changes can take effect immediately.

sudo /etc/init.d/apache2 restart

You will probably get an output like

AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1. Set the ‘ServerName’ directive globally to suppress this message

Ignore this message, It is harmless.

 

Step 6 (Host Entery):

Last step is for those who are not actually using the Qualified Domain Names, If you are using legit domains hosted at some Service Provider like Go Daddy or Name Cheap etc. You don’t need to perform this step. You are done with setting up Virtual Hosts in Ubuntu 14.04.

But those who are just using some random domains need to follow this last step to Setup Virtual Hosts in Ubuntu 14.04.

To  perform the last step make sure you are on your local system not on your VPS, because you have to add a Host Entry in your local system.

If you are on Linux edit the file

sudo vim /etc/hosts

Now add the names of the newly created Virtual Hosts in this file & you are done setting up the Virtual Hosts in Ubuntu 14.04.

127.0.0.1       localhost
127.0.1.1       guest-desktop
IP.OF.THE.SERVER      myvirtualhost1.com
IP.OF.THE.SERVER      myvirtualhost2.com

If you are on Windows follow the link.

 

Now you can test the Virtual Hosts by entering the names in a browser & you should see the welcome message.

Goto Borowser & type www.myvirtualhost1.com & you should see a page like…

Vhost 1

Virtual Hosts in Ubuntu 14.04

 

Now let’s open the other virtual host in the Browser www.myvirtualhost2.com

Vhost 2

Virtual Hosts in Ubuntu 14.04

 

That’s it folks! We have successfully configured Virtual Hosts in Ubuntu 14.04 🙂