Now that apache is running and configured on your Gentoo server we can add virtual hosts to let it serve more than one domain.

Virtual Hosts

Your apache server should be set up now, and you should have a handle on your basic configuration options. Apache may be answering requests, but if you want to be able to handle more than one domain with one web server, you'll need to set up a virtual host for each. We'll go through setting up a couple sample domains via virtual hosts.

Create the layout

In this example we'll be using two domains: domain1.com and domain2.com.

In your home directory create a 'public_html' folder:

cd ~
mkdir public_html

Now for each domain we want to host create a folder with a standard set of sub-folders:

mkdir -p public_html/domain1.com/{public,private,log,cgi-bin,backup}

and

mkdir -p public_html/domain2.com/{public,private,log,cgi-bin,backup}

That will create the folders public, private, log, cgi-bin and backup for each of our domains (domain1.com and domain2.com).

index.html

The content of the public folder is, naturally, up to you but for this example I am going to use a very simple html file so we can check the virtual hosts work.

Repeat the process so you have a similar file for domain2.com (simply replace all instances of 'domain1.com' with 'domain2.com).

OK. Now we have a basic structure for our two domains we can look at defining two virtual hosts.

Set the permissions

In order for apache to serve your web pages, the apache process will need to have read permissions for the various files that comprise your sites. We won't go into detail here (you can refer to this series on permissions if you want the details later), but to make sure the permissions are the way we need them run the following two commands:

sudo chmod -R a+rX ~/public_html
sudo chmod a+rx ~

The first command sets everything in your public_html directory to be readable and accessible by all users on the system. The second command sets your home directory to be the same way (though not all the files in it, like we did with public_html). Note that the "X" in the first command is capitalized.

If you add more directories for additional virtual domains later, run that first command again to make sure the new directories are accessible as well.

NameVirtualHost

With virtual hosts, one thing that often confuses people is the NameVirtualHost setting.

For each interface and port apache is set to listen on we need a NameVirtualHost directive. Something to keep in mind is you can only define it once per port.

Rather than add the line for NameVirtualHost in the main config file let's create a new config file to hold the setting. We can call it "namevirtualhost.conf", a nice descriptive name. We'll put it in the custom config directory we created earlier.

Edit the file:

sudo nano /etc/apache2/custom/namevirtualhost.conf

Let's just set NameVirtualHost to apply name-based virtual host logic and settings for HTTP requests made on any available interface (*) at port 80:

NameVirtualHost *:80

Custom Virtual Hosts

We've set up the basics and now we're ready to add our own virtual hosts. We'll create each in its own file in the directory:

/etc/apache2/vhosts.d/

Putting each virtual host in its own file will make it easier to edit, enable, or disable them in the future.

Now let's start by creating the vhost file for domain1:

sudo nano /etc/apache2/vhosts.d/domain1.com.conf

You'll want to tailor this for your own domain, of course, changing the username, directory, and domain name involved. Make sure all the settings relevant to this particular virtual host are inside the "VirtualHost" config block, or those settings will be treated like they're applying to apache's main configuration (because of the way the Include directive we discussed back in the configuration files article works).

Reload apache

To have apache use the new virtual domain we'll need it to reload its settings:

sudo /usr/sbin/apache2ctl graceful

Navigate

To test the domain without creating a DNS zone and record(s) on some Internet nameserver(s), I've modified the "/etc/hosts" file on my local computer to include some entries mapping "domain1.com", etc. to the demo server's public IP:

You can add similar entries in your own "hosts" file, though its location will vary depending on what OS is loaded on your local computer (try a Google search).

NOTE: Remember to remove those hosts entries when you're done with testing. It can get confusing down the line if you forget they're there, and find yourself directed to someplace other than what you've set in the public DNS records for your domain.

With those changes made for testing purposes, you can navigate to your site in a web browser on your local computer:

Tada! You should now see the index.html file you set up for your virtual host instead of apache's default page.

ServerAlias

Note that in the vhost file we set a ServerAlias. Providing you have the DNS set up correctly you can also use that address to reach your site. For quick testing purposes you can place another entry in your "hosts" file (like I did in the example code given above):

It is possible to force a browser that came into a site using a ServerAlias to switch to your main domain, but that involves the use of the "rewrite" module. For now we'll let it slide, but if it's important to switch someone to a main domain from an old one (that you would have set as a ServerAlias), you would need to look into using mod_rewrite to do it.

Repeat as necessary

To create and enable domain2.com simply go through the process again:

sudo nano /etc/apache2/vhosts.d/domain2.com.conf
...
# Enter the details for domain2.com as per the example shown above

To enable the site reload Apache:

sudo /usr/sbin/apache2ctl graceful

Finally add a hosts entry to your local machine and navigate to your second domain:

http://domain2.com
or
http://www.domain2.com

All being well, you will see the "domain2.com" index file.

Log Files

As defined in the vhosts files, each domain has its own log files. Let's take a quick look:

ls /home/demo/public_html/domain1.com/log/

The output should reflect what you set up in the virtual host's config:

access.log error.log

Keeping the logs for each domain separate means you won't have as many log entries to dig through if you have a problem with just one particular domain.

Default

It's important to note that apache is still set up with its default site configuration - listening to port 80, using a DocumentRoot of /var/www/localhost/htdocs, and several other details you can dig out of the apache "default_vhost.include" config file. You might want to change some of those default settings to match your primary site.

Another option is to disable the default virtual hosts by removing their keywords from the file:

/etc/conf.d/apache2

The keywords to remove from the "APACHE2OPTS" setting are "DEFAULTVHOST" and "SSLDEFAULTVHOST". Since the "-D" is part of specifying the keyword, remove the chunks that looks like:

-D DEFAULT_VHOST
-D SSL_DEFAULT_VHOST

Now apache will still treat the first DocumentRoot it finds as the default, and that will be in the non-default vhost config file that comes first alphabetically in your vhosts.d directory. If your primary domain doesn't come first alphabetically, put a "0" at the start of the file to make sure its config will.

You can then also comment out any other configuration options in the httpd.conf file that you would prefer to have set by your default virtual host instead.

Summary

We've gone into some detail here but, overall, setting up a virtual host is relatively straightforward. There are many settings and configurations to take into account but you should have your site up and running in no time.