(*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, 127.0.0.1, 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
Here is a sample vhosts file:
NameVirtualHost *:80 <VirtualHost *:80> ServerName localhost DocumentRoot "/Library/WebServer/Documents" </VirtualHost> <VirtualHost *:80> ServerName website1.com DocumentRoot "/var/www/website1.com" <Directory /var/www/website1.com> Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost *:80> ServerName website2.com DocumentRoot "/var/www/website2.com" <Directory /var/www/website2.com> Order allow,deny Allow from all </Directory> </VirtualHost>
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