Fix for virtual hosts in OS X 10.7, Lion Apache Web Server

(*skip down to ‘Now the problem with Lion’ for the quick problem/solution*)

Yesterday I decided to leave Nginx server and go back to Apache for my primary webserver.  Having upgraded to Lion a few weeks ago, I began a fresh setup of apache.  When setting up other parts of Lion, I remember reading about some Apache server problem, but at the time I wasn’t interested in Apache so I didn’t pay them attention.  Well that certainly changed yesterday when I spent half the day reading numerous posts about Lion and its’ inherent problems with Apache.  Because it was such a common problem, I spent most of my time on complex solutions, however it turned out to be quite simple, albiet a little non-intuitive.

Ok, for a bit of background.  When in development, Apache can host websites you are working on and typically the URLs for those websites are tied to IP addresses.  The most basic is using an IP address which every computer with a network adapter comes with,, or the loopback IP.  For development of only a single website, you can to to http://localhost/ and that will map to the loopback IP and apache will serve your local website.

Since you are in development, you generally don’t have lots of external IP’s to work with, but still might want to use a name other then ‘localhost’, or you might also want multiple named websites (which could share the same IP address).  There’s were virtual hosts come in.

When you make a network request, your computer will first check it’s hosts file to map the URL with an IP address (before checking a master directory, or DNS).  If that URL maps to the current machine, Apache handles it and uses two ways to figure out which website it is serving, one is by looking at the name of the url the other is IP address based.  We are looking at the first here.

If you following along, add a line to your /etc/hosts file (in windows it’s something like windows/system32/hosts?  I don’t remember).

As you can see, they use the same IP address.  In Apache, you need to tell it that you are using virtual hosts, so in your httpd.conf file, reference a vhosts file which you will be updating.  It is likely commented out in your /etc/apache2/httpd.conf file

Include /private/etc/apache2/extra/httpd-vhosts.conf

Here is a sample vhosts file:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/Library/WebServer/Documents"

<VirtualHost *:80>
    DocumentRoot "/var/www/"
    <Directory /var/www/>
        Order allow,deny
        Allow from all

<VirtualHost *:80>
    DocumentRoot "/var/www/"
    <Directory /var/www/>
        Order allow,deny
        Allow from all

Of course you’ll want to change website1&2 to whatever the names of your websites are.

Ok, that’s all run-of-the-mill, typical Apache config stuff, nothing special there.

Now the problem with Lion, is the way that the configuration is handled, namely sections of the httpd.conf file.  The post which actually helped me the most when searching for solutions was this one.  The guy added his vhost config in a different file then the typical vhosts.conf file.  He wasn’t sure why it worked, but it did.  Ok, let me first start by saying that you don’t need any special file called, “00000_MoreTestingRequired.conf” :p

The issue was how Apache actually includes the vhost config.  Depending on how you start apache, very likely it isn’t actually included/read.  One thing that the Lion configs have are lots of conditional blocks like, IfDefine WEBSHARING_ON, IfDefine !MACOSXSERVER, etc.  Because the previous poster included his config file outside of a conditional block (namely WEBSHARING_ON), it was actually included/read.

Why was this a bit confusing?  Well first, if you use the GUI to start apache by enabling Web Sharing from the “sharing” section of System Preferences it will work.  But!  the type of user who would actually use that GUI isn’t the type of user would would actually use virtual hosts.  Sure, they might use apache, but likely in the more basic single site, ‘localhost’ or user sharing.

For developers, the more likely way to start apache is via either httpd -k start, or apachectl start from the command line.  If either of those are used, the block which includes the vhost config is never read.  Therefore the solution is to either move ‘Include /private/etc/apache2/extra/httpd-vhosts.conf ‘out of the ‘IfDefine WEBSHARING_ON’ block or simply remove those IfDefine and associated /IfDefine lines.  Or maybe just tighten up the block to only the “User home directories” section which makes more sense and probably should have been the configuration to start with.

Hope this helps and maybe saves someone else from a few hours of frustration :p


15 thoughts on “Fix for virtual hosts in OS X 10.7, Lion Apache Web Server”

  1. Thank you for this! I had a hell of a time getting it to work, even following your directions. In the end, I had to turn on Web Sharing in the preference panes and uncomment (but leave in place) the Include line for the vhosts.conf file. Your article really helped save me some time by pointing me in the right direction, however. I’m all for simplifying where simple == better, but Apple really simplified the simplicity out of the Server settings.

  2. actually I still had another problem,…
    Lion treats the tld .local different from others – don’t call your virtual hosts .local on Lion! 😉

  3. Hi Paul,

    I just wanted to say thanks for writing this post – it was really helpful! I’ve spent hours on this but after reading this I basically just commented out the “ifDefine WEBSHARING_ON” opening and closing tags and it seemed to do the trick.

    Browsing directly to localhost isn’t working, but I’m not too worried about that. I don’t suppose your run django? Been battling with my python/django/mysqldb setup like nothing else;)

    Thanks again!

  4. God bless you for your post !

    I spend nearly three days with the Lion vhost hell. I ran the httpd -S and kept telling me the vhosts config file is OK ! I somewhat knew the file was not picking up as I completely replaced its content with non-sense. However, without your post I would not be able to find core problem.


  5. Thanks so much for this post. I am very grateful for your help.
    Do you have any ideas or can you point me in the right direction to do the following:
    I want to have 2 x webservers in a LAN and access both of them using virtual domains on port 80. – server1 ( :80 – server2 ( :80
    At the moment I point port 80 on the WAN to port 80 on server1. Is it possible to redirect from server1 to server2?

  6. Have any of you had success getting SSL to work with named virtual hosts under Lion? I’ve been beating my head for a couple days. The server either refuses to start, or connections to port 443 are redirected to the default site instead of my named host. Ideas? Successes?

  7. FInally got my MacBook Air recognizing my virtual hosts. Now apachectl -S actually shows the virtual hosts. The odd thing is that navigating to one of my virtual hosts, as well as going to my home site at /Users/myname/Sites, shows me only the html markup, not the rendered html. Strangely, cfml and php are rendered but not the html.

    What am I doing wrong.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s