Some basic things about Apache VirtualHosts

Apache

For people who thinks that setup an Apache is easy (and of course, with some experience behind me, I agree), I say, beware of some details.
For those who need real technical documentation on Apache, this is not for you, this article intends to help people who only tries to setup small development or personal web servers (this do not applies on complex environments or needs).

Yesterday, I had to setup a development environment for a colleague, and it took some hours just because of theses:

  • When the domain name on which you try to put your VirtualHost does not exists in the Internet jungle (for a development environment, no need for a real tld, we can use anything like vhost.mybox), just put 127.0.0.1 in your /etc/hosts for this host on the box hosting the web server, and no other IP, this is only necessary for Apache to know that the domain name points to itself.
  • First point was pretty obvious, don't you think ? But some people sometimes tries to put some weird or public IP when it does not work. Just remember that Apache does not have to care what the real world is like outisde, it just want to know if the domain name exists, end of discussion :)
  • The second problem was the NameVirtualHost handling. With Apache, you can do lots of complex things, but keeping a basic configuration is better in most case (we are not talking about HA servers, or wide hosting solutions here of course). Put a single NameVirtualHost *:80 in your apache2.conf or httpd.conf and do not override or repeat it in you VirtualHosts configurations files, even default one! If your server has many IP, and you wan't it to respond from all of them, just put <VirtualHost *:80> to all you VirtualHost definitions, avoid conflicts, and do a whole coherent set of VirtualHosts.
  • And to finish: Do not mix VirtualHost filesystem directories ! (e.g.: vhost default on /var/www and some other on /var/www/someother). This is pretty obvious, just for filesystem consistency. Remember that Apache wont yell at you if you mix VHosts rootdirs, because it does not care about it. In most case, it will just work fine; but in the case we had yesterday, and without no reasons at all, mixing VHosts root dirs worked when we requested the web server through the lynx www client directly from localhost, but lost its mind when trying to do some requests from lan or from the Internet.

Short post, but real quick solution to a many-hours-eater problem :)

About