Setup Apache Virtual Hosts on UBUNTU 14.04 LTS

Introduction

The Apache web server is the most popular way of serving web content on the internet. It accounts for more than half of all active websites on the internet and is very powerful and flexible. Apache breaks its functionality and components into individual units that can be customized and configured independently. The basic unit that describes an individual site or domain is called a virtual host.

These allow the admin to use one server to host multiple domains or sites off of a single interface or IP by using a matching mechanism. This is relevant to anyone looking to host more than one site off of a single VPS. Each domain that is configured will direct the visitor to a specific directory holding that site’s information, never indicating that the same server is also responsible for other sites. This scheme is expandable without any software limit as long as your server can handle the load.

Conditions

Before you start, you need to create non-root as explained in step 1 to 4 here.

You will need to have Apache installed server. If you have not, you need to use following command to install apache on server.

sudo apt-get update
sudo apt-get install apache2

 

Step 1.  — Create the Directory Structure

Document root (the top-level directory that Apache looks at to find content to serve) will be set to individual directories under the /var/www directory.

Within root directory, create public_html folder for site related files.

To create directory, user following commands

sudo mkdir -p /var/www/a2bizz.com/public_html

 

Step 2. — Access Permissions
sudo chown -R $USER:$USER /var/www/a2bizz.com/public_html

The $USER variable will take the value of the user you are currently logged in.

sudo chmod -R 755 /var/www

 

Step 3— Create IndexPage for Each Virtual Host
nano /var/www/a2bizz.com/public_html/index.html

index.html

<html>
  <head>
    <title>Welcome to A2bizz.com!</title>
  </head>
  <body>
    <h1>Success!  The awbizz.com virtual host is working!</h1>
  </body>
</html>

 

Step 4 — Create Virtual Host File

Apache comes with a default virtual host file called 000-default.conf that we can use as a jumping off point. We are going to copy it over to create a virtual host file for each of our domains.
Create the Virtual Host File

Copy conf file

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/a2bizz.com.conf

Edit a2bizz.com.conf file in editor

sudo nano /etc/apache2/sites-available/a2bizz.com.conf

The file look like,

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

First, we need to change the ServerAdmin directive to an email that the site administrator can receive emails through.

ServerAdmin admin@a2bizz.com

After this, we need to add two directives ( i.e. ServerNameServerAlias )

ServerName a2bizz.com
ServerAlias www.a2bizz.com

We need to change virtual host file is the location of the document root for domain.

DocumentRoot /var/www/a2bizz.com/public_html

Virtual host file should look like this:

<VirtualHost *:80>
    ServerAdmin admin@a2bizz.com
    ServerName a2bizz.com
    ServerAlias www.a2bizz.com
    DocumentRoot /var/www/a2bizz.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

save and close the file.

 

Step 5 — Enable the New Virtual Host Files

Use the a2ensite tool to enable the site.

sudo a2ensite a2bizz.com.conf

When you are finished, you need to restart Apache to make these changes take effect:

sudo service apache2 restart

You will most likely receive a message  something like following :

* Restarting web server apache2
 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this messag

This is a harmless message that does not affect our site.

Step 6 — Set Up Local Hosts File
sudo nano /etc/hosts

Add ip as per domain name

127.0.0.1   localhost
127.0.1.1   guest-desktop
222.222.121.111 a2bizz.com

This will direct any requests for a2bizz.com on our computer and send them to our server at 222.222.121.111

save and close file.

Step 7— Test virtual host
http://a2bizz.com