Virtual Hosting or Virtual Directory Using Apache

Introduction

Virtual hosting is a process for hosting multiple domain names on a single server. This is basically sharing the services of a single server for multiple companies or for multiple websites. Sometimes it's called Vhost. A Virtual Host is usually used by a company or organization that doesn't want to maintain or purchase multiple servers. Virtual Hosting can be supported using IIS Web Server or using Apache Web Server. In this article we will see how to create a virtual host using IIS Web Server on Windows Server 2012 VM.

You can do Virtual Hosting using IIS and Apache server. For more details about Virtual Hosting using IIS visit the following link.
http://www.c-sharpcorner.com/UploadFile/59b9d6/virtual-hosting-or-virtual-directory-using-iis/

This following procedure will provide you with deep knowledge about how to do Virtual Hosting using Apache Server.

Download the Apache “.msi” file from the https://archive.apache.org/dist/httpd/binaries/win32/ site.
For example httpd-2.2.25-win32-x86-no_ssl.msi.


Figure1 Download Apache

Install it on your server machine.


Figure 2 Instalation

Use the default server setting or change it depending on requirements.


Figure 3 Defoult Server Setting

Choose Typical Installation and finish the Apache installation process. After successful installation Apache will appear in your Taskbar icon list.


FIgure 4 Task Icon List

Now test Apache using an internet browser. Type localhost in the address bar and hit the Enter button.


Figure 5 Test Apache

Now create two web site folders and a default web page in the following Apache directory:
“C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs”


Figure 6 Create Website Folder

Now go to the following directory to add the virtual host details:
“C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\extra”


Figure 7 Directory

Open the “httpd-vhosts” file in a text editor and modify the virtual host part, in other words, the selected part in the following image.


Figure 8 Modify Virtual host

We can create as many virtual host as we want. In this tutorial we are creating two virtual hosts for two sites. Hence we added two virtual host entries.

  1. <VirtualHost *:80>  
  2. ServerName sitea.testadmin2.cloudapp.net  
  3. ServerAlias www.sitea.testadmin2.cloudapp.net  
  4. DocumentRoot "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\SiteA"  
  5. ErrorLog logs/sitea.com-error_log  
  6. TransferLog logs/sitea.com-access_log  
  7. </VirtualHost>
  1. <VirtualHost *:80>  
  2. ServerName siteb.testadmin2.cloudapp.net  
  3. ServerAlias www.siteb.testadmin2.cloudapp.net  
  4. DocumentRoot "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\SiteB"  
  5. ErrorLog logs/siteb.com-error_log  
  6. TransferLog logs/siteb.com-access_log  
  7. </VirtualHost>  
Where:
ErrorLog and TransferLog are optional parameters.
ServerName is the name of server or host name. We can change it depending on our needs.
ServerAlias is the alias name for ServerName. We can provide one or more alias names.
DocumentRoot is the directory of the main web page.

Optional

We can also add localhost to the virtual host list to provide different web page results.
  1. <VirtualHost *:80>  
  2. ServerName localhost  
  3. ServerAlias localhost, testadmin2.cloudapp.net  
  4. DocumentRoot "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs"  
  5. ErrorLog logs/localhost.com-error_log  
  6. TransferLog logs/localhost.com-access_log  
  7. </VirtualHost>

Figure 9 Add Localhost

To include this Virtual Host file in the Apache service we need to import this into the main Apache config file. Before that, save this virtual host file and close it and go to the following directory to include it in the main Apache cofig file.
“C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf”


Figure 10 Virtual host File

Open the “Httpd” file in a text editor and find “httpd-vhosts” text in this file.


Figure 11 HTTPD File

Now remove ” # ”, in other words the “hash”, from the “Include conf/extra/httpd-vhosts.conf” text line.


Figure 12 Remove #

Save this httpd file and close it.
Now open notepad in Administrative mode and browse to the following directory and open the host file.
“C:\Windows\System32\drivers\etc”


Figure 13 Open Notepad

Add the following entries of the new Virtual host web sites.

#Optional Virtual Host Entries
127.0.0.1 localhost
127.0.0.1 testadmin2.cloudapp.net

#Main Virtual Host Entries

127.0.0.1 sitea.testadmin2.cloudapp.net
127.0.0.1 siteb.testadmin2.cloudapp.net


Figure 14 Add Entries

Save this host file and open the Apache Service Monitor and restart it.


Figure 15 Save Host File

After successful restart of the Apache Service, test the virtual host in your machine. Open a web browser and test all the virtual hosts and local hosts.

http://localhost/
http://testadmin2.cloudapp.net/
http://sitea.testadmin2.cloudapp.net/
http://siteb.testadmin2.cloudapp.net/


Figure 16 Restart

If you want to test this virtual host from your remote machine, in other words over an internet connection, then you need to add an inbound rule for http port, in other words Port 80. For that open “Windows Firewall with Advanced Settings”.


Figure 17 Test Virtual Host

Select Inbound Rules. Right-click on it and select “New Rule..”.


Figure 18 Inbound Rules

Select the Port option and click on the Next button.


Figure 19 Select Port

For a HTTP port set 80 in the text box and click on the Next button.


Figure 20 HTTP Port Set 80

Select Allow the connection option and click on the Next button.


Figure 21 Select Allow the Connection

Check the all option and click on the Next button.


Figure 22 Check All Option

Add a Name and Description of the inbound rule and click on the Finish button.


Figure 23 Add Name

Now an inbound rule was successfully added to the inbound firewall settings.


Figure 24 Inbound Rule

We need to set the local DNS for this virtual host (that we already set on the VM). In your PC or remote machine or in a testing machine go to the following directory and open the “host” file in a text editor with Administrator privileges.
“C:\Windows\System32\drivers\etc”


Figure 25 Local DNS

Add the following entries so it will act as a local DNS server.

Syntax : <VM IP Address> <Virtual Host Name>

For example:

23.99.107.36 sitea.testadmin2.cloudapp.net
23.99.107.36 siteb.testadmin2.cloudapp.net


Figure 26 Add Following Entries

Now test this virtual host configuration in a web browser.


Figure 27 Test the Virtual Host