If you do web development on WAMP or LAMP, but not really adept in general network configurations, your typical local development web content might have URLs similar to as follows:
- http://localhost/projecta
- http://localhost/projectb
- http://localhost/projectc
- http://localhost/projectc-beta2
Wouldn’t it be nicer if those projects have URLs such as:
- http://projecta
- http://projectb
- http://projectc
- http://projectc-beta2
Continue reading on if you’re interested to find out how this can be achieved on both Windows and Linux computers. This writeup will cover setting up the hosts as well as configuring Apache (versions 1.3.x) to support the hosts.
This tutorial is designed for those who have administrative priviledges on the development computer (under Administrators group on Windows, or have root access for Linux). More often than not, if you’re not using a shared computer, you’ll most likely have such access.
Setting Up the Fake Hostnames
A Primer on Hostnames: Hostnames are names for computers connected on a network. For HTNet, its hostname is www.heritage-tech.net (and also heritage-tech.net). For the nitty-gritty details, please refer to Hostname on Wikipedia.
The fake hostnames such as projecta, projectb, projectc and projectc-beta2 can be set in either of these hosts configuration files (also referred to as hosts files) depending on your operating system:
- For Windows:
C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS(assuming your Windows installation is inC:\WINDOWS). - For Linux:
/etc/hosts
If you were to open these files in a text editor, you’ll see content similar to the following:
127.0.0.1 localhost 192.168.1.18 testhost.testdomain.com testhost
All hosts files will have the localhost entry which resolves to 127.0.0.1, also known as the loopback address. Basically, what the first line in the host file illustrated above tells us is to route all network packets addressed to localhost to the computer you’re using.
So to get our fake hostnames working, they each should have an entry resolving to 127.0.0.1 in your hosts file. Example as follows:
127.0.0.1 localhost 127.0.0.1 projecta 127.0.0.1 projectb 127.0.0.1 projectc 127.0.0.1 projectc-beta2 192.168.1.18 testhost.testdomain.com testhost
To test the configuration, you should ping the fake hosts and if you had followed the steps above correctly, you should get a response from 127.0.0.1 for the projecta, projectb, projectc and projectc-beta2 hosts. This concludes the host resolving section of this tutorial.
Configuring Apache
The next step involves configuring Apache to handle requests to the hosts defined above. To do this, you’ll need to edit your Apache installation’s configuration file; httpd.conf. On a default Apache for Windows installation, this file will be in C:\Program Files\Apache Group\Apache\conf\. For Linux, the location of the httpd.conf file usually depends on the distribution. On Slackware Linux, it’s in /etc/apache/.
Open the httpd.conf file in your favourite text editor and look for a line that starts with DocumentRoot. This line sets the location for the files that will be served as your default web site.
On Linux, it will look something like the following (Note that the actual location may differ on your setup):
DocumentRoot "/var/www/htdocs"
Whereas on Windows, it will be similar to (Note that the actual location may differ on your setup):
DocumentRoot "C:/Program Files/Apache Group/Apache2/htdocs"
Also note that for Windows, Apache can also use a normal slash (/) instead of the typical backslash (\) in the directory path. You should also enclose directory paths in quotes. From now on, we’ll refer to these directories as DocumentRoot.
If the directories where you want to serve content for the hosts created earlier reside within the DocumentRoot directory, you merely need to define a VirtualHost declaration for each of them. Let’s assume that the files for http://projecta will be in the directory named projecta under the DocumentRoot directory (which will be /var/www/htdocs/projecta/ for Linux or C:\Program Files\Apache Group\Apache2\htdocs\projecta\ for Windows).
Therefore, at the end of your httpd.conf file, append the following lines to create the projecta virtual host:
For Linux:
<VirtualHost *>
DocumentRoot /var/www/htdocs/projecta
ServerName slackbox
</VirtualHost>
For Windows:
<VirtualHost *>
DocumentRoot "C:/Program Files/Apache Group/Apache/htdocs/projecta"
ServerName slackbox
</VirtualHost>
If the files are served from a directory outside of the DocumentRoot, you need to create a Directory declaration for each of the directories in addition to the VirtualHost declaration.
For example, let’s assume that files from projectb will be served from /web/projectb (for Linux) or C:\web\projectb (for Windows). Therefore, the following lines should be appended to your httpd.conf file:
For Linux:
<Directory "/web/projectb">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *>
DocumentRoot /web/projectb
ServerName projectb
</VirtualHost>
For Windows:
<Directory "C:\web\projectb">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *>
DocumentRoot "C:\web\projectb"
ServerName projectb
</VirtualHost>
Testing the Setup
Once all of the above is done, you can now test your configuration. However, you need to restart the Apache service in order to apply the changes made above. Here’s how to do it if you’ve installed Apache as a service (9 times out of time, this is how it’s normally installed):
For Linux (distros using BSD-style init like Slackware):
/etc/rc.d/rc.httpd restart
For Linux (distros using sysv-style init like Red Hat):
service httpd restart
For Windows:
- Run:
services.msc- Look for Apache in the services list and select it
- Click the Restart Service button
If you’ve followed through this guide closely, you should now have a working Apache installation with multiple named local sites.
Was this guide useful for you? Please digg it or drop me a comment or two.
Update: This guide is for Apache 1.3.x. To get local VirtualHosts to work with Apache 2.0.x, please click here.










November 30th, 2006 at 7:05 pm
urm. confusing. I dont understand how to point it out of the whole apache folder, to a folder in my documents.
December 1st, 2006 at 7:42 am
Hi mike,
If you want to change the main web serving folder to a different location, it really is very simple. You say you want to point it to a folder in My Documents, so I assume you’re using Windows XP.
The real path to My Documents is:
C:\Documents and Settings\<Your Name>\My Documents\. So if you want to serve your web pages from a folder called “Webs” in My Documents, just set your DocumentRoot setting as follows:December 17th, 2006 at 4:36 am
Excellent straight to the point. Thanks ever so much
December 29th, 2006 at 1:55 am
Hi,
I try to find out how to get my server on mye local computer to be similar to the one on the web..
example:
On the webserver where my homepage is:
/home/triveran/public_html/
On my local computer:
C:\Programfiler\xampp\htdocs\homepage\
this two has to be the same, because i use php on my page and some of these are pointing to a directory where i have to have with all the path… like /home/triveran/public_html/…
anyone help me?
Thanks
January 2nd, 2007 at 10:48 am
Hi Torjokim,
Assuming you’re using a premade script (whether open source or otherwise), a properly designed one should have a configuration file to define the root directory.
This is not an Apache-specific issue but more related to your script’s installation method.
I suggest going through the manual or help file to determine where your root directory definition is stored.
All the best.
January 10th, 2007 at 4:22 am
I have been trying this for quite some time, and been running into a problem. First of all, I am using WAMP 1.6.6 from wampserver.com
I set up my hosts properly, I can ping them etc.
I then set this up in my httpd.conf file:
DocumentRoot “C:/wamp/www/__MY_PROJECTS__/test”
ServerName test
Now, whenever I load http://localhost/ it goes to the test directory. The same thing happens if I go to http://test/
I also tried setting up multiple virtualhosts, and the server always displays the directory specified in the FIRST virtualhost that is in the httpd.conf file.
Anyone know whats going on?
January 10th, 2007 at 2:50 pm
Hi Danny,
When using NameVirtualHost on Apache, the first VirtualHost declaration will be used as the default web site. Here’s my httpd.conf’s VirtualHost section:
Does yours look similar?
March 22nd, 2007 at 6:01 pm
[...] that resembles production and will also help learn Apache. Here is a good article “Setting Up Multiple Apache Local Web Sites On Your Computer” that provides details of how to do [...]
March 27th, 2007 at 10:42 am
Hey,
Thanks a lot for the article. I was searching for this for a long time. But, I am getting the same error Danny said above.
Here is my conf file entry
NameVirtualHost *
DocumentRoot “C:/Apache2/htdocs/sitec”
ServerName localsitea
DocumentRoot “C:/Apache2/htdocs/siteb”
ServerName localsiteb
DocumentRoot “C:/Apache2/htdocs/sitea”
ServerName localsitec
Can you please help me?
Thanks
March 27th, 2007 at 11:00 am
I got the answer, thankyou
March 30th, 2007 at 3:46 am
hey, can you tell me how did u fix the problem?? cuz i having the same, i have 2 virtual servers and both addresses go to the first virtual directory.
thanks.
March 30th, 2007 at 12:46 pm
mine working… yahooooo. Azmeen is the man. Salute forever!
April 12th, 2007 at 4:47 pm
HI. I am having probs with Vhosts. I have it working at the moment on different ports. Ports redirected from 80 to 8000 and 9000. I added Listen 8000 and 9000 to main config. Both sites work fine. Though if i try to get them to respond when they are both on port 8000 it all ways opens the main site. I can ping the servers as it states in the hosts file no problem (e.g server1 server2 etc). httpd-vhosts and hosts file is below. Anyhelp would be good as my head is starting to hurt..lol
NameVirtualHost *:8000
DocumentRoot D:/server/xampp/htdocs
ServerName server1:8000
ServerAdmin admin@localhost
ServerName server2:9000
ServerAlias *.http://example.no-ip.info
ServerAdmin admin@localhost
DocumentRoot D:/server/xampp/htdocs/dt
Options ExecCGI Includes FollowSymLinks
Options +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
———————–
127.0.0.1 localhost:80
127.0.0.1 server1:8000
127.0.0.1 server2:9000
April 12th, 2007 at 5:27 pm
Hi Robg,
First of all, you should change your hosts file to the following:
127.0.0.1 localhost
127.0.0.1 server1
127.0.0.1 server2
Reason being, the ports should not be part of the definition.
As for the virtual hosts settings, I’d use the following
# Default Webby - Any requests not catched by a vhost goes here
# Should be the FIRST vhost you define
<VirtualHost *:80>
DocumentRoot C:/path/to/default/site/files
ServerName localhost
ErrorLog logs/defaultsite-error_log
CustomLog logs/defaultsite-access_log common
</VirtualHost>
# Server1 Settings
<VirtualHost *:8000>
DocumentRoot C:/path/to/server1/site/files
ServerName server1
ErrorLog logs/server1-error_log
CustomLog logs/server1-access_log common
</VirtualHost>
# Server2 Settings
<VirtualHost *:9000>
DocumentRoot C:/path/to/server2/site/files
ServerName server2
ServerAlias example.no-ip.info # NOTE the format
ErrorLog logs/server2-error_log
CustomLog logs/server2-access_log common
</VirtualHost>
That should work for what you’re trying to achieve.
April 12th, 2007 at 6:01 pm
Hi again. Used the coding. And yes you are a legend dude thankyou. I still cannot work out why i cannot use the same ports(for in future if i make more sites. I will have so many different ports for each site)
thanx Rob
April 12th, 2007 at 8:23 pm
Robg,
Actually, you can. How do you think a web hosting company hosts so many domains on one server? Virtual hosts of course!
Let’s say you want server2 to also listen on port 8000, you just need to change it to:
# Server2 Settings
<VirtualHost *:8000>
DocumentRoot C:/path/to/server2/site/files
ServerName server2
ServerAlias example.no-ip.info # NOTE the format
ErrorLog logs/server2-error_log
CustomLog logs/server2-access_log common
</VirtualHost>
All done and ready to run
Glad to be of assistance.
April 12th, 2007 at 9:11 pm
Hi,
Hi, i know that. But if i change the port number to be the same as the main server, the main server responds. There is something wrong with the server responding by name.Thanx i anyhow i will just call by ports.
Rob
April 12th, 2007 at 10:30 pm
Hi
Problem solved. It was the redirect. When accessing each site in the address bar all there was, was http://12345677:8000 instead of and actual site name like http://example.com
Thankyou for your help
Rob
April 13th, 2007 at 6:30 am
Glad your resolved it.
April 25th, 2007 at 11:47 am
[...] Setting Up Multiple Apache Local Web Sites On Your Computer [...]
April 30th, 2007 at 11:17 am
Thanks for the article/comments. Very useful info.
May 2nd, 2007 at 2:22 am
Thanks so much for this. I’ve tried setting this up several times before, but was never able to get it working until now.
Thanks again!
May 2nd, 2007 at 9:23 pm
Mau and Matt,
Glad it’s helpful in helping you
May 5th, 2007 at 9:24 pm
Hi guys,
I did what was written above but I get only my first subhost work and it does not seem to open ASP, PHP, etc. files(even HTML).
It opens only images.
I am running Apache 2 and my system is XP(Service Pack 2).
BTW:When I ping the newly created hosts I get replies(When I shut down my firewall).
June 1st, 2007 at 4:11 am
Thanks very much – I wanted to run my PHP scripts on my local machine.
July 10th, 2007 at 3:14 am
To everyone who is having the problem where your first VirtualHost keeps coming up if you have multiple projects set up. You need to make sure that you have included the NameVirtualHost directive. Here’s an example of my files so that I got it to work:
HOSTS File
——————————–
127.0.0.1 localhost
127.0.0.1 servera
127.0.0.1 serverb
127.0.0.1 serverc
127.0.0.1 serverd
HTTPD.CONF File
——————————–
NameVirtualHost 127.0.0.1
ServerName servera
DocumentRoot “C:\WWW\servera”
ServerName serverb
DocumentRoot “C:\WWW\serverb”
ServerName serverc
DocumentRoot “C:\WWW\serverc”
ServerName serverd
DocumentRoot “C:\WWW\serverd”
NOTE the NameVirtualHost 127.0.0.1 line before the VirtualHosts are declared. You need to have this line for everything to work. I hope this helps…
July 29th, 2007 at 1:11 am
[...] Setting Up Multiple Apache Local Web Sites On Your Computer [...]
September 19th, 2007 at 2:42 pm
[...] decided to revisit one of my older posts; Setting Up Multiple Apache Local Web Sites On Your Computer; which was written as a guideline for Apache 1.3.x for both Linux and Windows [...]
October 3rd, 2007 at 12:15 pm
Worked perfectly…really appreciate the help.
October 5th, 2007 at 4:51 pm
Does not seem to work for me (I do use apache 2.2). If I type bugzilla:8001 I get the apache “it works” page. I should get the login page
Here is my configuration
Hosts file
127.0.0.1 localhost
127.0.0.1 bugzilla
httpd.conf
#Listen 0.0.0.0:0
Listen 8089
Listen 8001
# symbolic links and aliases may be used to point to other locations.
DocumentRoot “D:\Apache2.2\htdocs”
# This should be changed to whatever you set DocumentRoot to.
httpd-vhost.conf
NameVirtualHost *:8089
# VirtualHost example:
DocumentRoot “D:\Apache2.2\htdocs”
ServerName localhost
ErrorLog logs/defaultsite-error_log
CustomLog logs/defaultsite-access_log common
DocumentRoot “D:\BUGZILLA”
ServerName bugzilla
ErrorLog logs/bugzilla-error_log
CustomLog logs/bugzilla-access_log common
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
October 5th, 2007 at 4:57 pm
I had to remove the symbols from the lines that dit not show up on this website. Here is my virtual configuration again
VirtualHost *
DocumentRoot “D:\Apache2.2\htdocs”
ServerName localhost
ErrorLog logs/defaultsite-error_log
CustomLog logs/defaultsite-access_log common
VirtualHost *:8001
DocumentRoot “D:\BUGZILLA”
ServerName bugzilla
ErrorLog logs/bugzilla-error_log
CustomLog logs/bugzilla-access_log common
VirtualHost
Directory “D:\BUGZILLA”
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Directory
October 5th, 2007 at 5:03 pm
J Berg,
I think the instructions in Using Local VirtualHosts On Apache 2.0.x would be more suitable for you.
I use Apache 2.2.x myself and they work like a charm
January 20th, 2008 at 1:44 am
This is a great article.
does anyone know how i can configure these local host names to be viewable by other machines within the same network. since these are not domains, and there is no internal DNS services, im not sure how this would work.
January 20th, 2008 at 2:01 am
Hi Joe Schwab,
It depends on what your web server runs. If it’s on a Linux box, you could run the Samba service and set aliases for the hostnames you want to server pages on.
On a Windows network, I don’t think there’s a similar function unfortunately. The tedious alternative would be to edit each client’s hosts file.
January 20th, 2008 at 8:02 am
Hey Azmeen,
thanks for your response, Im running MAMP on mac OS X (which is essentially unix).
if i were to edit each clients host file, would i point them to the internal IP address(192.168.1.x)? and not the localhost 127.0.0.1. does that sound about right?
Joe
March 2nd, 2008 at 1:55 am
I am having the same problem as many others – the first virtualhost becomes the default. I followed all the suggestions in some of the responses (include the NameVirtualHost 127.0.0.1). No luck. What else could this be?
March 2nd, 2008 at 2:08 am
I figured it out. Thank you, bye!
(Just Kidding)
The problem I think that many people are having is the syntax. Jeff, you are right. It has to do with the NameVirtualHost directive. I was having trouble because most of the examples are not showing the exact syntax with respects to open and close tags (ie: . So, here is the syntax:
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
NameVirtualHost 127.0.0.1
ServerName domain1
DocumentRoot “C:\path_to_domains\domain1\httpdocs”
ServerName domain2
DocumentRoot “C:\path_to_domains\domain2\httpdocs”
Andrew.
March 2nd, 2008 at 2:09 am
…and I see why now! The submission form on this page removes the tags – lol.
March 2nd, 2008 at 2:10 am
so, NameVirtualHost 127.0.0.1 is outside of all tags. By itself.
August 6th, 2008 at 6:36 pm
Hi all
i wrote this in my httpd.conf
Listen 80
Listen 8000
Listen 9000
ServerName example1.domain.com
Redirect / http://example1.domain.com/page1
ServerName example2.domain.com
Redirect / https://example1.domain.com/page2
but virtual hosts r not working
when i use port 80 in virtual hosts instead of 8000 and 9000 it works fine
so what is my problem
can anyone help ???
thank you
August 6th, 2008 at 6:39 pm
LOL tags removed
VirtualHost *:8000
ServerName asec2.tarakeeb.com
Redirect / http://asec2.tarakeeb.com:10038/wps/portal
/VirtualHost
VirtualHost *:9000
ServerName asec3.tarakeeb.com
Redirect / https://localhost:10003/ibm/console/logon.jsp
/VirtualHost
August 7th, 2008 at 3:17 pm
Taha Yusuf,
The example I’m giving below is for Apache 2.2.x (may work for Apache 2.0.x too):
<VirtualHost asec2.tarakeeb.com:8000>ServerName asec2.tarakeeb.comRedirect / http://asec2.tarakeeb.com:10038/wps/portal</VirtualHost>What it does is forward any requests to
http://asec2.tarakeeb.com:8000/tohttp://asec2.tarakeeb.com:10038/wps/portal.Therefore, a request for:
http://asec2.tarakeeb.com:8000/somedir/somefile.phpWill be forwarded to:
http://asec2.tarakeeb.com:10038/wps/portal/somedir/somefile.phpPlease get back to me if you have any further questions.
Cheers!
September 11th, 2008 at 5:04 pm
Hi Danny,
Thanks man for the very helpful article.
Please remember to add the following lines to the httpd.conf file for the virtualhost to work.
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
Plus when adding the virtual hosts to the conf file indicate the port to listen to as shown below
Cheers!!!!!!!!
January 14th, 2009 at 10:41 pm
Options Indexes FollowSymLinks MultiViews
Why?
January 30th, 2009 at 12:34 am
Used the coding. And yes you are a legend dude thankyou. I still cannot work out why i cannot use the same ports(for in future if i make more sites. I will have so many different ports for each site)
thanx Rob
January 30th, 2009 at 12:35 am
The submission form on this page removes the tags
January 30th, 2009 at 12:36 am
Please remember to add the following lines to the httpd.conf file for the virtualhost to work.
February 9th, 2009 at 4:49 am
I have tried EVERYTHING written in all the above posts and read the guide about three times and still I am getting the same problem as everyone else i.e. only the first web site defined in httpd.conf loads if you are running multiple sites. I’ve addded NameVirtualHost 127.0.0.1 as suggested and set up everything else as suggested and it still refuses to work and always serves up the first web site. Help!
HOSTS FILE
127.0.0.1 localhost
127.0.0.1 local.example1.co.uk
127.0.0.1 local.example2.co.uk
127.0.0.1 local.example3.co.uk
127.0.0.1 local.example4.co.uk
HTTPD CONF
Include “c:/wamp/alias/*”
NameVirtualHost 127.0.0.1
# ViewTraffic dev site
DocumentRoot “C:/wamp/www/example2.co.uk/”
ServerName local.example2.co.uk
ErrorLog logs/viewtraffic-error_log
CustomLog logs/viewtraffic-access_log common
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
# My portfolio/blog dev site
DocumentRoot “C:/wamp/www/example1.co.uk/”
ServerName local.example1.co.uk
ErrorLog logs/example1-error_log
CustomLog logs/example1-access_log common
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
February 16th, 2009 at 8:32 am
Thanks a ton! this helped.
Dugg it.
February 16th, 2009 at 10:57 am
one side note to the other readers. For me, it was defaulting to the first virtual host defined, like the user from comment #48.
What I found that worked was changing it to port 80 instead of just “*”
So in the example above, where he does a directory declaration, instead of “VirtualHost *” put “VirtualHost *:80″
Also, above all of these, place “NameVirtualHost *:80″ in there.
this worked for me.
February 20th, 2009 at 6:03 pm
ou This is fine!!
I`m Russian
Sorrу за ошибки если что
ок?
May 7th, 2009 at 6:38 am
worked like a charm…thanks for clear, concise instructions.