Setup Apache VirtualHost in Ubuntu

Posted on Sunday February 19, 2012 / by Eric Potvin

If you run multiple sites (including multiple domains) on the same hosting plan, you will have to setup each of them before using them, otherwise all the domains will go the same place.

Here's a quick solution to efficiently maintain all these domains and easily enable or disable some in case of emergency.

For this article we will use three (3) domains:

The ".com" domains has nothing in common and the ".net" domain belongs to The "" belongs to you and the "" and "" belongs to a friend.

Preparing the folder directory

Let's create the directory structure. Let's create the structure for the "". Because this domain belongs to you, you can use the default webroot /var/www/ or /usr/local/httpd/.

sudo -p mkdir /var/www/
chown myuser:myuser 755 /var/www/

Now, let's create the account for the second domain.

bookofzeus@ubuntu:~$ sudo adduser domain1

This will display something like:

[sudo] password for bookofzeus:
Adding user `domain2' ...
Adding new group `domain2' (1001) ...
Adding new user `domain2' (1001) with group `domain2' ...
Creating home directory `/home/domain2' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for domain2
Enter the new value, or press ENTER for the default
	Full Name []: John Doe
	Room Number []:
	Work Phone []:
	Home Phone []:
	Other []:
Is the information correct? [Y/n] y

This will create the account and the folder structure for the user.

Now, let's create the public_html where the website files will be uploaded.

sudo mkdir -p /home/domain2/{public_html,private,logs}

Let's give the permissions:

sudo chown -R domain2:domain2 /home/domain2/

This will create the basic structure:

bookofzeus@ubuntu:/home/domain2$ ll
total 12
drwxr-xr-x 2 domain2 domain2 4096 2012-02-20 07:46 logs
drwxr-xr-x 2 domain2 domain2 4096 2012-02-20 07:46 private
drwxr-xr-x 2 domain2 domain2 4096 2012-02-20 07:46 public_html

Setup the VirtualHost

Now the directory structure is created, we can setup the VirtualHost.

Make sure the sites-enabled is in the apache configuration. If not, you need to add it to the file.

sudo vi /etc/apache2/apache2.conf

And add the following line:

# Include the virtual host configurations:
Include sites-enabled/

Now, let's create the configuration file for both domains. Let's create the first domain (

vi /etc/apache2/sites-available/domain1.conf

and use the following config:

<VirtualHost *:80>
 DocumentRoot "/var/www/"
 <Directory "/var/www/">
  allow from all
  Options +Indexes

Same idea for the domain2 (.com and .net)

vi /etc/apache2/sites-available/domain2.conf

and use the following config:

<VirtualHost *:80>
 DocumentRoot "/home/domain2/public_html/"

 LogLevel warn
 ErrorLog  /home/domain2/logs/error.log
 CustomLog /home/domain2/logs/access.log combined

 <Directory "/home/domain2/public_html/">
  allow from all
  Options +Indexes

You notice the ServerAlias use both .com and .net domains and the ServerName use only the .com. Very important not to mix both.

Now that both configuration is created, let's enable them.

ln -s /etc/apache2/sites-available/domain1.conf /etc/apache2/sites-enabled/
ln -s /etc/apache2/sites-available/domain2.conf /etc/apache2/sites-enabled/

By creating the symbolic link, you can easily enable (create a symlink) or disable (delete the symlink) the website.

Now, re-start the web server:

/etc/init.d/apache2 restart

If everything went well, when you go to the you will see your website and when you go to the or you will see the other website.