apache Virtual Host Introduction

In this section, we are going to set up a Name Based Virtual Host. This is one of the many features available on the apache web server.

apache Virtual Host Configuration Top of Page

The definition of an apache Virtual Host is as follows:

The term Virtual Host refers to the practice of running more than one web site (such as company1.example.com and company2.example.com) on a single machine. Virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.

--Apache Virtual Host Documentation

There are several steps to this process, listed below.

  1. Create the config file that will define the virtual host.
  2. Create the directory that will contain content for the virtual host.
  3. Restart the web server.
  4. Create and/or verify the DNS entry for the subdomain.
  5. Verify that the system works.

Figure 6.18. Virtual Host Configuration Image #1

Virtual Host Configuration Image #1

In the image above, the commands are thus:

  `cd /etc/httpd/conf.d`                # this directory holds additional config files that the web server loads
  `vi apache-subdomain.conf`            # this file will hold the config directives for our virtual host

Figure 6.19. Virtual Host Configuration Image #2

Virtual Host Configuration Image #2

This image shows the content of the file 'apache-subdomain.conf' config file. Note that there are two stanzas, actually defining two virtual hosts. This is an apache fail-safe. By defining the first Virtual Host with the same configuration directives as the main web server, apache will revert to the main web server in the event that there are any mal-formed requests for access to our server. In those cases, it will serve the content at '/var/www/html'. Note the names of the log files that are stated. These are the same log files that we looked at in the section called "apache Log File Introduction"

At this point, please duplicate the image above as shown on your server. Make sure to enter your own particular information as needed.

apache Virtual Host Content Top of Page

Now we need to create the directory structure that will hold content to serve for the subdomain.

Figure 6.20. apache Virtual Host Image #3

apache Virtual Host Image #3

The steps take are listed below.

  `cd /var/www`                             # change to the appropriate directory
  `mkdir apache-subdomain`                  # create the directory that will hold content
                                            # note that it's named the same as the virtual host
  `echo "this is the alpha apache-subdomain" > apache-subdomain/index.html`
                                            # the index file will be served upon calls to the subdomain
  `cat apache-subdomain/index.html`         # verify that the contents of the file are correct

apache Virtual Host DNS Entry Top of Page

One of the crucial elements that makes the Virtual Host work is to have the proper DNS entry in place. We previously created a DNS entry for apache-subdomain.<your-domain.name> in Figure 3.5, "Configure Rackspace DNS #4". If that's not in place, the Virtual Host won't work. Please verify that the proper DNS configuration is in place[21].

apache Virtual Host Access Top of Page

Two steps left:

  1. Reload the server config: `systemctl reload httpd`
  2. Access the new Virtual Host as shown below.

Figure 6.21. Virtual Host Configuration Image #4

Virtual Host Configuration Image #4

Finally, when called by name, the new Virtual Host shows the content we placed in the appropriate directory. If it works, congratulations. If it doesn't the first thing to check will be the log files. Also, verify the DNS entry.

[21] HINT: try the command `dig apache-subdomain.<your-domain.name`>.