Apache Virtual Hosts in Ubuntu in 7 easy steps

In this tutorial I’ll walk you through setting up 3 Apache virtual hosts running on a single Ubuntu server. An Apache virtual host, as explained by Apache, refers to the practice of running more than one web site (such as www.company1.com and www.company2.com) on a single machine. Virtual hosts can be “IP-based” or “name-based”. By default, Ubuntu already has this capability enabled, so things are much easier to configure these days. Enough with the intro, this is how you do it:

LAST UPDATE:
 It's May 2014 and Ubuntu 14.04 has been out for a little while.
Navigate to /etc/apache2/sites-available and see what's there. 
In the past the default website file has just been named "default" while later it was "default.conf" yet now it is "000-default.conf."
 "sudo a2dissite 000-default.conf" disables the default web site, allowing you to promote another one in its place.

We are going to create 3 sites, site1.com, site2.com and site3.com

1. Create the folders that will host your new sites. By default, Apache in Ubuntu serves from /var/www. So create these:

mkdir /var/www/site1
mkdir /var/www/site2
mkdir /var/www/site3

2. Copy the current default setting found in /etc/apache2/sites-available/default and name it the same as your new site.

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site1
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site2
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site3

3. Edit the new config files for each site using your preferred text editor. Add the line ServerName server1 right below the ServerAdmin line and change both DocumentRoot and Directory to point to your new sites.
This is what it should look like (you’ll do exactly the same for each of your 3 new sites, repeat this step for as many new sites as you’ll be creating):

/etc/apache2/sites-available/site1

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  ServerName YOURDOMAIN.YOU
  DocumentRoot /var/www/YOURDOMAINFOLDER (without slash trail)
 <Directory />
  Options FollowSymLinks
  AllowOverride None
 </Directory>
 <Directory /var/www/YOURDOMAINFOLDER/> (WITH slash trail)
  Options Indexes MultiViews
  AllowOverride None
  Order allow,deny
  allow from all
 </Directory>
</VirtualHost>

4. After you have edited the config files for each of the 3 or how many virtual hosts you are creating, just tell Apache to start serving the new domains and stop serving the default:

sudo a2ensite site1
sudo a2ensite site2
sudo a2ensite site3
sudo a2dissite default
note: (a2ensite, a2dissite - enable or disable an apache2 site / virtual host)

5. Now reload apache and you should be able to get to each of your new domains:

sudo /etc/init.d/apache2 reload

6. At this point the virtual host setup is done, all that is left is to tell the server that our-test-site.local should be resloved to 127.0.0.1.
We do that by typing

sudo gedit /etc/hosts

and adding 127.0.0.1 our-test-site.local after the localhost (line 1).

The entire hosts file should look like

127.0.0.1   localhost
127.0.0.1   YOURDOMAIN.YOU
127.0.1.1   ubuntu-vm

7. Save it, close the editor and finally type

sudo /etc/init.d/apache2 restart

Pay attention… to the user’s permissions !!!