Set Up Virtual Host on Ubuntu

how_to_create_a_virtual_host

 

 

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.

  1. Set Up

>  sudo su

>  apt-get install apache2

  1. 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

  1. 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

  1. 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.

  1. Create the New Virtual Host File

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

  1. 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

  1. Restart Apache

> service apache2 restart

  1. Setting Up the Local Hosts

>  nano /etc/hosts

ADD

#Virtual  Hosts

172.16.1.28           example.com

172.16.1.28           www.example.com

9 Comments

Add a Comment

Your email address will not be published. Required fields are marked *