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.
Popularity: 18% [?]
Richard adds this Comment:
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.
November 2nd, 2007 at 12:07 am
David adds this Comment:
The same happened to me.
NameVirtualHost *
was commented out, causing the trouble.
Thanks for this post!
November 2nd, 2007 at 6:01 am
Alex adds this Comment:
Richard - thanks for the tip!
November 2nd, 2007 at 11:04 am
Hans adds this Comment:
NameVirtualHost *:80
needs to be the same. i talk about “*”
NameVirtualHost *:80
would work and
NameVirtualHost *
would work.
November 3rd, 2007 at 8:15 pm
Darryl adds this Comment:
Thanks Alot, this tip saved the day for me.
November 10th, 2007 at 8:42 pm
Joey Marchy adds this Comment:
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.
November 15th, 2007 at 1:20 pm
Ian Jupp adds this Comment:
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?
November 29th, 2007 at 4:01 pm
Carl Tashian adds this Comment:
Sorry, bad copy-paste on the URL. Here a better one:
http://tinyurl.com/28oxkv
November 30th, 2007 at 1:31 pm
Leroy Campbell adds this Comment:
@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!
December 8th, 2007 at 12:57 pm
Pat adds this Comment:
Just found this post via google and it seemed to solve my problem perfectly. Thanks!
March 16th, 2008 at 7:45 am
Combo Pack: CakePHP and Leopard Virtual Hosts | GiveGoodWeb adds this Pingback:
[…] 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 […]
April 4th, 2008 at 2:52 pm
josh adds this Comment:
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
April 5th, 2008 at 12:18 am
mod_rails and apache on OS X Leopard | tekin.co.uk adds this Pingback:
[…] If you find that Apache will only serve up the first of your virtual hosts, check you’ve correctly specified the NameVirtualHost directive. […]
June 2nd, 2008 at 2:09 am
Jon adds this Comment:
Oi! Thanks! You just totally saved my afternoon. I was bugging out on this one. Thanks again.
June 23rd, 2008 at 11:43 am