Of course, sometimes, you don’t have the luxury of giving your machine
multiple IP addresses. IP addresses are in shorter and shorter supply, and
frequently, such as if you have an DSL connection as I do, you only have one.
In this case, name-based virtual hosting is for you.
You don’t have to give your machine multiple IP addresses, but you will
still have to contact the person that is responsible for your DNS server, and
have them put more than one DNS record in for your machine. These extra records
are called C-records, or CNames. (The main record pointing at a machine is
called the AName, or A-record.) You can have as many CNames as you like
pointing to a particular machine.
Once you have your DNS configured to send traffic to you for all these
names, you’ll need to do the following in your server configuration file:
NameVirtualHost 192.168.1.1ServerName name1.mydomain.com DocumentRoot /usr/local/apache/name1docs ServerAlias name1ServerName name2.mydomain.com DocumentRoot /usr/local/apache/name2docs ServerAlias name2ServerName name3.mydomain.com DocumentRoot /usr/local/apache/name3docs ServerAlias name3
Once again, as in the IP-based virtual hosts, you can put any directive you
want in these sections. I've kept them all pretty simple.
There are several things to note here.
First, you must specify, with the
what IP address should be responding to virtual host requests. You can use
IP-based and name-based virtual hosting in conjunction - that is, you can do
both on the same server at the same time. So you must specify which addresses
you're using for this. I'll frequently set up virtual hosts on the internal and
external interface of a server (intranet servers and internet servers, that is)
and so you must specify both addresses:NameVirtualHost 192.168.1.1 NameVirtualHost 188.8.131.52
Furthermore, you can run virtual hosts on different ports, as well as
different addresses, if you really want to complicate things:NameVirtualHost 192.168.1.1:80 NameVirtualHost 192.168.1.1:90
Just make sure that all the names make it into DNS, or you might end up
serving a page that you were not intending to.
The magic for the virtual host stuff is that the browser tells the server
what server it is requesting content from. There are still some older browsers
in use that don't do this correctly, and so they might get content from the
"default" server on your machine. This is almost never a real
consideration, since any browser produced in the last five years (perhaps even
before that--I tend to lose track of time these days) has this ability. There
are work-arounds in Apache so that it will work for even these browsers, but
I'll let you research that on your own, if you really want to. Start at
and go from there.
There's actually a third alternative. You can run completely separate server
daemons on your server machine and have each one serving a separate Web site.
The downside of this is that each one must run on a different port,
since you can't have multiple services listening on the same port at the same
You can get a server to run with a particular configuration file with the