Google

Wednesday, April 22, 2009

Apache - Making Localhost and Virtual Hosts (vhosts) Work Together

I’ve been searching for this for quite sometimes almost forget about it.  Anyway, I’ve found the solution here to share…

If you have a situation that you want to round your live site from your machine, you can always change the hosts file and your conf file.  Example, you want to make local version www.mysite.com accessible

Add setting in your Apache Configuration file (httpd.conf), for me I prefer to add in a new file so that it will not mixed up

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

Include conf/extra/mysite-vhosts.conf

 

In mysite-vhost.conf, you have to set

<VirtualHost *:80>
ServerAdmin anyemailaddress@anymail.com
DocumentRoot "C:/Apache/htdocs/mysite.com"
    ServerName www.mysite.com
    ErrorLog "logs/mysite.com.log"
    CustomLog "logs/mysite.com-access.log" common
</VirtualHost>

 

Pre-create the log file if Apache prompt you error when restarting.

Next, change your hosts file to (your hosts file located in [Windows] C:/windows/system32/drivers/etc/hosts)

Do this: (you can access via mysite.com or www.mysite.com)

127.0.0.1       localhost
127.0.0.1       mysite.com
127.0.0.1       www.mysite.com

 

If you restarted your apache right now and have other file that located in your htdocs folder, these site will not be accessible, for example your http://localhost/test will not work.  The solution:

<VirtualHost _default_:80>
    DocumentRoot C:/Apache/htdocs
</VirtualHost>

Restart your apache and DONE!

Enjoy coding!!!

No comments: