Setup Apache Virtual Hosts On Ubuntu 15.04

About Virtual hosts

Virtual Hosts are used to setup more than one domain or websites using a single IP address. This is very useful if anybody wants to run multiple websites using a single IP address on single VPS.

In this tutorial, let me show how to setup virtual hosts in Apache web server on Ubuntu 15.04 server. Also, this method should work on previous Ubuntu distributions such as Ubuntu 14.10, 14.04 etc.

Scenario

For the purpose of this tutorial, I will be using Ubuntu 15.04 64bit server version, and I am going to host two testing websites namely “unixmen1.local” and “unixmen2.local”.

My test box IP address and hostname are 192.168.1.102/24 and server.unixmen.local respectively. Be sure to modify the virtual domain names as per your requirement.

Install Apache Webserver

Prior to install apache server, let us update our Ubuntu server:

To do that, run:

sudo apt-get update

Now, install apache web server using the following command:

sudo apt-get install apache2

After installing apache server, let us test whether the webserver is working properly or not by navigating to the URL http://ip-address/.

Apache2 Ubuntu Default Page: It works - Mozilla Firefox_001

As you see in the above picture, apache webserver is working.

Now, let us proceed to setup virtual hosts in Apache web server.

Setup Apache Virtual Hosts

1. Create Virtual Directories

Now, let us proceed to setup virtual hosts. As I mentioned earlier, I am going to host two virtual hosts called “unixmen1.local”, and “unixmen2.local”.

Create a public directory to place the two virtual hosts data.

First, let us create a directory for unixmen1.local site:

sudo mkdir -p /var/www/html/unixmen1.local/public_html

Then, create the directory for unixmen2.local site:

sudo mkdir -p /var/www/html/unixmen2.local/public_html

2. Setting Up Ownership and Permissions

The above directories are owned by root user now. We should change the ownership of these two directories to the regular user.

sudo chown -R $USER:$USER /var/www/html/unixmen1.local/public_html/
sudo chown -R $USER:$USER /var/www/html/unixmen2.local/public_html/

The “$USER” variable indicates the currently logged in user.

Set the read permissions to the Apache web root (/var/www/html/) directory, so that everyone can read files from that directory.

sudo chmod -R 755 /var/www/html/

We have created the directories for holding the websites data and assigned the necessary permissions and ownership to them.

4. Create Sample pages for Virtual Hosts

Now, we have to create the sample pages to be served through the websites.

First, let us create a sample page to the unixmen1.local virtual host.

Create a ‘index.html’ for unixmen1.local virtual host,

sudo vi /var/www/html/unixmen1.local/public_html/index.html

Add the following contents:

<html>
 <head>
 <title>www.unixmen1.local</title>
 </head>
 <body>
 <h1>Welcome To Unixmen1.local website</h1>
 </body>
</html>

Save and close the file.

Similarly, add the sample page to the second virtual host.

sudo vi /var/www/html/unixmen2.local/public_html/index.html

Add the following contents:

<html>
 <head>
 <title>www.unixmen2.local</title>
 </head>
 <body>
 <h1>Welcome To Unixmen2.local website</h1>
 </body>
</html>

Save and close the file.

5. Create Virtual Host Files

By default, Apache comes with a default virtual host file called 000-default.conf. We will copy the 000-default.conf file contents to our new virtual host files.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/unixmen1.local.conf
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/unixmen2.local.conf

Make sure the virtual host files contains .conf extension at the end.

Now, modify the unximen1.local.conf file to reflect with our new own values.

sudo vi /etc/apache2/sites-available/unixmen1.local.conf

Make the relevant changes that reflect to the unixmen1 site.

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@unixmen1.local
        ServerName unixmen1.local
        ServerAlias www.unixmen1.local
        DocumentRoot /var/www/html/unixmen1.local/public_html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Like wise, modify the second virtual host file.

sudo vi /etc/apache2/sites-available/unixmen2.local.conf

Make the relevant changes that reflect to the unixmen2 site.

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@unixmen2.local
        ServerName unixmen2.local
        ServerAlias www.unixmen2.local
        DocumentRoot /var/www/html/unixmen2.local/public_html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

After modifying the virtual hosts files, disable the default virtual host (000.default.conf), and enable new virtual hosts as shown below.

sudo a2dissite 000-default.conf
sudo a2ensite unixmen1.local.conf
sudo a2ensite unixmen2.local.conf

Finally, restart the apache service.

In Ubuntu 15.04:

sudo systemctl restart apache2

In Ubuntu 14.10 and previous versions:

sudo service apache2 restart

That’s it. Now, we successfully configured the apache virtual hosts on our Ubuntu server.

Testing Virtual Hosts

Edit file /etc/hosts,

sudo vi /etc/hosts

Add the virtual domain names one by one as shown below.

[...]
192.168.1.102   unixmen1.local
192.168.1.102   unixmen2.local

Save and close the file.

Open up your browser and point to the URL http://unixmen1.local or http://unixmen2.local. You should see the sample pages which we created earlier.

Unixmen1.local Test page:

www.unixmen1.local - Mozilla Firefox_002

Unixmen2.local Test page:

www.unixmen2.local - Mozilla Firefox_003

If you want to access these sites from your remote systems, you should add the actual domain name records in your DNS server. Hence, I don’t have any actual domain names and DNS server, I tested this only on my local system, and It’s worked perfectly as I expected.

Likewise, you can create and host as many as sites you wanted. Setting up Virtual Hosts in Ubuntu is not that difficult. Hope this tutorial will help you.

Cheers!