Check out the updated post on getting Localhost Wildcard Subdomains setup
[sociallocker] [/sociallocker]
This is almost a tutorial for myself, because on and off for the last few months I’ve been trying to get a Virtual Hosts set up on my local Windows 7 machine so I can run WordPress and WordPress Multisite.
Currently I am using XAMPP to run Apache and MySQL, but it requires all local hosted directories to run under the “localhost” site, for example: http://localhost/demo
or http://localhost/clientdomain
.
Virtual Hosts WordPress
Really there is nothing wrong with the above, but I’ve got a few WordPress multisite installs running that mimic live sites that use wildcard DNS with sub-domains. So dealing with a local sub-directory becomes hard to test and build when the live site is running sub-domains.
So, this post will cover how I got a sub-directory Multisite install working in Windows 7 with XAMPP. I’ll also list some great posts which helped me get to my final goal.
The Steps
- First I am going to assume you’re using a Windows machine and have XAMPP installed.
- Open the XAMPP control panel application and stop Apache. Be aware that late Windows machines might run it as a service, so check the box to the left of the Apache module.
- Navigate to
C:/xampp/apache/conf/extra
or wherever your XAMPP files are located. - Open the file named
httpd-vhosts.conf
with a text editor. - Around line 19 find
# NameVirtualHost *:80
and uncomment or remove the hash. - At the very bottom of the file paste the following code:
<VirtualHost *> ServerAdmin [email protected] DocumentRoot "C:/xampp/htdocs" # change this line with your htdocs folder ServerName localhost ServerAlias localhost <Directory "C:/xampp/htdocs"> Options Indexes FollowSymLinks Includes ExecCGI Order allow,deny Allow from all </Directory> </VirtualHost>
With out that line of code you will lose access to your default
htdocs
directory. IE.http://localhost/
will be inaccessible. - Now you can copy and paste the code above below to add your Virtual Host directories. For example I’m working on a site called Eatery Engine so the following snippet will allow me to work with sub-domains on my local install:
<VirtualHost eateryengine.dev> ServerAdmin [email protected] DocumentRoot "C:/xampp/htdocs/eateryengine" # change this line with your htdocs folder ServerName eateryengine.dev ServerAlias eateryengine.dev <Directory "C:/xampp/htdocs/eateryengine"> Order allow,deny Allow from all </Directory> </VirtualHost>
-
Notes:
- change
<VirtualHost eateryengine.dev>
to something like<VirtualHost wordpress.dev>
or<VirtualHost wordpress.loc>
. - Be sure to change your
ServerName
andServerAlias
with the same as above.
- change
- Next head over to your Windows host file to edit your HOSTS. the file will be located at
C:/Windows/System32/drivers/etc/hosts
, where hosts is the file. Open it with notepad. - Look for
# localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost
and add the following just after that line:
# localhost name resolution is handled within DNS itself. 127.0.0.1 localhost 127.0.0.1 eateryengine.dev #change to match your Virtual Host. 127.0.0.1 demo.eateryengine.dev #manually add new sub-domains.
- Restart Apache and test everything.
Final notes
Be sure to do a fresh install for any WordPress Multisite installs that may require sub-domains. Thanks to Tildmark and some of the comments for finally pointing me in the correct direction. This old MU forum post and this stackoverflow post also helped.
And please if you’ve got questions or comments, leave them below.
Update June 2012
I started to have issues with multiple custom domains, and had to change a few lines in the httpd-vhosts.conf
file. See new code below:
Note: WordPress strips backslashes, so below I’ve replaced them with forward slashes. I believe it with work regardless either way.
<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs" ServerName localhost </VirtualHost> <VirtualHost *:80> DocumentRoot "C:/Users/Austin Passy/Documents/InMotion Hosting/frostywebdev.com/html" ServerName frostyweb.dev <Directory "C:/Users/Austin Passy/Documents/InMotion Hosting/frostywebdev.com/html"> Options Indexes FollowSymLinks ExecCGI Includes Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/eateryengine" ServerName eateryengine.dev <Directory "C:/xampp/htdocs/eateryengine"> Options Indexes FollowSymLinks ExecCGI Includes Order allow,deny Allow from all </Directory> </VirtualHost>
Sidebar
Alternatively you can use a product like ServerPress, which handles the creation process for you! I was lucky enough to meet Stephen at a recent OC WordPress meetup.
Nice one. This would have come in handy a couple months ago. My biggest problem with running WP multi-site on my local dev box and using subdirectories has always been with creating links like /contact-us/ that send you back to the main site. This looks like it would have been a perfect solution had I not just left my windows machine.
I’ll be passing this on the the WordPress Vegas meetup group. I’m sure they’ll dig it!
For sure! What are you using for your Apple now? Built in Apache or MAMMP, because I think MAMP Pro allows domain mapping through there UI.
Hands Down, I just did this. Although I am using MAMP PRO!
But I learn a lot from you austin
Thank you Austin, just what was I looking for. After testing all different config, finally found the solution for Win7. Thanks π
Can we make it work with Domain mapping as well? I understand this is a further step than subdomains, but would be really cool if I could set up domain mapping on XAMPP.
Cool tutorial. I have tried your procedure above but was getting error when was trying to access the sub-domains: when I tried to login to the first site created the page was redirected to the login page again and I can’t access the dashboard…
After making some tests, reading more information in some forums finally I got it working fine in this way, step-by-step:
1) After install Xampp I created the directory for the multisite, called mult.dev
2) Then I have installed the WordPress with the Network in that directory
3) In the httpd-vhosts.conf file I placed the following code:
NameVirtualHost *:80
DocumentRoot “J:/xampp/htdocs”
ServerName localhost
DocumentRoot “J:/xampp/htdocs/mult.dev”
ServerName mult.dev
ServerAlias *.mult.dev
Options Indexes FollowSymLinks ExecCGI Includes
Order allow,deny
Allow from all
4) For the “hosts” file I added the following code:
127.0.0.1 mult.dev
127.0.0.1 siteone.mult.dev #that would be the first site to be created
5) Then I came back to the Network admin area and created the “siteone” there. After, everything goes perfect.
Thanks too much for your help! π
Excellent! Thanks… π
i have this
DocumentRoot “C:/xampp/htdocs”
ServerName localhost
DocumentRoot “C:/xampp/htdocs/wordpress”
ServerName wordpress.dev
Options Indexes FollowSymLinks ExecCGI Includes
Order allow,deny
Allow from all
but my adress still like this -> http://wordpress.dev/wordpress/
Same here.
ServerAdmin [email protected]
DocumentRoot “C:/xampp-portable/htdocs”
ServerName mgd.local
ServerAlias mgd.local
Options Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
and my domain is still: http://mgd.local/mgd/
How did you end up solving it?
See Ben’s reply dated NOVEMBER 28, 2012 AT 3:20 AM
I believe that this is just the ticket! Thanks for posting and with great precision.
I am looking to add a multi-site WordPress network to my development environment and researching how to do the setup. This post is a good gateway.
Silly question perhaps, but can this work on earlier versions of Windows (e.g., Vista) or is something peculiar to Windows 7? Hope to bypass Windows 7 with my next upgrade. π
This should work with any version of Windows.
Thanks, very helpful!
Its also worth mentioning that you might need to modify the httpd.conf manually so that the below in enabled:
Include conf/extra/httpd-vhosts.conf
I tried this and Apache just hangs when I try to start it. I am new to Apache, and all I need to do is change my DocumentRoot to “C:/xampp/htdocs/wordpress/wp-content” instead of “C:/xampp/htdocs” that Apache uses. I can access “C:/xampp/htdocs” locally if I need to.
How can I do this with out all the virtual crap?
thanks for the help really helped me to, this comment I’m taduciendo with a translator because I do not understand English, so it was really great but very helpful.
unde_peru_2013
With windows vista 64 – I could not find a directory called
C:/Windows/System32/drivers/etc/hosts
even searching there was nothing called hosts
should I just create the new sub dirs and the hosts file?
Great article Austin – looks like I just need to change my URLs in the database and it’ll work perfectly for me.
I do a lot of testing on other computers (Macs, other Windows machines with older browsers, etc.) so I have set myself up with a static internal IP so I can look at the site anywhere within our network.
Rather than localhost/project it’s 192.168.0.206/project.
Any idea how I’d accomplish this for my IP address, rather than just localhost/… ?
If I find a solution for this, I’ll let you know.
I found the solution to this. in the same hosts file (C:/Windows/System32/drivers/etc/hosts) you can put in your direct IP address on another computer (my dev computer is 192.168.0.206) and point the the same virtualhost domain to it!
ie.
192.168.0.206 eateryengine.dev
Yup that will do the trick! I have done this for my Drobo before.
thanks for help, now i want to create this in linux fedora then how this possible. its same or difference.
WordPress site moving problem is about absolute url and absolute paths stored in database.
This method help about absolute urls…
Are there similar method about absolute paths?
Thank you
I have done everything just the way put above. But Apache could not start.
Any solution?
Thanks
The problem is solved, drupal was not properly installed!
I see. No worries.
I had an issue saving the hosts file, its asking me to save as. Anyone have any suggestions?
Aaaand, never mind. Changed the files permissions for my account and worked perfectly! Great article!
New issue, cant get apache to start back up. Attached vhost.config below.
Any help would be greatly appreciated.
also you must check your httpd.conf for
# Virtual hosts
#Include etc/extra/httpd-vhosts.conf
to uncomment this line
Include etc/extra/httpd-vhosts.conf
Thanks
Thanks! This bit of information was left out everywhere. Uncommenting “Include etc/extra/httpd-vhosts.conf” in the httpd.conf finally SOLVED my problem!
modified the http-vhosts file like so:
NameVirtualHost *:80
ServerAdmin [email protected]
DocumentRoot C:/xampp/htdocs/medPlatform_compressor_DB2″
ServerName sanome.dev
ServerAlias sanome.dev
Options Indexes FollowSymLinks ExecCGI Includes
Order allow,deny
Allow from all
ServerAdmin [email protected]
DocumentRoot C:/xampp/htdocs/medPlatform_compressor_DB2″
ServerName sanome.dev
ServerAlias sanome.dev
Options Indexes FollowSymLinks ExecCGI Includes
Order allow,deny
Allow from all
Verified that the http.conf file has the vhost file included.
Also the the host file has the following:
127.0.0.1 localhost
127.0.0.1 sanome.dev #change to match your Virtual Host.
127.0.0.1 demo.sanome.dev #manually add new sub-domains.
Apache won’t start up now. What did I do wrong or am I missing something?
Thanks
Thanks for the great tutorial. Here is what I’ve done.
in file hosts
——–
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
127.0.0.1 dev.mysite.com
——–
and in httpd-vhosts.conf
——–
NameVirtualHost *:80
DocumentRoot “D:\xampp\htdocs”
ServerName localhost
ServerAlias localhost
DocumentRoot “D:\xampp\htdocs\www\dev2”
ServerName dev.mysite.com
ServerAlias dev.mysite.com
Options Indexes FollowSymLinks ExecCGI Includes
Order allow,deny
Allow from all
——–
When I give dev.mysite.com in the browser url, it does not give any error and simply redirects me to “http://localhost/www/dev2” displaying my site properly, but displaying “http://localhost/www/dev2” in the URL, which defeats the whole purpose of virtual hosts. What am I doing wrong here? Anyone? Many thanks in advance.
nice article but i perform all of above steps but my vitual host still not functioning it is giving error server not found
can you help me?
Thanks for these. I’ve been looking for a way to set this up but none of the ones I’ve seen didn’t work. This finally did it. Thanks a million.
You’re welcome.
Dear Austin,
I found this wamp server easy to implement virtual hosts:
UWamp server, and this server you could develop using sqlite database successfuly, in others solutions the sqlite database creation demands extra configurations.
Try this and write your impressions about, I migrate all projects from Xampp to UWamp.
The only work tedious to do is update all hosts files from the local network statrions to use the virtual host name associated with the IP Address Fixed to access the local intranet. If the user don’t want use de IP Address to access the intranet site. It’s a hard work for great lans (above 20 stations).
http://www.uwamp.com try you will like.
This was really simple and nice.
Thanks.
Perfect, thanks!
Refer to the above description, I set up a virtual host in my windows 7 PC, and it is running 3 website there.
http://www.365d.tw
http://www.veronafood.com.tw
http://www.peterrooster.com.tw
Now, Iβve a problem, if key-in domain only (ex. veronafood.com.tw), always link to http://www.365d.tw, can you help me to solve please.
Sorry for my poor English, and I come from Taiwan.
This seems very usefull ,is it possible to implement this procedure with magento CE 1.9 ?
You can also add localhost as a virtual host so you can still access http://localhost/
Hi, I did all you said here, my vhosts works fine, except for 1 thing.
I have this:
myproject.com/index.php?signIn=1
I put my user and pass, and I login to my project, but appears in the url:
myproject.com\/index.php
puts the “\” automatically. How can I fix it?
You’ll have to create a rewrite rule in the root directory.
Just used this and some updates have occurred to xampp/apache so this is the important change
Thanks for the update Natan.
Thank you for the article , this is really help full
i just forgot to add this in my file
and my Appache was not starting then i Google it and got you.
π
thanks again and keep it up
Thank you for your article. This is so helpful π
Thanks for this explanation had some troubles with multiple domains and this fixed it!
Glad I could help out!
I am using window 8 and xampp server ,i have been having problems with my virtual host,i think is my httpd, how can i edit the httpd to work with it. Can anyone help me out,i will appreciate.
i’ve got error i got this from my xampp control-panel
11:59:55 AM [Apache] Error: Apache shutdown unexpectedly.
11:59:55 AM [Apache] This may be due to a blocked port, missing dependencies,
11:59:55 AM [Apache] improper privileges, a crash, or a shutdown by another method.
11:59:55 AM [Apache] Press the Logs button to view error logs and check
11:59:55 AM [Apache] the Windows Event Viewer for more clues
11:59:55 AM [Apache] If you need more help, copy and post this
11:59:55 AM [Apache] entire log window on the forums
Great Job , Nice Work Thankx man.
On some versions of Windows, you’ll need to run Notepad as administrator to edit C:/Windows/System32/drivers/etc/hosts. Just right-click and click “Run as administrator”.
i have one problem to setup virtual host..
i set virtual host but it is directly redirected to dashboard now whats the solution for it…?
thanks. it really work for me.
how to setup xampp to accesss from ip public
Thank you bro very much