While the default Apache configuration is good enough for mostWeb applications, there's a lot more under the hood of the planet's mostpopular Web server. In this article, find out how to create virtual hostson a single Web server, and use Server-Side Includes for greaterflexibility in your HTML pages.
It's a common misconception that a single Web server can host, or "serve", only a single Web site, since a server typically has only a single IP address assigned to it. This is not at all true - and over the next couple of pages, I'll be showing you how you can use Apache to serve up more than one Web site at a time.
First, though, the basics - every computer on the Internet is assigned an IP address, which serves as a unique identifier for that computer. In order to connect to a Web site, a user needs to know the IP address of the computer on which that Web site is stored. However, since users cannot be expected to remember strings of numbers for different Web sites, the Domain Name System was introduced to make things a little easier.
The Domain Name System, or DNS, maps each numeric string to an easy-to-remember word or phrase. For example, the IP address 216.115.108.243 and the word "yahoo.com" both refer to the same Web site; however, the latter is much easier to remember. By mapping names to IP addresses, the DNS makes it easier to navigate the Web.
What does this have to do with Apache? Well, Apache was the first Web server to introduce "name-based virtual hosting". The concept is simple and elegant: different domain names all point to the same IP address, and the Web server at that IP address has the intelligence necessary to display a different Web page for each domain.
Name-based virtual hosting has been available since the HTTP/1.1 protocol came out. However, there is one gotcha - in order for this to work, the client browser must also include support for HTTP/1.1. Most newer browsers do include this support - and for those which don't, there's a workaround which allows them to perceive similar functionality.{mospagebreak title=Alpha And Beta} The keys to name-based virtual hosting are the appropriately-named NameVirtualHost and directives in the "httpd.conf" configuration file; these directives are used to specify basic information for the "virtual host", such as server name and server root, administrator email address, and log file locations.
Let's consider a simple scenario - creating two virtual hosts on the domain "localhost" (your Linux box) for the Web sites "melonfire-alpha.com" and "melonfire-beta.com". With the new NameVirtualHost directive, this is simplicity itself - open up the "httpd.conf" file in your favourite text editor, look for the "Virtual Hosts" section, and add the following entry to it:
NameVirtualHost 127.0.0.1:80
The NameVirtualHost directive specifies the IP address of the server which
will be used to resolve virtual host names.
With that out of the way, it's now time to begin adding the virtual host definitions themselves.
specifies the IP address of the virtual host. Very simply, this is the IP address of the machine that holds the Web pages you plan to display. In the example above, the machine is "localhost", which traditionally has the IP address 127.0.0.1
The second line
ServerAdmin webmaster@melonfire-alpha.com
specifies the email address of the administrator for this virtual
host.
are probably the most important - they specify the domain name for the
virtual host, and the physical location of the Web pages for that domain on the hard drive. Both these lines are crucial to ensuring that the correct Web page appears when the domain is accessed by a client browser.
Finally, the remaining two lines
ErrorLog logs/melonfire-alpha.com-error_log
CustomLog logs/melonfire-alpha.com-access_log common
specify the files to which server activity is to be logged. You can log
server visits and server errors to separate files for each of your virtual hosts, or put it all into the common log files (the default option).
You can also add other Apache directives to this virtual host entry. Once you're done, close the entry with a