Custom Search
 


How to use Apache Virtual Host to run multiple local websites on Windows



If you have installed Apache, PHP, and MySQL on Windows, you are ready to build your first dynamic, database-driven website. What if you later need to build a second website or third? With a little bit modifications in Apache's configuration file httpd.conf and Windows hosts file, you can set up and run multiple local websites like this:
  1. http://local_website1

  2. http://local_website2

  3. http://local_website3

  4. ...

To set up multiple local sites, we need to modify two files: Windows hosts file and Apache's configuration file.

Modify Windows hosts file When we set up Apache on Windows, I used local site at C:\geeksengine for illustration purpose. The site at C:\geeksengine can be accessed by either of the following URLs:

  1. http://localhost
  2. http://127.0.0.1
  3. http://169.254.32.57 or 192.168.0.1 (eg. local network IP address)

Wouldn't be nicer if I can access the site like this http://geeksengine (which can instantly tell me I run a local site rather than my internet site http://www.geeksengine.com)? To achieve this effect, we need to modify Windows hosts file.

What is Windows hosts file

Hosts file is a simple text file which contains the mappings of IP addresses to human-recognisable host names (e.g. domain names like http://www.geeksengine.com). It's similar to the structure of internet name servers (a.k.a domain name servers) that implements Domain Name System (DNS) protocol.

Windows hosts file is used by Microsoft TCP/IP for windows. Each entry in hosts file 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. In addition, Comments may be inserted on individual lines by prefix the line with a '#' symbol.

Content in a hosts file can be like this:

127.0.0.1  geeksengine 127.0.0.1  local_website2 127.0.0.1  local_website3 127.0.0.1  phpbb

How hosts file works

  1. When you go to a website via your web browser, the first thing Windows does is check hosts file to see if the website URL is already listed in its hosts file.

  2. If the URL is not listed in hosts file, it goes out to the Internet to find out where to send your request.

  3. If the URL is listed in hosts file, your browser direct your request to the mapped IP address which can be either a local IP address (as in our case for multiple sites) or an external Internet IP address.

  4. If the IP address is a local IP (e.g. 127.0.0.1), the browser sends your request to your local Apache web server.

Where to find hosts file

You can locate and open the hosts file by a text editor such as Notepad.

Operating System File Location on Hard Disk
Linux/Unix /etc/hosts
Windows 3.1/95/98/ME C:\windows\hosts
Windows NT/2000/XP Pro C:\WINNT\system32\drivers\etc\hosts or C:\windows\system32\drivers\etc\hosts
Windows XP Home C:\windows\system32\drivers\etc\hosts

You may have noticed I have an entry in my hosts file for geeksengine. This entry maps from IP address 127.0.0.1 to fake host name geeksengine.

127.0.0.1 ==> geeksengine

Actually, the IP address can be any of your local loopback IPs such as 127.0.0.2, 127.0.0.3 or 192.168.0.1. This way, I can run geeksengine.com locally on my computer as http://geeksengine for development use.

You can edit your hosts file and map your local site(s) to whatever domain name you like. You can even map google.com to IP 127.0.0.1. If you do, everytime you go to http://www.google.com, your localhost website will be displayed.

Modify your hosts file

Now open your hosts file and add entry 127.0.0.1  local_website2 and save it.

Windows Vista and Windows 7 have stricter security mechanism that makes changing the hosts file a lot harder. Please refer to Microsoft Knowledge Base article http://support.microsoft.com/kb/923947 to see how to modify the Hosts file in Windows Vista and Windows 7.

Windows hosts file entries

If your computer uses a decent anti-virus software, it should warn you by popping up an alert window about the modifications made in hosts file. This is because hosts file can be the target of some malware or spyware such as browser hijacker.

Click the button on the alert window to accept the change.

Modify Apache configuration file In article How to install Apache on Windows, I set up GeeksEngine as the default local website for Apache. To achieve that, I added in two entries in Apache's httpd.conf file to set up DocumentRoot directory out of which we will serve our local default website:

Set documentroot folder

and

Set documentroot directory

Your site may be at a different directory. I normally use C drive as I ghost my system on regular basis to backup the whole computer.

Now, it's the fun part. To run a second site (e.g. http://local_website2) on your local Windows system, follow these steps:

Step #1: Create the test PHP page

  1. Create a new folder on C drive, e.g. C:\site2

  2. Add a PHP page index.php in this new folder.

  3. Add the following code in index.php page and save it:


    <?
    echo phpinfo();
    ?>

Step #2: Add a virtual host in httpd.conf file

  1. Open httpd.conf file.

    Start -> Programs -> Apache HTTP Server -> Configure Apache Server -> Edit the Apache httpd.conf Configuration file

    Edit Apache configuration file

  2. When the configuration file is opened, move to the bottom of the file - Section 3: Virtual Hosts.

  3. Add a VirtualHost directive for the second site:

    You can copy and paste code below into your configuration file:

    <VirtualHost 127.0.0.1>
          ServerAdmin webmaster@local_website2
          DocumentRoot "C:\site2"
          ServerName local_website2
          ErrorLog logs/local_website2-error.log
          CustomLog logs/local_website2-access_log common
            
          <Directory C:\site2>
                 AllowOverride All
          </Directory>

    </VirtualHost>

    Virtual hosts allow web requests to be sent to different IP addresses or hostnames and have them handled by the same Apache server process.

    Note that adding AllowOverride All directive to the virtual host is important because it allows the information defined in .htaccess file for site http://local_website2 to override earlier access information define in httpd.conf. For example, if you have defined URL rewrite rules in C:\site2\.htaccess but didn't add AllowOverride All directive in virtual host, the rewrite will not work for this virtual host website.

  4. Save httpd.conf and restart Apache web server. Remember this: every time you make a change to Apache configuration file, you need to restart Apache server to make the change take effect.

Step #3: Test the virtual host site

To test if our newly added site works, open your web browser and type http://local_website2 into address bar and hit Enter. If you can see the PHP configuration info as shown below, your setup for the second local website has worked. If it didn't work, restart your Apache server and try again.

PHP Info

I hope you are able to set up your second site running on the same computer using Apache Virtual Hosts. You can add unlimited sites to your computer and one Apache serves them all.


Copyright© GeeksEngine.com



Related Articles:

1.Steps to install PHP 5.x on Windows as a development machine
2.How to install Apache 2.x web server on Windows
3.How to connect two different versions of MySQL server on the same computer
4.How to configure MySQL server 5.1 on Windows
5.How to install MySQL server 5.1 on Windows with screenshots
6.Five ways to create include path for PHP
7.How to use Date and Time data as integer value in PHP and MySQL
8.Install all PEAR packages by yourself
9.How to install PEAR on Windows
10.How to use PHP and Microsoft SMTP Virtual Server to send emails
11.How to install PHP server-side scripting language on Windows
12.How to install Apache 1.3 web server on Windows
13.How to install two different versions of MySQL server on the same PC
14.How to configure MySQL server 4.1 on Windows
15.How to install MySQL server 4.1 on Windows with screenshots
16.Export Northwind Access database to MySQL via ODBC


Other Recent Articles from the WAMP & LAMP category:

1.Steps to install PHP 5.x on Windows as a development machine
2.How to install Apache 2.x web server on Windows
3.How to connect two different versions of MySQL server on the same computer
4.How to configure MySQL server 5.1 on Windows
5.How to install MySQL server 5.1 on Windows with screenshots
6.How to upgrade from PHP4 to PHP5
7.How to load time zone data for MySQL on Windows
8.Install all PEAR packages by yourself
9.How to install PEAR on Windows
10.How to use PHP and Microsoft SMTP Virtual Server to send emails
11.How to install PHP server-side scripting language on Windows
12.How to install Apache 1.3 web server on Windows
13.How to install two different versions of MySQL server on the same PC
14.How to configure MySQL server 4.1 on Windows
15.How to install MySQL server 4.1 on Windows with screenshots
16.How to set up DSN in ODBC Data Source Administrator on Windows
17.Export Northwind Access database to MySQL via ODBC

Copyright © 2014 GeeksEngine.com. All Rights Reserved.

This website is hosted by LunarPages.

No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it.

 
Home | Feedback | Terms of Use | Privacy Policy