Apache 2 Only Serves First Virtual Host

Posted in: Development, Technology

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: 11% [?]

Posted November 1st, 2007 @ 10:17 PM

21 Replies

  1. 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

  2. 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

  3. Alex adds this Comment:

    Richard – thanks for the tip!

    November 2nd, 2007 at 11:04 am

  4. 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

  5. Darryl adds this Comment:

    Thanks Alot, this tip saved the day for me.

    November 10th, 2007 at 8:42 pm

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. Michael Ludgate adds this Comment:

    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…)

    July 14th, 2008 at 2:04 pm

  16. Doug adds this Comment:

    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!

    September 27th, 2008 at 5:19 pm

  17. sky adds this Comment:

    great. works. thx.!

    January 6th, 2009 at 9:17 am

  18. gc adds this Comment:

    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.

    January 25th, 2009 at 8:04 pm

  19. Mike adds this Comment:

    Thanx a lot! Saved a lot of time for me!

    February 3rd, 2009 at 2:16 am

  20. Jeff adds this Comment:

    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/

    February 13th, 2009 at 2:57 pm

  21. Brad adds this Comment:

    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

    May 10th, 2009 at 10:22 am

Add a Comment

Please note: Use of a non-personal web site or blog in the field below and/or comments that are off-topic, personal attacks, or support requests will likely be removed at my discretion.

Note: This post is over a year and a half old. You may want to check later in this blog to see if there is new information relevant to your comment.

Fatblogging » « Spanning Sync Impressions

About This Site

This is the personal web site of Alex King, a web developer in Denver, Colorado USA. More...


Crowd Favorite

Crowd Favorite is my software and web development business.

We build web applications, design and develop custom WordPress themes and plugins, and build custom sites using WordPress as a CMS.


I also have a tumblog that aggregates my online content from other services (Twitter, Flickr, del.icio.us. etc.).

America - america09.com

Ads

web design services