Setting Up A Virtual Host in Apache
Setting up a virtual host in the Apache web server is not exactly a PHP topic,but many PHP developers use the Apache web server to test web pages on theirdevelopment machine.
There is a lot of information around on how to do this,but the first time I tried it, I found the existing information to be moreconfusing than helpful. Hopefully, this page will simplify the processa bit. Please note that this information pertains to setting up a virtualhost in Apache on a Windows machine for use as a local testingserver. Setting up a virtual host for an actual production server is beyondthe scope of this article and you should refer to the official Apachedocumentationfor that.
The first file we'll need to edit is the Apache httpd.conf file.If you installed the Apache software using the download from the Apacheweb site, you should have a menu item that will open this file for editing.Click Start->Programs->Apache HTTP Server->Configure Apache Server->Editthe Apache httpd.conf Configuration File. If you don't have that start menuitem, start your text editor and open the file. It will be in a sub-foldernamed conf of your Apache folder. For example, mine is here:
C:\Program Files\Apache Group\Apache\conf\httpd.conf
Note that Apache changed the preferred method for configuring the Apacheserver with the release of Apache 2.2. For versions beginning with 2.2,the peferred configuration is more modular. Setting up a virtualhost as described here will still work with the newer versions, butto follow the modular approach, the editing of httpd.conf is only touncomment (remove the # from the beginning of the following line:
Everything elseis entered in the file httpd-vhosts.conf,which will be located in the extra folder below the below the foldercontaining httpd.conf. As mentioned, the method described here willstill work.
Version 2.2 also changed some of the default security configuration parameters.To set things up the way you'll need them, you'll need to add the followingblock to either your httpd.conf file, just above the virtual hosts,or to your httpd-vhosts.conf file:Order Deny,AllowAllow from all
The above assumes you're using the directory structure described below. Adjustthat as necessary to reflect your actual directory.
Now, for this example, we'll assume that you have your web sites locatedin a folder on your C drive called My Sites. Each web site has a sub-folderof its own under that folder, like this:C:\My Sites\Site1 C:\My Sites\Site2
Say also, for this example, that the domains for the two sites are site1.comand site2.com. We're going to set up virtual hosts for those two sites usingthe domain names site1.local and site2.local. That way, you'll be able to tellat a glance whether you're looking at the live site, or your testing site.
In reality, you can call the domains anything you want. You could just aseasily name them microsoft.monkeybutt and ibm.greentambourine. I choose touse the convention of using the same domain name along with the .local TLDto simplify and minimize the typing needed to switch between the live siteand the testing site. The only important point, and it's really important,is that you NEVER use an actual, real, live domain name. Ifyou used, for example, site1.com for the local virtual host, you would never beable to actually reach the live site. All requests for the live site wouldbe re-directed to your local virtual host.
Go to the very bottom of your httpd.conf file in your text editor. You shouldsee an example of a virtual host there. Each line of that example will beginwith an octothorpe (#). The octothorpe character marks the line as a comment,so the example is not executed. Add the following lines below that example:NameVirtualHost 127.0.0.1 DocumentRoot "C:\My Sites\Site1" ServerName site1.local DocumentRoot "C:\My Sites\Site2" ServerName site2.local
That's all you need to do! Save and close the file. That will tell the Apacheserver everything it needs to know in order for it to serve the pages usingthe domain names site1.local and site2.local. One note is that, in the aboveexample, we have a space in the path. Because of that, we put quotation marksaround the document root directory. If the path does not have any spaces init, do not quote the path. If the directory used for your sites were, for exampleMySites instead of My Sites, the document root line would look like this instead:DocumentRoot C:\MySites\Site1
Obviously, if you typed http://site1.local in your browser, it would notbe found by your Internet provider's DNS server.We're next going to edit another file to work around that. The second fileyou need to edit is called hosts, with no file extension.It is a Windows system file and it will enable you to enter specific addressesfor specific domains instead of using a DNS lookup. The normal location forthis file is:
If you don't find it there, do a search in your windows directory for theword hosts in the file name. The file you want is called hosts, with no fileextension. The correct file will begin with the following lines:# Copyright (c) 1993-1999 Microsoft Corp.## This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
Once again, in this file, the octothorpe character is a comment marker. Linesbeginning with it are comments. In all likelihood, there will be nothing there,except for comments. If there are any other non-commented entries, leave themalone. Just go to the bottom of the file, below all the comments and any existingentries and add the following two lines:127.0.0.1 site1.local127.0.0.1 site2.local
That's all you need to do there. Save and close the hosts file.
You're almost done! The only remaining thing you need to do is to re-startthe Apache server. You need to do this because Apache only reads the configurationfile when it first starts up. Click Start->Programs->Apache HTTP Server->ControlApache Server->Restart. If you don't have that menu item, open a commandprompt and change to the Apache directory, and type the following command andpress the Enter key:
apache -w -n "Apache" -k restart
You should see a message like this:
The Apache service is restarting.The Apache service has restarted.
That's it! You're done! Close the command window and start your web browser.In the browser's address bar, type http://site1.local and hit the Enter key.You should now see your local copy of your site1.
Okay, now I'll mention one very small, but possibly important, caveat. Whenyou create the virtual hosts like this, the default http://localhost will nolonger work. In many cases, that is unimportant. However, if you're using somethinglike phpMyAdmin, you'll still need it. The solution to that is to create oneadditional virtual host called "localhost"that points to the original Apache htdocs folder. It might look somethinglike this:DocumentRoot C:\Apache\htdocs ServerName localhost
Don't forget to include that additional virtual host when you edit the Windowshosts file.
Note that there are other optional settings you can use to configure thevirtual host. The above uses only two lines and that's all that's really necessary.You can read about other options in the Apachedocumentation. Note that this link is to the Apache web site and it willopen a new browser window.
I hope you found this more helpful than confusing. Good luck!