Tuesday 30 July 2013

Set Up Virtual Host on Ubuntu 12.04

1.     About Virtual Hosts

Virtual Hosts are used to run more than one domain off of a single IP address. This is especially useful to people who need to run several sites off on one virtual private server. The sites display different information to the visitors, depending on with which the users accessed the site.There is no limit to the number of virtual hosts that can be added to a VPS.


2.     Set Up

>  sudo su
 apt-get install apache2

3.     Create a new directory

The first step in creating a virtual host is to a create a directory where we will keep the new website’s information. 

>  mkdir –p /var/www/example.com/public_html

4.     Grant Permission

We need to grant ownership of the directory to the user, instead of just keeping it on the root system.

>  chown –R $USER:$USER /var/www/example.com/public_html
>  chmod –R 755 /var/www

5.     Create the Page

>  nano /var/www/example.com/public_html/index.html

We can add some text to the file so we will have something to look at when the IP redirects to the virtual host.

<html>
      <head>
                  <title>www.example.com</title>
      </head>
      <body>
                  <h1>Success: You have Set Up Virtual Host </h1>
      </body>
</html>

Save and Exit.

6.     Create the New Virtual Host File

>  cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example.com

7.     Turn On Virtual Hosts

 nano /etc/apache2/sites-available/example.com

<VirtualHost *:80>
      ServerAdmin   webmaster@example.com
      ServerName    example.com
      ServerAlias      www.example.com
      DocumentRoot /var/www/example.com/public_html

Activate the host, with the built in apache shortcut

>  a2ensite example.com


8.     Restart Apache

> service apache2 restart

9.     Setting Up the Local Hosts

 nano /etc/hosts
ADD
#Virtual  Hosts
172.16.1.28           example.com
172.16.1.28           www.example.com

All Done.

Happy to help you !! 

No comments:

Post a Comment