VIRTUAL HOSTS
Would you like to serve more than one domain from the same server computer?
You COULD do it by running several copies of your web server each listening a different IP address, but this burns up available IP addresses and it also makes for a very inefficient server. Better to run a single web server that picks up all the requests and handles multiple web sites.
SETTING UP SO THE BROWSER CAN SEE MULTIPLE HOSTS
In order for virtual hosting to work, your client systems must resolve down to the same IP address for multiple domain names - this is easy enough to set up as the domain names are registered and administered via DNS or (for testing purposes) via NIS or local files.
Once a client system has identified the IP address, it will contact the server including the server name in its request - thus allowing the server to distinguish the server (domain) that's being called up.
Here's an example of how that protocol works (and how to test it) in practise:
trainee@daffodil:~> telnet snowdrop.spring.wellho.net 80
Trying 192.168.200.154...
Connected to snowdrop.spring.wellho.net.
Escape character is '^]'.
GET /index.html HTTP/1.1
host: snowdrop.spring.wellho.net
HTTP/1.1 200 OK
Date: Sun, 16 Apr 2006 12:59:54 GMT
Server: Apache/2.0.54 (Linux/SUSE)
Last-Modified: Wed, 12 Apr 2006 08:51:19 GMT
ETag: "68c33-3b-f179afc0"
Accept-Ranges: bytes
Content-Length: 59
Content-Type: text/html
The world is round
<a href=/second.html>A second page</a>
trainee@daffodil:~> telnet raindrop.spring.wellho.net 80
Trying 192.168.200.154...
Connected to raindrop.spring.wellho.net.
Escape character is '^]'.
GET /index.html HTTP/1.1
host: raindrop.spring.wellho.net
HTTP/1.1 200 OK
Date: Sun, 16 Apr 2006 13:01:01 GMT
Server: Apache/2.0.54 (Linux/SUSE)
Last-Modified: Wed, 12 Apr 2006 10:17:54 GMT
ETag: "69cba-4e-271f1880"
Accept-Ranges: bytes
Content-Length: 78
Content-Type: text/html
hjdfhjksdfhjksdf<hr><hr>jkhsdhjasdhas<hr>
Raindrops keep falling from the sky
You'll have noticed that both names resolved out to the same IP address - that's set up on our DNS server.
CONFIGURING VIRTUAL HOSTS
At the and of the main Apache httpd configuration file (or in included files if your server is configured using those), you'll add one or more virtual host sections. Here's an example:
#
# VirtualHost template
# Note: to use the template, rename it to /etc/apache2/vhost.d/yourvhost.conf.
# Files must have the .conf suffix to be loaded.
#
# See /usr/share/doc/packages/apache2/README.QUICKSTART for further hints
# about virtual hosts.
#
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost snowdrop.spring.wellho.net>
NameVirtualHost snowdrop.spring.wellho.net
ServerAdmin graham@wellho.net
ServerName snowdrop.spring.wellho.net
</VirtualHost>
<VirtualHost snowflake.spring.wellho.net>
ServerAdmin graham@wellho.net
ServerName snowflake.spring.wellho.net
DocumentRoot /home/snowflake/htdocs
ErrorLog /var/log/apache2/snowflake.error_log
CustomLog /var/log/apache2/snowflake.access_log combined
HostnameLookups Off
UseCanonicalName Off
ServerSignature On
<Directory "/home/snowflake/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost raindrop.spring.wellho.net>
ServerAdmin graham@wellho.net
ServerName raindrop.spring.wellho.net
DocumentRoot /home/raindrop/htdocs
ErrorLog /var/log/apache2/raindrop.error_log
CustomLog /var/log/apache2/raindrop.access_log combined
HostnameLookups Off
UseCanonicalName Off
ServerSignature On
ScriptAlias /cgi-bin/ "/home/raindrop/cgi-bin/"
<Directory "/home/raindrop/cgi-bin">
AllowOverride None
Options +ExecCGI -Includes
Order allow,deny
Allow from all
</Directory>
<Directory "/home/raindrop/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Apart from the "lead" comments, I've stripped out much of the comments from the templates so that you can really see what's going on.
The FIRST virtual host inherits all the settings from the main server setup and has very little extra defined; the others include new directories for the webs site home pages, new sets of options, etc. - sufficient to make them into complete web sites in the own rights.
See also
Virtual Hosting with Apache httpd
Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via
our main web site
Web Application Deployment - Apache httpd - virtual hosts [690] - ()
[1687] - ()
[1896] - ()
[3132] - ()
[3568] - ()
[4404] - ()
resource index - Deployment
Solutions centre home page
You'll find shorter technical items at
The Horse's Mouth and
delegate's questions answered at
the
Opentalk forum.
At Well House Consultants, we provide
training courses on
subjects such as Ruby, Lua, Perl, Python, Linux, C, C++,
Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer)
many questions, and answers to those which are of general
interest are published in this area of our site.