Apache Guide: Setting Up Virtual Hosts

What's a Virtual Host?

As suggested in the paragraph above, virtual hosts are Web sites with different names that all run on the same server hardware. The idea is that Apache knows which site you are trying to get, even though they are all on the same server, and gives you content from the right one.

Need to make a single DSL connection look like a colocation facility? Then you'll want to look at setting up virtual hosts -- Web sites with different names that all run on the same server hardware. Rich Bowen explains three different methods of setting up virtual hosts on your Apache server.

It's this trick that lets me run several Web sites on my home machine, from a variety of different domain names, and several names within one domain, so that my one little machine behind my DSL link looks like a room full of servers. Neat trick.

Reasons for Using Virtual Hosts

Well, you probably already have some ideas in mind. But here's how I generally use virtual hosts. I work for a Web-design company, and when we're developing a customer site, we set up a virtual host specially for that company. These are usually name-based, since these are faster to set up (one step shorter), and are almost always internal only. That is, the names are put into our internal DNS, so that internal hosts can see the Web sites, but external browsers see only the boring, featureless external page on our development server.

The advantages to doing this are numerous. It lets us set the directory structure on the site the same way that it will be on the real server when we are done with development. It lets us keep fines completely separate from one "server" and another, so that there is no confusion as to what is on which site. And, from the developers' perspective, it lets them set up completely different sites in DreamWeaver, rather than just different subdirectories on the same site.

And, of course, you don't have to have a new server for each site, no matter how many customer projects you're working on at one time. Which is the biggest reason for using virtual hosts in any scenario.

Name-based vs IP-based Virtual Hosting

Virtual hosts can be specified one of two ways. These are configuration differences on the server, and are not visible to the client--that is, there is no way that the user can tell what sort of virtual host they are using. Or even that they are using a virtual host, for that matter.

The two types are IP-based virtual hosting and name-based virtual hosting. In a nutshell, the difference is that IP-based virtual hosts have a different IP address for each virtual host, while name-based virtual hosts have the same IP address, but use different names for each one. There are reasons for each. There's not much difference in implementation.

IP-based Virtual Hosting

In IP-based virtual hosting, you are running more than one web site on the same server machine, but each web site has its own IP address. In order to do this, you have to first tell your operating system about the multiple IP addresses. Most modern operating systems let you give your machine as many IP addresses as you want. The specific details of how you give your machine multiple IP addresses varies from OS to OS, and I'm not going to go into that right now. If you don't know how to accomplish this on your OS, you might want to consult your local guru. It's typically not very difficult. Or, you may just want to skip down to the section on name-based virtual hosts.

On older operating systems, it was sometimes even necessary to have one NIC (network interface card) per IP address, but that is almost never the case any more.

This article was originally published on Jul 17, 2000
Page 1 of 4

Thanks for your registration, follow us on our social networks to keep up-to-date