Creating Virtual Hosts with Apache
I get asked this allot, so for all of you interested in knowing how to Create Virtual Hosts with Apache - here you go:
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. I am 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, I am 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.)
I am going to set up the virtual host test_site. So to create the directories for the virtual host, the following commands must be run:
• sudo mkdir /var/www/test_site
• sudo chmod -R 755 /var/www/test_site
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 /var/www/test_site.
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 test_site), create a new file within sites-available called /etc/apache/sites-available/test_site. The contents of that file will look something similar to this (depending upon the needs and application of the virtual host):
#We want to be able to access the web site using www.test.domain.com or test.domain.com
#log file for this server
CustomLog /var/log/apache2/www.test.domain.com-access.log combined
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:
1.) Change to the /etc/apache/sites-enabled directory with the command cd /etc/apache/sites-enabled .
2.) Create the link with the command sudo ln -s /etc/apache/sites-available test_site.
It is also possible to create the links automatically with the command sudo a2ensite test_site and then, should the virtual site need to be disabled, remove the link with the command sudo a2dissite test_site.
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/hosts file in a text editor (with administrative rights).
• Add a line like 127.0.0.1 localhost.localdomain localhost test.domain.com www.test.domain.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:
1.) 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.
2.) Make sure the Apache configuration file is aware of virtual hosts by making sure the line Include conf.d/*.conf is not commented out.
3.) Create the new virtual hosts file (we'll call it vhosts.conf) in /etc/httpd/conf.d/ .
4.) Add the virtual site to the /etc/hosts file.
5.) Restart Apache with the command /etc/rc.d/init.d/httpd restart.
The directory container, for the non-Debian host, will look similar to the very basic container below:
The virtual hosts file mentioned above, will need to look like this:
Now that the virtual host is up and running, it is now possible to build the site within the /var/www/test_site directory. You can do this for as many virtual hosts as you need (assuming the server can take the added load). Building this virtual site could mean either coding by hand or using a tool like Drupal, Xoops, or Joomla! to help build the virtual site.
Hope that helps.