Note: this post is mainly to help people searching for a solution to this issue.
Yesterday I upgraded to Mac OS X 10.5 Leopard which brought Apache 2 to me along with the many other changes. I develop sites locally using domains like alexking.dev, crowdfavorite.dev, etc. by setting these up HOSTS entries and virtual hosts in Apache.
In Apache 1.x, I had the following in my httpd.conf:
NameVirtualHost *:80
<VirtualHost *>
DocumentRoot /path/to/site
ServerName alexking.dev
</VirtualHost>
...
This worked fine, serving all of my virtual hosts.
After Leopard upgraded me to Apache 2, I added this to my /etc/apache2/extra/httpd-vhosts.conf file and enabled that file in the httpd.conf file. After doing so, no matter which site I tried to load (alexking.dev, crowdfavorite.dev, etc.), it would only load the first declared virtual host.
My Apache skills are feeble, but luckily, our resident Crowd Favorite Linux jockey came to my rescue. Mike knew that Apache 2 handled the *:80 differently than Apache 1.x. Sure enough, this simple change:
NameVirtualHost *
<VirtualHost *>
DocumentRoot /path/to/site
ServerName alexking.dev
</VirtualHost>
...
and everything worked like a champ.
Hopefully this will help someone else struggling with the same issue.
Oops, sorry, it looks like the > got mixed up. My comment should have said:
You could have changed the Virtualhost tag into <VirtualHost *:80> and that wold have worked as well.
The same happened to me.
NameVirtualHost *
was commented out, causing the trouble.
Thanks for this post!
Richard – thanks for the tip!
NameVirtualHost *:80
needs to be the same. i talk about “*”
NameVirtualHost *:80
would work and
NameVirtualHost *
would work.
Thanks Alot, this tip saved the day for me.
For those people struggling with getting the second virtual host site to work, like I have been, make sure both site declarations are inside the VirtualHost tags:
VirtualHost *:80
ServerName beta-site-1.com
DocumentRoot “/Library/WebServer/beta-site-1”
ServerName beta-site-2.com
DocumentRoot “/Library/WebServer/beta-site-2”
/VirtualHost
I ended up having issues with the second site because I had two VirtualHost tag blocks.
This issue has consumed 10 hours of my time looking at blogs and tried every script suggested to get around this.
Same issue: does not matter what u put, test, info, etc as root directories, they are either not found or serve up the default localhost apache program:
NameVirtualHost *
VirtualHost *:80
ServerName test
DocumentRoot “/Library/WebServer/Documents/test”
ServerName info
DocumentRoot “/Library/WebServer/Documents/info”
/VirtualHost
Can anyone finally put this one to bed this monster?
Thanks for the postings, useful but has something been missed out in another config file somewhere?
Sorry, bad copy-paste on the URL. Here a better one:
http://tinyurl.com/28oxkv
@Ian: I’ve had the same issue. I followed Alex’s instructions and still had trouble accessing my WordPress installations (even though my other CMS installations worked). I found out that WordPress stores your blog’s URL in the database. You’ll have to edit the “wp_options” table of your database (look for “siteurl” and “home”) to match your new virtual host. I hope this helps!
Just found this post via google and it seemed to solve my problem perfectly. Thanks!
[…] Apache 2 Only Servers First Virtual Host | alexking.org Installing CakePHP on OS X Leopard | KeithMedlin.com Installing MySQL on Mac OS X | HiveLogic.com CakePHP.org ← Using BBEdit’s Update Document Feature […]
THANK YOU!
You pointed me in the right direction. It seems only 1 virtual host needs “NameVirtualHost *” but you NEED 1. I had moved hosts and decided I didn’t need the default site. I deleted it’s entry in sites-enabled. Well that screwed me because thats the virtual host that had that entry. So after 3 hours of trying to figure out what the difference was between my old machine and new machine this post pointed me to the answer.
I’de buy you a beer if I could
[…] If you find that Apache will only serve up the first of your virtual hosts, check you’ve correctly specified the NameVirtualHost directive. […]
Oi! Thanks! You just totally saved my afternoon. I was bugging out on this one. Thanks again.
I spent far too much time getting this sorted, the OP sent me in the right direction, but some of the comments were pretty unhelpful.
Inside apache’s httpd.conf file uncomment the line found at: # Virtual hosts
Include conf/extra/httpd-vhosts.conf
Within httpd-vhosts.conf uncomment the line:
# NameVirtualHost *:80
Below this (as demonstrated in file) use separate code blocks for each VirtualHost.
ServerName MyWeb1.dev
DocumentRoot “c:/wamp/www/MyWeb1”
ServerName MyWeb2.dev
DocumentRoot “c:/wamp/www/MyWeb2”
I found no issue with using VirtualHost * or VirtualHost *:80 Only that the later allows for specific port watching, which is useful.
The bane for me was using include within httpd-vhosts to point to another conf file (local full path) I had created (don’t know why this didn’t work…)
Thanks…
I was using Webmin to set up my virtual hosts on an SUSE 10 machine with apache2.
The first virtual host worked fine but the second just wouldn’t work… Then I found this posting.
For some reason, it revised the httpd.conf file so that the NameVirtualHost * entry to the bottom of the file with the VirtualHost entries above it.
I moved it back to the top and that fixed the problem. My problem was a bit different but you gave me an idea of how to fix it.
Thanks!
great. works. thx.!
I had followed all of this advice, working on a Mac OS X 10.5 machine, and was completely flustered. Finally, one of the links above said that sometimes things get stale, and you just need a magical reboot to get things working.
Sure enough, despite hours of running apachectl, all I had to do was a reboot of my machine for it to see the new settings.
Well, at least Macs are easy the other 99% of the time.
Thanx a lot! Saved a lot of time for me!
I spent days on this after upgrading. Glad I found this site and advice it help. So this is what worked for me…
1. set hostname foo.dev in hosts file
127.0.0.1 foo.dev
2. set virtual host in httpd-vhosts.conf file
NameVirtualHost *:80
DocumentRoot “/www/foo”
ServerName foo.dev
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
3. uncomment the Virtual hosts Include in the httpd.conf
Also you may want to check out VirtualHostX. I have not tried it yet but it looks interesting.
http://clickontyler.[...]irtualhostx/
Holy crap. Like many others, I just spent HOURS scattered across a few days trying to figure this out. Thanks for the post! THIS is what user generated content is all about!
B
I have scoured hundreds of virtual host configuration tutorials today, and this is the first one to get it right. Your work is absolutely appreciated.
[…] Thanks to Alex King for the solution to fix Apache 2 only serving the first virtual host. […]
My server was ubuntu 9.10 x64. I installed the ubuntu desktop, and I installed webgui 1.500 and created virtual hosts under the server tab of webgui. Use the default settings except for the server name put in your .com and fill in the document root for where you html files, etc are. It creates the proper files when you click apply changes on the server tab, then stop apache2, then start apache2. The virtualhosts must be using the * apparently. It places the proper files for you in sites-available and sites-enabled.
Its not webgui its webmin.
Did the trick for me. thx for the writeup!
This post is STILL helping people! I’d been dealing with this for months by commenting out virtual hosts in my httpd-vhosts.conf file. It feels so good not to have to do that anymore! 😀
Yes, just posting a quick comment to tell you that this post fixed something that I thought was beyond me. This issue was preventing me from upgrading my little Mac mini to Snow Leopard – it’s been stuck on TIGER forever! I hit the problem when I upgraded my MBP, messed with it for a while, then gave up because it wasn’t as important as keeping my sites up on my Mac mini.
So, thank you very much. I don’t know how I missed this post in my hundreds of Google searches previously but I did not miss it tonight.
Thank you!
It work like a charm
Fixed my problem, thank you!
Thanks a lot for your post. I have spent 1.5days on this issue. And its working now. Thanks again!
thx was getting desperate
Fixed it for me. Last time I had this issue, I did several things and one of them fixed it and I never knew what it was. After setting up a new ubuntu server, I was stuck with this problem again. Wasted a good one hour when I took a break and found this page. Thanks a bunch. Now I now why we need a default or NameVirtualHost *.
Great work spent hours trying to figure this out, as I was wondering if it had to deal with the Zend Framework etc. Thank God for problem solving skills
I was using Webmin on Ubuntu and instead of using Server Name I used the Handle connections to address field when creating Virtual Hosts. This resulted in rather than . Changing this fixed the problem but I couldn’t find the solution anywhere on the web – not even in this thread – but it led me in the right direction.
Wow…just wow. Was about to commit hari kari before I found your solution. Thanks bunches. Was playing with a new SSL cert and seven of my sites went down for about 7 hours. Began entertaining a complete apache reinstall before I came across your answer.
Thanks again man.