Creating a virtual host in Apache allows for multiple domains to be hosted on a single IP address. This is extremely useful in terms of hosting multiple domains on a single host or VPS. As far as I know, you can have an unlimited number of virtual hosts. The only limitation is the system resources in terms of CPU usage, memory and storage. This article will show you how to add Apache virtual hosts in Ubuntu.

Typically, the web root for Apache in Ubuntu is located in /var/www. Instead, we will use a directory under the /home/username directory. So, let’s now create a directory in /home/username/fakedomain.com/. We can achieve this by typing the following:

<pre lang="html">
$ mkdir /home/username/fakedomain.com

We will give it read and write permissions.

<pre lang="html">
$ chmod 755 -R /home/username/fakedomain.com

Now, let’s create a new virtual host file.

<pre lang="html">
$ sudo touch /etc/apache2/sites-available/fakedomain.com

We will add the following configuration.

<pre lang="html">
<virtualhost>
ServerAdmin webmaster@localhost
Servername fakedomain.com
ServerAlias www.fakedomain.com
DocumentRoot /home/username/fakedomain.com
</virtualhost>

Save the file by pressing Control-O followed by Control-X.

Activate our new host.

<pre lang="html">
$ sudo a2ensite fakedomain.com

Restart Apache.

<pre lang="html">
$ sudo service apache2 restart

Edit your hosts file. The first line is for Ubuntu, the second for Mac OS.

<pre lang="html">
$ sudo nano /etc/hosts
$ sudo nano /private/etc/hosts

Add the fakedomain.com to your hosts file. Substitute the IP address of your host.

<pre lang="html">
192.168.0.5  fakedomain.com

Save the file by pressing Control-O followed by Control-X.

You can now access fakedomain.com from your browser by typing ‘http://fakedomain.com’ on the address bar.