Creating Virtual Hosts

Virtual Hosting is a method of hosting multiple websites (domain names like abc.com, great.com etc) on a single server. There are two main types of virtual hosting, name based and IP based.

  • “name-based”: We have multiple names running on each IP address. It is sometimes also called host-based or non-IP virtual hosts.
  • “IP-based”: We have a different IP address for every web site

Versions 1.1 and later of Apache support both IP-based and name-based virtual hosts (vhosts).

Sample virtual host block that can be placed in httpd.conf file to create a virtual host

NameVirtualHost 172.20.30.40:80

<VirtualHost 172.20.30.40:80>
ServerName example.com
DocumentRoot "/var/www/html/example"
<Directory "/var/www/html/example">
Options FollowSymLinks Includes
AllowOverride all
Order allow,deny
Allow from all
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
</Directory>
</VirtualHost>

Apache 2.2 adopts a modular approach to its main configuration file, httpd.conf.  We can use external files and include the needed ones. For virtual host we can include httpd-vhosts.conf.

 

Here is how you can create and run virtual hosts on your local machine

  1. Add entry in the hosts fileOpen C:\WINDOWS\system32\drivers\etc\hosts in notepadAt the end of the file you will see entry like127.0.0.1       localhostAdd another entry on next line127.0.0.1       testvhost.com

    Now save the document.

  2. Open Apache configuration file Apache2.2\conf\httpd.conf in notepadSearch for
    #Virtual hosts
    #Include conf/extra/httpd-vhosts.conf
    

    and uncomment  the second line so that it looks like

    #Virtual hosts
    Include conf/extra/httpd-vhosts.conf
    

    Save and close it

  3. Open Apache2.2\conf\extra\httpd-vhosts.conf file in editorThe file has the following code
    #
    # You may use the command line option '-S' to verify your virtual host
    # configuration.#
    # Use name-based virtual hosting.
    #
    ##NameVirtualHost *:80#
    # VirtualHost example:
    # Almost any Apache directive may go into a VirtualHost container.
    # The first VirtualHost section is used for all requests that do not
    # match a ServerName or ServerAlias in any <VirtualHost> block.
    #
    ##<VirtualHost *:80>
    ##    ServerAdmin [email protected]
    ##    DocumentRoot /www/docs/dummy-host.example.com
    ##    ServerName dummy-host.example.com
    ##    ServerAlias www.dummy-host.example.com
    ##    ErrorLog @[email protected]/dummy-host.example.com-error_log
    ##    CustomLog @[email protected]/dummy-host.example.com-access_log common
    ##</VirtualHost>##<VirtualHost *:80>
    ##    ServerAdmin [email protected]
    ##    DocumentRoot /www/docs/dummy-host2.example.com
    ##    ServerName dummy-host2.example.com
    ##    ErrorLog @[email protected]/dummy-host2.example.com-error_log
    ##    CustomLog @[email protected]/dummy-host2.example.com-access_log common
    ##</VirtualHost>
    

    All the lines are commented out

    Remove comment from the line

    ##NameVirtualHost *:80
    

    so that it looks like

    NameVirtualHost *:80
    
    

    Now uncomment the <VirtualHost *:80></VirtualHost> block lines and modify them so that we have

    <VirtualHost *:80>
    DocumentRoot C:\htdocs\testvhost
    ServerName testvhost.com
    </VirtualHost>
    

    Now save the file.

  4. Restart the Apache server.
  5. Now in browser http:\\testvhost.com\ and you can access the site hosted in folder C:\htdocs\testvhost
  6. You can create C:\vhosts folder to host all your virtual host sites. Then add
    <Directory C:/vhosts>
    Order Deny,Allow
    Allow from all
    </Directory>
    

    in Apache2.2\conf\extra\httpd-vhosts.conf above

    NameVirtualHost *:80
    

    to set the proper permission for the hosting directory

  7. If you are connected to internet through a proxy server then you will need to add “testvhost.com” in the browser setting “No Proxy for” setting.
  8. Creating Virtual host disables the main server, it means you will no longer be able to access http:\\localhost it will take you to http:\\testvhost.com\ in our case. Add the following as the first virtual host declaration so that main server will always be accessible
    <VirtualHost *:80>
    DocumentRoot c:/htdocs
    ServerName localhost
    </VirtualHost>
    

References

  1. Virtual Hosting
  2. Apache Virtual Host documentation
  3. Apache Virtual Host Examples

Leave a Comment

Back to top