GuidesApache Guide: Setting Up Virtual Hosts Page 2

Apache Guide: Setting Up Virtual Hosts Page 2




Once you have given your machine multiple IP addresses, you will need to put
each IP in your DNS, so that it will resolve to the names that you want to give
those addresses. Again, I’m not going to go into the details of setting up DNS
records. You should contact the person that is responsible for your DNS server
to get these records put in place.

Now, the part that directly relates to Apache. Assuming that you have all
the IP addresses on your machine, and each IP address has a DNS record for it,
you’ll put the following information in your Apache httpd.conf
configuration file.

For this discussion, I’ll assume that you have 3 IP addresses, and that
you’ve given them the names name1.mydomain.com,
name2.mydomain.com, and name3.mydomain.com. You can, however,
given them names from entirely different domains, such as
www.mydomain.com and www.myotherdomain.com.

The addition to your httpd.conf file will look like:

        
        DocumentRoot /usr/local/apache/name1_www
        ServerName name1.mydomain.com
        ErrorLog /usr/local/apache/logs/name1_logs/error_log
        TransferLog /usr/local/apache/logs/name1_logs/access_log
        
        
        ServerAdmin [email protected]
        DocumentRoot /usr/local/apache/name2_www
        ServerName name2.mydomain.com
        ServerAlias name2
        
        
        DocumentRoot /usr/local/apache/name3_www
        ServerName name3.mydomain.com
        ScriptAlias /use/local/apache/name3_cgi
        

Notice that you don't need to configure everything for each virtual
host. Whatever you don't specify, it will inherit from the main server
configuration. For example, you'll notice that on the second virtual host, I
did not specify a location for the server logs. This virtual host will log to
the main server log files.

Notice also that in the third section, I specify a CGI directory. All the
other sections will default to using the cgi directory specified in the main
server configuration, when a URL is accessed containing /cgi-bin/,
because it has not been specified in the virtual host configuration section.

Almost any configuration directive is valid inside a VirtualHost section. In
the server documentation, you'll notice that each directive says where it is
valid, and it should be pretty clear whether or not you can put a particular
directive in one of these sections.

Remember to test your new configuration files before restarting your server:

       /usr/local/apache/bin/apachectl configtest

Another handy tip, which also applies to name-based virtual hosts, is to use
the ServerAlias directive. When accessing a server internally, on
your LAN, you'll often want to use the server's short name, eg
"name2", rather than the full name "name2.mydomain.com."
Using the ServerAlias ensures that you don't get an unexpected
page when trying to use that short name. Apache has no way to know that
"name2" and "name2.mydomain.com" are actually the same Web
site.

Once you restart your server to reload the configuration file, Apache will
figure out, based on the URL that you typed in, which web site you are trying
to access, and will serve you the right page based on that information. Even
though they are all on the same physical machine. Pretty cool, huh?

Name-Based Virtual Hosts

Latest Posts

Related Stories