20 Dec 2014

Virtual Host Configuration in Apache

Apache was one of the first servers to support IP-based virtual hosts right out of the box. Versions 1.1 and later of Apache support both IP-based and name-based virtual hosts (vhosts). The latter variant of virtual hosts is sometimes also called host-based or non-IP virtual hosts.

The term Virtual Host refers to the practice of running more than one web site on a single machine. Virtual hosts can be 'IP-based', meaning that you have a different IP address for every web site, or 'name-based', meaning that you have multiple names running on each IP address.

Let us see Name-Based Virtual Host configuration in Apache.

1. Uncomment httpd-vhosts.conf in httpd.conf

If you’ve installed Apache 2 from source, by default, the following line will be commented in the httpd.conf file. Uncomment (remove ‘#’) from the line shown below.

#Include conf/extra/httpd-vhosts.conf

2. Setup Virtual host

2.1 For a virtual host

Go to '../conf/extra/httpd-vhosts.conf' in apache installation folder and modify the ‘httpd-vhosts.conf’ as shown below to setup named-based virtual host setting for one host.
   NameVirtualHost *:80

   <VirtualHost *:80>
      ServerAdmin example@email.com
      DocumentRoot "/usr/local/apache2/docs/example"
      ServerName example.com
      ServerAlias www.example.com
      ErrorLog "logs/example/error_log"
      CustomLog "logs/example/access_log" common
   </VirtualHost>

2.2. For multiple virtual hosts

Go to '../conf/extra/httpd-vhosts.conf' in apache installation folder and modify the ‘httpd-vhosts.conf’ as shown below to setup named-based virtual host setting for one host.
   NameVirtualHost *:80

   <VirtualHost *:80>
      ServerAdmin example1@email.com
      DocumentRoot "/usr/local/apache2/docs/example1"
      ServerName example.com
      ServerAlias www.example1.com
      ErrorLog "logs/example1/error_log"
      CustomLog "logs/example1/access_log" common
   </VirtualHost>
 
   <VirtualHost *:80>
      ServerAdmin example2@email.com
      DocumentRoot "/usr/local/apache2/docs/sts2"
      ServerName example2.com
      ServerAlias www.example2.com
      ErrorLog "logs/example2/error_log"
      CustomLog "logs/example2/access_log" common
   </VirtualHost>
The above configuration is called as 'Name-Based Virtual Host'. In this configuration when webserver receives a request, it looks for the hostname in the HTTP header, and depending on the hostname, it serves the corresponding website.

Are you facing Forbidden 403 error?

If you are facing forbidden 403 error on accessing the host then set the options directive either in the global directory setting in the httpd.conf or in the specific directory block in httpd-vhosts.conf as:

Options Indexes FollowSymLinks Includes ExecCGI

By default, your global directory settings is (httpd.conf line ~188):
   <Directorty />
      Options FollowSymLinks
      AllowOverride All
      Order deny,allow
      Allow from all
   </Directory>
Set the options to: Options Indexes FollowSymLinks Includes ExecCGI

Finally, it should like as shown below:
   <Directorty />
      #Options FollowSymLinks
      Options Indexes FollowSymLinks Includes ExecCGI
      AllowOverride All
      Order deny,allow
      Allow from all
   </Directory>





Popular Posts

Write to Us
Name
Email
Message