Virtual domains can seem confusing at first, but actual it's pretty simple. Here's the basic idea: you have ONE web server, at ONE IP address, but you host multiple websites, each with different content.
How can that work? Well, part of the secret is Apache itself, but Apache gets a big help from the way that clients (web browsers) use the http protocol to request web pages. We can do some testing with telnet to show examples: (you type the stuff in bold)
bash-2.05a$ telnet pcunix.com 80 Trying 64.226.42.29... Connected to pcunix.com. Escape character is '^]'. GET http://pcunix.com/index.html <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <HTML><HEAD> <TITLE>301 Moved Permanently</TITLE> </HEAD><BODY> <H1>Moved Permanently</H1> The document has moved <A HREF="http://aplawrence.com/index.html">here</A>.<P> <HR> <ADDRESS>Apache/1.3.22 Server at pcunix.com Port 80</ADDRESS> </BODY></HTML> Connection closed by foreign host.
This is what your web browser really sees if it connects to http://pcunix.com. What you see is something different, because your browser pays attention to the "301 Moved Permanently" and gives you "aplawrence.com" instead (for why I do that, see Solving Google Page Rank problems with a 301 Redirect).
Now lets ask for a different virtual domain. Notice that we are still connecting to pcunix.com:
bash-2.05a$ telnet pcunix.com 80
Trying 64.226.42.29...
Connected to pcunix.com.
Escape character is '^]'.
GET http://aplawrence.com/index.html
<html>
<head>
<STYLE TYPE="text/css">
<!--
BODY { background-color: white; }
H2 { color:#000066; font-family: arial, helvetica, sans-serif; }
H3 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4.narrow { color:#000066; font-family: arial, helvetica, sans-serif; margin-left:2em}
P, OL, UL, DL, BLOCKQUOTE { color: #000000; font-family: arial, helvetica, sans-serif; }
... (lines deleted) ...
<br clear=left>
</BODY>
</HTML>
Connection closed by foreign host.
That's different: Apache gave us a different page. Now again:
bash-2.05a$ telnet pcunix.com 80
Trying 64.226.42.29...
Connected to pcunix.com.
Escape character is '^]'.
GET http://www.zenex.com/index.html
<html>
<head>
<STYLE TYPE="text/css">
<!--
BODY { background-color: white; }
H2 { color:#000066; font-family: arial, helvetica, sans-serif; }
H3 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4.narrow { color:#000066; font-family: arial, helvetica, sans-serif; margin-left:2em}
P, OL, UL, DL, BLOCKQUOTE { color: #000000; font-family: arial, helvetica, sans-serif; }
... (lines deleted) ...
<br clear=left>
</BODY>
</HTML>
Connection closed by foreign host.
This time I deliberately made a mistake and asked for a domain that doesn't exist (as far as I know, anyway - and it certainly doesn't exist on my site!). Apache gave me the aplawrence.com page. We'll see why later, but let's ask for the right domain, zenez.com (at the time of this writing Zenez.com was hosted here as a virtual domain but might not be when you read this):
bash-2.05a$ telnet pcunix.com 80 Trying 64.226.42.29... Connected to pcunix.com. Escape character is '^]'. GET http://www.zenez.com/index.html <HTML> <HEAD> <TITLE>ZENEZ Main Home Page</TITLE> </HEAD> <BODY bgcolor="#000000" text="#ffffff" link="#FFFF00" vlink="#FF0000"> <A HREF="http://www.mysql.com">MySQL is a trademark of MySQL AB, Inc. in the United States and other countries.</A><BR> </H6> ... lines deleted ... </center> </BODY> </HTML>
What happpens if we ask for a domain that does exist?
bash-2.05a$ telnet pcunix.com 80
Trying 64.226.42.29...
Connected to pcunix.com.
Escape character is '^]'.
GET http://microsoft.com/index.html
<html>
<head>
<STYLE TYPE="text/css">
<!--
BODY { background-color: white; }
H2 { color:#000066; font-family: arial, helvetica, sans-serif; }
H3 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4 { color:#000066; font-family: arial, helvetica, sans-serif; }
H4.narrow { color:#000066; font-family: arial, helvetica, sans-serif; margin-left:2em}
P, OL, UL, DL, BLOCKQUOTE { color: #000000; font-family: arial, helvetica, sans-serif; }
... (lines deleted) ...
<br clear=left>
</BODY>
</HTML>
It's giving you pages from my site, not Microsoft.
Of course your browser wouldn't connect to one site and then request a page from a different site: I'm just doing that to demonstrate what goes on behind the scenes.
Well, we see know that the client software has to ask for the virtual domain it wants. How does Apache know what to do? That's fairly simple, and only involves changing some configuration files. On my site, that starts with httpd.conf in the /www/conf directory, which has
Include conf/vhosts.conf
in it. The vhosts.conf looks something like this: (I've eliminated uninteresting lines)
<VirtualHost zenez.com> DocumentRoot /usr/aplawfiles/www/virthosts/zenez.com/htdocs ServerName zenez.com ServerAlias www.zenez.com <Directory "/usr/aplawfiles/www/virthosts/zenez.com/htdocs"> Options All AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost pcunix.com> DocumentRoot /usr/aplawfiles/www/virthosts/pcunix.com/htdocs ServerName pcunix.com ServerAlias www.pcunix.com <Directory "/usr/aplawfiles/www/virthosts/pcunix.com/htdocs"> Options All AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost aplawrence.com> DocumentRoot /usr/aplawfiles/www/virthosts/vps.pcunix.com/htdocs ServerName aplawrence.com ServerAlias www.aplawrence.com <Directory "/usr/aplawfiles/www/virthosts/vps.pcunix.com/htdocs"> Options All AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
Notice that the only real difference for each virtual domain is the DocumentRoot: where the actual pages are.
So that's how Apache knows where to go when it sees "GET http://aplawrence.com" vs. "GET http://www.zenez.com". It just goes the the DocumentRoot that its configuration tells it to, and gives you the pages you want.
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar