Tuesday, July 14, 2009

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


<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs/"
ServerAdmin admin@localhost
</VirtualHost>


<VirtualHost *:80>
ServerName mysite.com.localhost
DocumentRoot "C:/xampp/htdocs/mysite/"
ServerAdmin admin@mysite.com.localhost
<Directory "C:/xampp/htdocs/mysite/">
Options Indexes FollowSymLinks
AllowOverride FileInfo
Order allow,deny
Allow from all
</Directory>


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.

9 comments:

  1. 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 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/

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

    ReplyDelete
  3. Hi, just one small note:

    If you are developing on Windows, you don't need to reboot the system after you changed your hosts file.
    On the command prompt enter: "ipconfig /flushdns" (without the quotes), and voila, you are done!

    greetz,

    Karim

    ReplyDelete
  4. But what if you want to also have the www version on your localhost, www.mysite.com.localhost

    Is that also possible?

    ReplyDelete
  5. Being a beginner in hosting field, your post is highly useful to me. Thanks for sharing such a nice information. Keep on sharing more of these.web hosting provider,

    ReplyDelete
  6. Though I have done all setting,I am getting wamp homepage on addbook.local.

    Can You say why it is happening???

    ReplyDelete
  7. I found great points in your post its really important post for me. Keep sharing!!
    Web hosting

    ReplyDelete
  8. Webcare360 Provides you safe and secure cheap Offshore Hosting Best offshore hosting and best offshore hosting with 99.9% Up time Guarantee, with Ddos protection.

    ReplyDelete
  9. I found great points in your post its really important post for me. Keep sharing!!
    property dealers in Islamabad

    ReplyDelete