Vanadium Vanadium

web design

WAMP Part 2 – virtual hosts

Leave a Comment

Part 2 continues from WAMP Part 1 – installation. Virtual hosts in Apache allow the server to run multiple sites at the same time, from whatever folders we want.

Enabling virtual hosts

  1. Open Notepad (you will have to right click it and Run as administrator to be allowed to save the file) and open C:\Program Files\Apache\Apache 2.4\conf\httpd.conf.
  2. Uncomment the line near the bottom to beome Include conf/extra/httpd-vhosts.conf. This allows you to use the httpd-vhosts.conf file for configuring your virtual hosts. Virtual hosts can be configured right in the httpd.conf file, but this keeps things more organized.

Adding virtual hosts

  1. In Notepad (Run as administrator) open C:\Program Files\Apache\Apache 2.4\conf\extra\httpd-vhosts.conf.
  2. Next add the following lines to allow your website files to be served by Apache. You can repeat this block for each website (note the Unix style forward slashes), or if you keep all of your websites in a common folder, just add the common folder to allow access to all of the websites in that folder. AllowOverride All has to do with .htaccess files, but is nice to have in there. <Directory “C:/Users/username/Websites”> Require all granted AllowOverride All </Directory>
  3. Replace the directives in the first <VirtualHost> section with directives for your primary website. The settings for the first VirtualHost override the default website that was set in the httpd.conf file in the DocumentRoot setting, and this becomes the website that is returned when a website request matches your computer’s IP address, but doesn’t match any of the named virtual hosts that you create.
    1. ServerAdmin – The email address of the server admin. This line can be removed to default to the ServerAdmin in httpd.conf.
    2. DocumentRoot – The location of the website. Note the Unix style forward slashes.
    3. ServerName – The URL for the website. The first one can be localhost, but additional ones will need different names. If my website URL is website.com, I like to use website.localhost for the local version.
    4. ServerAlias – An alternate URL for the website, if you want to be able to get to the same website with more than one URL. This line can be removed.
    5. ErrorLog – The error log file to use for this site. This line can be removed to default to the ErrorLog in httpd.conf.
    6. CustomLog – Same as the ErrorLog above. It can be removed to default to the ErrorLog in httpd.conf.
    7. AccessFileName – This is an optional one that I always add, and use the value .htaccess to allow the use of individual .htaccess files for each site.
  4. Add more virtual hosts by duplicating the whole <VirtualHost> section and replacing the directives with values for each additional site.
  5. Delete any <VirtualHost> sections that you aren’t using and save the file.
  6. Open the Apache Monitor from the system tray, and restart Apache.
  7. And unless you are using some external Domain Nameserver (DNS) to point your URLs at your computer’s external IP address, you will need to open C:\Windows\System32\Drivers\etc\hosts and add the website URLs here so that when you type them into your browser, your computer will know which IP address to go to (always 127.0.0.1 for your localhost). Copy one of the examples and paste it in at the bottom, uncommented, with your own URL. Create a new line for each URL. 127.0.0.1 website.localhost 127.0.0.1 website-2.localhost.
  8. More information can be found at http://httpd.apache.org/docs/2.4/vhosts/name-based.html.
  9. Save the file and try it out. Enter website.localhost into a browser, and you should get a different site than website-2.localhost.

Congratulations, you now have virtual hosts enabled to run multiple sites on your server.

Next

 

Leave a Reply

Your email address will not be published. Required fields are marked *