rss feed Twitter Page Facebook Page Github Page Stack Over Flow Page

Setup Apache VirtualHost in Ubuntu

If you run multiple sites (including multiple domains) on the same hosting plan, you will have to set up 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 domain2.com. The "domain1.com" belongs to you and the "domain2.com" and "domain2.net" belongs to a friend.

Preparing the folder directory

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

sudo -p mkdir /var/www/domain1.com/public_html/
chown myuser:myuser 755 /var/www/domain1.com/

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 set up 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 (domain1.com).

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

and use the following config:

<VirtualHost *:80>
 DocumentRoot "/var/www/domain1.com/"
 ServerName domain1.com
 ServerAlias domain1.com www.domain1.com
 <Directory "/var/www/domain1.com/">
  allow from all
  Options +Indexes
 </Directory>
</VirtualHost>

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/"
 ServerName domain2.com
 ServerAlias domain2.com www.domain2.com domain2.net www.domain2.net

 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
 </Directory>
</VirtualHost>

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 domain1.com you will see your website and when you go to the domain2.com or domain2.net you will see the other website.