How to solve problems with Apache 2 vhosts?
This is worth a blog post, as it took us a while to get to the root of the problem. We had a fresh installation of RedHat (RHEL 5, to be precise) which comes with Apache 2. I'm used to having a
vhosts include for httpd.conf that looks something like this:
And in httpd.conf you'd typically find something like this:
# Virtual hosts Include conf/extra/httpd-vhosts.conf
Well on RHEL 5 with Apache 2 things are slightly different. For a start, every *.conf file in the
/etc/httpd/conf.d/ directory is included automatically, so you won't find an Include line in your httpd.conf file.
Secondly, when you add your include for your virtual hosts, as in the first example, to the aforementioned directory you'll get these warnings from Apache on restart:
[Portal Oct 05 06:59:34 2008] [warn] _default_ VirtualHost overlap on port 80, the first has precedence [Portal Oct 05 06:59:34 2008] [warn] NameVirtualHost *:80 has no VirtualHosts
And your virtual hosts will not work - in fact, Apache will not get past the first entry in your include file and all traffic to any IP on port 80 will get directed there. =(
There is a fix here, but this skips around the problem rather than truly tackling it:
Sure, if I set my config file to use the IP address instead of * it will work, but that sucks! I have dozens of web servers to deploy this config to. I don't want to have to edit the file on each one and change the IP. I also don't want to have to *remember* to when I need to update either. That's how mistakes happen.
I was turned on to the real fix on a forum, where a guy called "Tim" says:
Sounds like the virtual hosts conflicting with the default set in the main configuration. I seem to recall putting
andaround the initial website configuration in the main configuration (the /var/www/html/ part) to fix that in one incarnation of Apache, a while ago.
Bang on the money! A little investigation revealed some default vhosts entries in http.conf. Comment them out, like so:
# Virtual hosts
# Virtual host Default Virtual Host
And you're good. Restart Apache, warnings gone and your original, wild-card-based virtual hosts settings work fine. *phew*