A common question that seems to come up often is; how do I setup virtual host on apache. I’m going to show you how to do this on apache running on windows. It really is very simple.
I’m going to show you how you can do this in 5 minutes, and just 4 steps. Before we start you will need to download a fresh copy of XAMPP Lite. Once downloaded, unzip the file. You do not need to install this it will run right out of the extracted directory. See if you can do this in 5 minutes. Post your results in the comment section below. Let’s get started:
Step 1 adding host name to windows
You will need to open your windows host file. Depending on your version of windows, this file will be located in different locations. Here is the list of where your host file will be:
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
Windows Vista:
C:\Windows\System32\Drivers\etc\hosts
Open the file with notepad. If your using Vista you’ll need to run notepad as administrator. Just left click on the notepad icon and click run as administrator in the drop down menu.
Now add this to the bottom of the file
127.0.0.1 wordpress
127.0.0.1 websitebaker
The number to the right is your computers default ip address. In most cases the number should be the same as I have listed here. The words are the virtual host name. In this case I am adding 2 new hostname, wordpress, for my WordPress directory, and websitebaker for my Website Baker directory.
Save the file. Make sure notepad doesn’t append .txt to the file. That’s it for step 1, see easy so far.
Step 2 Edit the httpd-vhosts.conf
Open the httpd-vhosts.conf file with notepad. You will find it in xampplite\apache\conf\extra. Copy and paste the following code at the end of the file.
<VirtualHost *:80>
DocumentRoot /xampplite/htdocs
ServerName localhost
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName websitebaker
DocumentRoot /xampplite/htdocs/websitebaker
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /xampplite/htdocs/websitebaker>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName wordpress
DocumentRoot /xampplite/htdocs/wordpress
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /xampplite/htdocs/wordpress>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Now scroll back up and un-comment this line:
#NameVirtualHost *:80 (toward the top)
To look like this:
NameVirtualHost *:80
Save the file and create the 2 directories (wordpress & websitebaker) in your htdocs directory.
Step 3 Start Apache and test it.
go to \xampplite directory and click on xampp-control.exe and click the start button next to apache,
Watch for errors. If apache reports no errors then give your virtual host a try http://wordpress/
I hope you had fun, please comment your experience.
Step 4 (Okay, now I can count, with a little help from Eric)
Tell us about your experience by commenting bellow, and enjoy your XAMPP server. Or you could always try a web host.
Related posts:
On my copy of Apache, the config file is called “httpd-vhosts.conf” (note the ’s’).
Since this is a configuration for Windows, the reader should note that paths with embedded spaces will prevent Apache from loading at all. For instance, “c:/Program Files/xampp/htdocs/wordpress” will produce an error since it has an embedded space. The solution is to represent pathnames with embedded spaces by the old DOS 8-char short filenames, and keep the forward slashes (not backslash). Thus,
DocumentRoot c:/PROGRA~1/xampp/htdocs/wordpress
I also notice that there is no “Step 3″. Maybe it’s not needed? (smile)
I really do need to turn on my inner editor. Thank your for noteing the missing “s” will fix that.
Step 3 yea had to rewrite a few times, so my bad. LOL
The back slashes can be used in apache 2 on windows. I’ve been doing it that way for a while now.
The reason I didn’t give the full path is because with this setup you can move the package around, or use it on a thumb drive. but yes it’s a good idea to enclose the path in parentheses.
Thanks for your comments, They are always welcome.
Hi David,
thanks for this wonderful piece of work
There’s one part missing, I think: In the httpd.conf (in /xampp/apache/conf) you should also uncomment the line
#Include conf/extra/httpd-vhosts.conf
to
Include conf/extra/httpd-vhosts.conf
other wise apache won’t load the httpd-vhosts.conf
Also, strange enough, I have noticed that on my system I have to write
NameVirtualHost mydomain1.td:80
…
for *each* virtual host, e.g.
NameVirtualHost mydomain1.td:80
…
NameVirtualHost mydomain2.td:80
…
NameVirtualHost mydomain3.td:80
…
don’t know why, but it only works this way
A better way to test the new configurations is starting the apache_start.bat and watching the screen (the DOS box) and/or having a look at the error logs. xampp-control.exe only starts the server or not. I had a lot of trouble because there was sth wrong with the uncommented and adjustened
## ErrorLog @rel_logfiledir@/dummy-host.example.com-error_log
## CustomLog @rel_logfiledir@/dummy-host.example.com-access_log common
which I finally dropped. Now everything works fine
After some fiddling, I just found a solution to a CGI problem I have had. In my html forms, I like to have forms submitted to “/cgi-bin/foo.cgi” or “/cgi/bar.cgi”, and see the form processed from the /cgi folder for that web site. That way, the code that works on my development computer can be copied to the live server without any modification.
The fix was to copy over these 2 blocks of code, and insert them just before the closing “” tag:
ScriptAlias /cgi/ c:/xampplite/htdocs/wordpress/cgi/
AllowOverride None
Options None
Order allow,deny
Allow from all
Now I can run my cgi and php files from the /cgi folder for that website, instead of awkward workarounds!
It looks good. I tried Step 1. I saw:
“You do not have permission to save in this
directory. See the administrator to obtain
permission. Would you like to save in the My
Documents folder instead? Yes No”
I am using a public computer. What is the 1st
thing that I need to do, to go past this obstacle?
Thank you in advance. Con
Con, If your using a public computer, I would suggest not using virtual host. without the ability to add host names to the system I think you may be out of luck.
I have been struggling to get this right for a while now.. I’m running windows vista, and each time I try setup a v-host, I get an error when restarting the apache server, which leads to windows shutting the server.. Anyone else having this issue?
Thanks
Your example is fine if the two directories are on the same harddrive. My two directories are on two separate drives. what do I do in this case?
Hi, I have a similar problem to Doc (above)
I am trying to share a folder on my “D” drive, and the htdocs folder is on the “C” drive.
How would I go about adding this in as a virtual directory
f.e. main site C = http://www.domain.com and D = http://www.domain.com/files
Thanks.
I am encountering a rather strange problem with this. It works, but all the virtual hosts I set up end up going to the DocumentRoot of the localhost virtual host.
Hi.
Same as Anthony (above). I have set it all up as documented, but for some reason it jumps to the root – which is in my situation the xampp (full version) welcome screen. I am sure there is something simple to watch out for, but I can’t see it. Any help would be appreciated.
Thanks.
Hi again.
Solved it: in the httpd-conf, the “root” needs to be determined. Example:
Thanks.
Oops!
Example:
–> Directory >Driveletter:/xampp/htdocs/mywebsitefolder”<
I have edited the httpd-vhosts.conf file. I go to test the first site…works fine but when i test the second site it brins up the first sites home page…. any help. I have 2 domain names pointing to one ip setup in my dns admin page. Do i need to specify another port # :80 then :8080 for the other site(site 2)in my con fig file? Thanks for the help. ghost
Oh when i test from outside lan the 2nd domain name i get page cant be displayed….could be the domain registar has not finish stting up the dns change from their parked dns name..i checked the status and it didnt say that it was still “processing” my change…lets see what happens in a few days , thanks ghost
hi thanks, finally got somthing work.. excellent notes…just wonder how do u get to solve it…
Thanks David. It worked like a charm to me. I am using it on WindowsXP server for internal blog.
I think now I have to copy the files from the existing blog and paste it in this folder and then edit it. Hopefully I can edit the theme also.
Thanks once again for the clear instructions
ghostdogg,
I’m having the exact same issue. The 2nd virtual host listed in httpd-vhost.conf will always return the 1st host when you hit it with a browser. Did you ever solve your issue?
[...] 4 Steps to Apache with virtual host on Windows [...]
i have 2 host names that ties to my ip address ,but how do i set each to a different virtual site on my server
I’m trying to set it up this morning and … like many people … have some issues. I’m running XP Pro and the Full version of xampp. After following all the directions I go to http://wordpress/ and this is the error I get
Warning: file_get_contents(lang.tmp) [function.file-get-contents]: failed to open stream: No such file or directory in C:\xampp\htdocs\xampp\index.php on line 2
Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\xampp\index.php:2) in C:\xampp\htdocs\xampp\index.php on line 4
Any thoughts?
Kevin,
Change your slashes to forward slashes
Hello Kevin,
create a file with “lang.tmp” as name in your htdocs/xampp directory. That will resolve your problem.
Hi, thank u for replying my entrecard drop. i have blogroll your site in my serious droppers list. I visit all the sites in this list almost everyday plus most of my visitors too. We all have the same idea of getting return drop cards. Hope that you can blogroll my site back in your web too.
Thank you,
Reena http://reenashwina.blogspot.com
Great Help! The only flaw are the path examples, but I see that’s been mentioned above already. Keep it up (and running).
I can see that you know how to install this really well, but my question is…how the heck do you uninstall it?
I’ve been half tempted to just delete the file, but I remember going through the installation and all the stuff I had to add to the registry, etc. I just want it off my system completely and as cleanly as possible (I don’t know why it doesn’t just come with an uninstaller) because I shut down my design business to take time for other things…so I don’t need it any more.
Any help would be much appreciated, I have been frustrated with trying to figure out how to get rid of this for weeks now…
Hi David, long time since I wrote, how are thing?
I use a WAMP server on my local, would that not be a simpler alternative to XAMPP?
Hey Sunny,
Doing very well thanks.
WAMP may work for some but I really like XAMPP There is no need to install with the lite edition, so I can move the server via a thumb drive to other systems.
Plus I’ve been using this package for a long time… I know it pretty well.
[...] It would also be a good idea to install a server on your home system. I use XAMPP Lite and have written a tutorial on running it with virtual host. [...]
@ David
Yeah, the thumb drive accessibility can be very useful.
Hi
I am trying to configure virtualhost in apache
below is my sample file
NameVirtualHost *:80
DocumentRoot “D:\bea\user_projects\BASDomain\applications\smbasapp”
ServerName 172.24.121.189
Options FollowSymLinks
AllowOverride All
ServerName smbasapp
DocumentRoot “D:\bea\user_projects\BASDomain\applications\smbasapp”
Options FollowSymLinks
AllowOverride All
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
One problem from my side In: I want place http://localhost:7001
it is possible , I am trying but I am fail..please help
lavanyakumar
change NameVirtualHost *:80 to lavanyakumar You will have to change the port Apache listens to in the main Apache config file.
Well, thank so much.
I Don’t know this script,
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Help, Me Please…
This worked for me with the addition of the following entry. Without it, I continued to get an permission denied error.
ServerName myserver DocumentRoot “D:/myserver”
Order allow,deny
Allow from all
[...] 4 Steps to Apache with virtual host on Windows. [...]
>If your using Vista you’ll need
>to run notepad as administrator.
>Just left click on the notepad
>icon and click run as
>administrator in the drop down
>menu.
Of course, you mean “right click”, not “left click”
The same issue. i’m only able to see the first vHost and not The 2nd vHost even with the configuration you have here.
the first and the second only show the first vHost.
have you ever seen this situation?? if any interest i can post my vHost configuration
thanks!!
[...] Así pues si ahora dejamos un archivo /data/dev1.mydomain/www/index.php con su pertinente echo “Hello World!”; podremos acceder directamente al mismo desde http://dev1.mydomain siempre que tengamos correctamente configurado correctamente el archivo archivo hosts. [...]
Hi everyone,
I’ve just installed WAMP Server 2, using the above instructions as a starting point, but I’ve done it slightly differently:
1. Edit hosts file (as suggested) and put 127.0.0.1 localhost.mysite
2. Edited httpd.conf file by UNcommenting the lines:
LoadModule userdir_module modules/mod_userdir.so AND
LoadModule vhost_alias_module modules/mod_vhost_alias.so
and further down:
Include conf/extra/httpd-userdir.conf AND
Include conf/extra/httpd-vhosts.conf
3. Edited httpd-userdir.conf by adding:
Options All
AllowOverride All
Order allow,deny
Allow from all
4. Edited httpd-vhosts.conf by adding:
DocumentRoot C:/path/to/files
ServerName localhost.mysite
ErrorLog “logs/localhost.mysite-error.log”
CustomLog “logs/localhost.mysite-access.log” common
I needed to put the ErrorLog and CustomLog lines in for mine to work.
5. Restarted all services, and voila!
Hope that helps someone too.
Cheers,
Jacob
Bah, try with code tags:
Options All
AllowOverride All
Order allow,deny
Allow from all
and
DocumentRoot C:/path/to/files
ServerName localhost.mysite
ErrorLog "logs/localhost.mysite-error.log"
CustomLog "logs/localhost.mysite-access.log" common
how can i configure to change document root in windows vita having apache2.2
nisha,
Edit this line in the Apache httpd.conf file:
DocumentRoot “c:/whatever/you/want”
Cheers,
Jacob
Hi,
I followed exactly what is instructed above but when I entered “wordpress” on the address bar it only displays the root of the xampp (http://wordpress/xampp/).
Can anyone help me please.
Thanx!
Hey guys, is there a way to create a virtual host on just a thumb drive? I’m running xampp lite on my thumb drive originally on Windows XP Pro, but I’m going to do some work using a Mac at school. (To be clear, I want to work on multiple sites [located outside the xampp folder, which I assume will use the DocumentRoot line] on both Windows and Mac). What should I do? I appreciate any help!
Hi David,
Can you please help. I am trying to set up virtual hosts but the index.html that is served is from the following folder when I try typing either
//localhost or
//aarmax.localhost
in the address bar.
My C:/xampp/apache/conf/extra/httpd-vhosts.conf file is set up as follows:
NameVirtualHost *:80
DocumentRoot C:/xampp/htdocs
ServerName localhost
DocumentRoot C:/xampp/htdocs/aarmax
ServerName aarmax.localhost
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
My hosts file has two entries
127.0.0.1 localhost
127.0.0.1 aarmax.localhost
I using Windows Vista and XAMPP 1.7
Please let me know what I am doing wrong.
@Philipp
create a file with “lang.tmp” as name in your htdocs/xampp directory. That will resolve your problem.
Thanks Philpp