Creating virtual host on apache, php

You can create a virtual host on your local system or your sever. i.e. you can have http://trial/ instead of http://localhost/trial/ where trial is the name of the directory that contains the website.

Here are the steps for creating virtual host on your sever:

1) open the host file and edit it as follows:

the host file is found at C:\WINDOWS\system32\drivers\etc\hosts for windows

open the file and add the following code to the bottom of the file on a new line       trial

2) Now open the httpd.conf file under the apache/bin/conf/ directory

and add the following code :  # Virtual hosts
Include conf/extra/httpd-vhosts.conf

or if already present then please uncomment the following line
: Include conf/extra/httpd-vhosts.conf

i.e. remove the # from the front of the line

3) Open apache/bin/conf/extra/httpd-vhosts.conf file

if not present create directory extra under

and create a file httpd-vhosts.conf file

and add this code to the file:

# Virtual Hosts
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn’t need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
# Please see the documentation at
# <URL:;
# for further details before you try to setup virtual hosts.
# You may use the command line option ‘-S’ to verify your virtual host
# configuration.
<Directory D:/wamp/www/vhosts>
Order Deny,Allow
Allow from all
# 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 webmaster@dummy-host.localhost
#    DocumentRoot “C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host.localhost”
#    ServerName dummy-host.localhost
#    ServerAlias http://www.dummy-host.localhost
#    ErrorLog “logs/dummy-host.localhost-error.log”
#    CustomLog “logs/dummy-host.localhost-access.log” common

#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host2.localhost
#    DocumentRoot “C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host2.localhost”
#    ServerName dummy-host2.localhost
#    ErrorLog “logs/dummy-host2.localhost-error.log”
#    CustomLog “logs/dummy-host2.localhost-access.log” common

<VirtualHost *:80>
DocumentRoot D:/wamp/www
ServerName localhost
<VirtualHost *:80>
DocumentRoot D:/wamp/www/vhosts/trial
ServerName trial

if the file is present then please add the following lines at the bottom of the file

<VirtualHost *:80>
DocumentRoot D:/wamp/www
ServerName localhost
<VirtualHost *:80>
DocumentRoot D:/wamp/www/vhosts/trial
ServerName trial

4) restart your apache services and just type http://trial/ into the address bar of your browser and it will work

If you face any problem with this please feel free to contact me. I’ll try to resolve your problem as soon as possible.


Sachin (

About these ads

7 thoughts on “Creating virtual host on apache, php

  1. hi,
    i try to do this. everything is same above. but is is still going to default server root . i have added rb.zfnews to hosts file. vhosts file is

    Order Deny,Allow
    Allow from all

    DocumentRoot C:/wamp/www
    ServerName localhost

    DocumentRoot C:/wamp/www/zfNews
    ServerName rb.zfnews

  2. First of thanks…..this blog was quite useful to me.
    I have query in this regard. Actually i have two servers (Apache & Tomcat running on the same IP Address) and two apps app1 and app2 running on apache and tomcat respectively. What i need is that whenever user types, it will be redirected to app1 running in apache and when it types it is redirected to app2 running in tomcat. Is this kind of setup possible through virtual hosting

  3. hi sam
    thanks for your reply. Actually both the web servers are running on the same machine but on differrent ports. I solved the problem myself by integrating apache and tomcat using mod_jk connector. I made these changes in the httpd-vhosts file

    DocumentRoot “C:/wamp/www”
    JkMount /app2/* ajp13w
    ErrorLog logs/app2-error_log
    CustomLog logs/app2-access_log common

    Now if request comes for app2 it will be redirected to tomcat and others will be handled by Apache itself.
    [Notice JkMount statement]

    • hi suki,

      actuallly i should be thanking you for this information.
      and its great that you were able to sort out the problem youself.
      keep it up!!!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s