Sunday, February 5, 2012

Multiple virtual hosts with Apache (on windows)

Creating multiple virtual hosts using apache is so common, you may find making it so easy and straight forward on Linux or even Mac, but believe me it is not the case with Windows !​


   Here is the deal, the idea behind how to make this is to give another IP for your new host, and make apache listen on that IP, let's do it:

Step 1: add your new host in the hosts file under "C:\Windows\System32\drivers\etc​" and assign new IP to it:
  1. 127.0.0.1       localhost  
  2. 127.0.0.2       mysite.com  
Step 2: make your apache server listens on the new IP by modifying httpd.conf file in the "apache\conf" folder, put the following in the Listen section:
  1. Listen 80  
  2. Listen 127.0.0.2:80  
Step 3: and finally, open "apache\conf\extra\httpd-vhosts.conf" and add your new virtual hosts like this:
  1. <virtualhost mysite.com:80>
  2. DocumentRoot "absolute_location_to_mysite_public_folder"
  3. ServerName Boston.com
  4. ServerAlias boston.com
  5. <directory "absolute_location_to_mysite_public_folder">
  6. AllowOverride All
  7. Options Indexes FollowSymLinks
  8. Order allow,deny
  9. Allow from all
  10. </directory>
  11. </virtualhost>

  12. <virtualhost localhost:80>
  13. DocumentRoot "apache_htdocs_folder"
  14. ServerName localhost
  15. ServerAlias localhost
  16. <directory "apache_htdocs_folder">
  17. AllowOverride All
  18. Options Indexes FollowSymLinks
  19. Order allow,deny
  20. Allow from all
  21. </directory>
  22. </virtualhost>








Finally, you can use the two hosts in your browser:
  • localhost
  • mysite.com

No comments:

Post a Comment