Apache is very flexible, and it’s easy to configure Apache to handle several domains even when your Web server only has one IP address to share between them. You can use this to host multiple sites, or just to provide a sandbox for development rather than making changes on your live site. This weekend, we’ll learn how to create virtual hosts with Apache.
Setting up virtual hosts might seem like a big challenge, but it’s not. In fact, you can set up a virtual host with just a few edits to Apache’s configuration and by setting up additional directories for the documents. For this how to, I want to use an Apache installation on a Ubuntu server. Please be aware, the instructions for this may require modification if being done on a non-Debian distribution because of the way that Apache is packaged. However, the Apache directives should be standard across distributions and should work even if Apache isn’t running on Linux.
Creating the Directory Structure
Before the configurations can be tackled, the directory structure for the virtual site must be created. We are going to be working with Apache as installed on a Ubuntu server, so the Apache document root will be
/var/www. The directory structure for the new Web site can be created anywhere. Some create those directories in their home (
~/) directory, some create them in
/usr/local/apache, and other, various locations. For the sake of simplicity, We are going to illustrate setting the virtual host in the document root of Apache (in Ubuntu that would be
/var/www). By doing this, it will not be necessary to change ownership of the newly created directory or the parent directory housing the virtual host (since Apache must have access to the directories and files.)
We are going to set up the virtual host demosite.com. So to create the directories for the virtual host, the following commands must be run:
sudo mkdir /var/www/demosite.com
sudo chmod -R 755 /var/www/demosite.com
Now that the home directory for the virtual host is done, it’s time to start configuring Apache so it is aware of the new site. After configuring Apache, the site can then be built within
The first step in the Apache configuration is to make sure Apache knows virtual hosts are enabled. In the Ubuntu set up look for a line (near the bottom) of
/etc/apache/apache2.conf that looks like:
Make sure that line is not commented out (does not begin with a ‘
#‘ character). That sites-enabled include points to
/etc/apache/sites-enabled. A look in that directory will reveal a file called 000-default. That file should contain all of the directory containers needed for virtual sites on the server. To create a new virtual host (to map the example demosite.com), create a new file within sites-available called /etc/apache/sites-available/demosite.com. The contents of that file will look something similar to this (depending upon the needs and application of the virtual host):
<VirtualHost test.domain.com> ServerAdmin webmaster@localhost #We want to be able to access the web site using www.test.domain.com or test.domain.com ServerAlias www.test.domain.com DocumentRoot /var/www/demosite.com #log file for this server CustomLog /var/log/apache2/www.test.domain.com-access.log combined </VirtualHost>
The above code assumes that domain.com is the actual domain to be used (insert proper domain where necessary). It’s almost ready to fire up, but we have two simple steps left before restarting Apache. The first step is to create a link in
/etc/apache/sites-enabled to the file we just created. This is done with the following steps:
- Change to the /etc/apache/sites-enabled directory with the command
- Create the link with the command
sudo ln -s /etc/apache/sites-available demosite.com.
It is also possible to create the links automatically with the command
sudo a2ensite demosite.com and then, should the virtual site need to be disabled, remove the link with the command
sudo a2dissite demosite.com.
The last step is to make sure the server knows that our new virtual site is to be found on the server and not out on the Internet. For this, do the following:
- Open up the
/etc/hostsfile in a text editor (with administrative rights).
- Add a line like
127.0.0.1 localhost.localdomain localhost test.domain.com www.demosite.com.
- Save and close that file.
Now it’s time to restart Apache with the command
sudo /etc/init.d/apache2 restart and test virtual host by pointing a browser to the address.
If the server hosting the virtual sites is a non-Debian distribution, the steps are different for hosting virtual sites. Here’s how it works:
- Create the directory container for the virtual site in
/etc/httpd/conf/httpd.conf. This container will look similar to that used for the Debian-based server.
- Make sure the Apache configuration file is aware of virtual hosts by making sure the line
Include conf.d/*.confis not commented out.
- Create the new virtual hosts file (we’ll call it
- Add the virtual site to the
- Restart Apache with the command
The directory container, for the non-Debian host, will look similar to the very basic container below:
<VirtualHost *:80> ServerName demosite.com DocumentRoot /var/www/html/demosite.com </VirtualHost>
The virtual hosts file mentioned above, will need to look like this:
<VirtualHost 127.0.0.1> DocumentRoot /var/http/www/demosite.com ServerName www.demosite.com </VirtualHost> <VirtualHost 127.0.0.1> DocumentRoot /var/http/www/demosite.com ServerName test.demosite.com </VirtualHost>
Now that the virtual host is up and running, it is now possible to build the site within the
You can do this for as many virtual hosts as you need (assuming the server can take the added load).