18 Jan 2013

Apache #1: Set up Virtual Hosts on Windows

For Windows

In order to set up your PC to work with multiple local sites, you need to find two files:

  • C:\Windows\System32\drivers\etc\hosts
  • C:\xampp\apache\conf\extra\httpd-vhosts.conf

Note that the second file may be in a different location, depending on how and where Apache is installed. The above shows the location when XAMPP installed to root.

hosts file

You'll notice that this file does not have an extension. It may contain the following when you open it up for the first time:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host
 
# localhost name resolution is handled within DNS itself.
 127.0.0.1       localhost
 ::1             localhost

Now add the server name of your choice. I usually keep this simple, adding a '.local' so that I can recognise it easily as a local server in the address bar. For example a site called 'example.com' could have a local server name of 'example.local', but there is no need too keep these similar, you could call the server name something like 'myamazingsite.local' if you really wanted to. So, in order to add server names, you can do something like this:

# localhost name resolution is handled within DNS itself.
127.0.0.1       localhost
::1             localhost
# Add virtuals below

    127.0.0.1  diafol.local
    127.0.0.1  chemistry.local

httpd-vhosts.conf

OK, that the first bit done. Now we add an entry to the vhosts file. When you first open the file, it may look like this:

# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
##NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any VirtualHost> block.
#
##<VirtualHost *:80>
  ##ServerAdmin postmaster@dummy-host.localhost
  ##DocumentRoot "C:/xampp/htdocs/dummy-host.localhost"
  ##ServerName dummy-host.localhost
  ##ServerAlias www.dummy-host.localhost
  ##ErrorLog "logs/dummy-host.localhost-error.log"
  ##CustomLog "logs/dummy-host.localhost-access.log" combined
##</VirtualHost>

##<VirtualHost *:80>
  ##ServerAdmin postmaster@dummy-host2.localhost
  ##DocumentRoot "C:/xampp/htdocs/dummy-host2.localhost"
  ##ServerName dummy-host2.localhost
  ##ServerAlias www.dummy-host2.localhost
  ##ErrorLog "logs/dummy-host2.localhost-error.log"
  ##CustomLog "logs/dummy-host2.localhost-access.log" combined
##</VirtualHost>

In order to add the two sites from the hosts files, simple append something like the following to the end of the file:

NameVirtualHost *:80
<VirtualHost *:80>
  DocumentRoot "C:\xampp\htdocs"
  ServerName localhost
</VirtualHost>
<VirtualHost *:80>
  DocumentRoot "C:\xampp\htdocs\diafol"
  ServerName diafol.local
  <Directory "C:\xampp\htdocs\diafol">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>
<VirtualHost *:80>
  DocumentRoot "C:\xampp\htdocs\chemistry_advanced"
  ServerName chemistry.local
  <Directory "C:\xampp\htdocs\chemistry_advanced">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

Remember to restart Apache!

2 comments: