2

Apache: Setting virtual host on localhost

It was up to this task since yesterday. I haven’t done this before, setting sub domain- virtual host on local system. I tried a lot yesterday but in vain, however when I came to office today I configured it successfully.
As I was stuck in the middle and feel lot of pain, so I better write it for those facing the same issue.
Why I was up to this task?
Well, actually my client was facing problem and reported that session is not working properly on domain and sub domain. So I was having no way other than setting virtual host.
However before leaving yesterday, I talked to my client that I am unable to solve this problem. He told me that I shouldn’t be worried about it because he will take care of this issue. The more important thing is that when I arrived today, I found that the problem has already been solved. Thanks to him for giving my favor.
Thought the problem has been solved, but still is worthy to write a small post here because I have successfully solved the problem.
Before going to discuss it, I would like to tell you that the configuration has been taken place on window so the paths may not be same.
Let have a look.
First of all you will need to open.

C:\xampp\apache\conf\extra\httpd-vhosts.conf

I assumed that you have install xampp.
And add following lines to httpd-vhosts.conf file.

NameVirtualHost *:80


    ServerName localhost
    DocumentRoot "C:/xampp/htdocs/"
    ServerAdmin admin@localhost



    ServerName mysite.com.localhost
    DocumentRoot "C:/xampp/htdocs/mysite/"
    ServerAdmin admin@mysite.com.localhost

    Options Indexes FollowSymLinks
    AllowOverride FileInfo
    Order allow,deny
    Allow from all

Here I have defined server localhost pointing to my default htdocs directory and another host mysite.com.localhost pointing to “C:/xampp/htdocs/mysite/”.
Another important thing is
Here you would need to specify the port. In my case its 80.
If you don’t specify the port on which apache listen, apache may take you to the same page for both the server. So be careful.

Next go to your Window/system32/dirvers/etc/ and open hosts file.
You will find

127.0.0.1       localhost

At the end of the file. Add following line next to it.

127.0.0.1    mysite.com.localhost

Reboot your system and you are done.

Filed in: Apache Tags: ,

2 Responses to "Apache: Setting virtual host on localhost"

  1. chrisweb says:

    Nice tutorial!

    Just wanted to mention that i had problems with cookies when setting up my first vhost on my local xampp. The problem was i only had one dot in my domain:

    127.0.0.1 mysite.dev

    the cookies did not work. I changed it to:

    127.0.0.1 http://www.mysite.dev

    then it worked. There reason is this:

    "Cookies can be assigned to individual machines or an entire Internet domain. The only restrictions on this value is that it must at least contain two dots" http://www.cookiecentral.com/faq/

  2. typografia says:

    Here is a little batch script automating this process:
    Virtual host manager for Windows

Leave a Reply

Submit Comment

© 9761 Faheem Abbas. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
Top Footer