Hosting several websites on the same IP address.

Nov 24, 2006 11:28 GMT  ·  By

Virtual Hosts refers to Apache's ability to host several websites on the same machine and even on the same IP address. For instance, let's say that you have a small website, www.yourdomain.com, which also provides free email accounts to your visitors. Of course, you will also have to set up a webmail so your visitors can actually use the email address created on your site. You could very easily set up the webmail to be accessible from the address www.yourdomain.com/webmail, but for both aesthetic and practical reasons, the webmail service could be found at webmail.yourdomain.com. Moreover, you can set up as many virtual hosts as you want, on a single IP address.

- The virtual hosts can be defined and configured in Apache's config file. This config file can be found in Fedora Core by default in /etc/httpd/conf/httpd.conf but the location of this file may vary on other distributions.

- Once found, open it with your favorite text editor and search for the NameVirtualHost. Make sure it looks like this (and it's not commented out):

code
NameVirtualHost *:80
NOTE: The following example will require you to have the main website located in /var/www/html and the new webmail will be installed in /var/www/webmail. If your locations vary, change the example accordingly, before adding them to httpd.conf.

- To get it up and running quickly, add these lines at the end of httpd.conf:

code
ServerName www.yourdomain.com
DocumentRoot /var/www/html/

ServerName webmail.yourdomain.com
DocumentRoot /var/www/webmail
- If you want more advanced virtual host configuration, you can use any of the following directives:

Logging

- By default, Apache will record messages to access_log and error_log files located in /var/log/httpd. However, if you want each virtual host to have its own separate logs, add these directives to the virtual host block:

code
ErrorLog /var/log/httpd/website1-error_log common
CustomLog /var/log/httpd/website2-access_log common
- For example, if you want your webmail site to record messages to files webmail-access_log and webmail-error_log, then your vhost block for webmail site will look like this:
code
ServerName webmail.yourdomain.com
DocumentRoot /var/www/webmail
ErrorLog /var/log/httpd/webmail-error_log common
CustomLog /var/log/httpd/webmail-access_log common
Error Pages

- You can set Apache to serve a custom page when a visitor gets a 404 (not found) or 500 (internal server error) or any other error code, for that matter. For instance, you can redirect a visitor which received a 404 error to the main index file, or to a 404 file you created. For this, you'll have to add the following directives to the virtual host block, just like in the above example:

code
ErrorDocument 404 /index.htm
ErrorDocument 500 /index.htm
NOTE: The error page could be anything; but remember: the location starts with the directory set in DocumentRoot in httpd.conf. For instance, if your DocumentRoot is /var/www/html and the error page is in /var/www/html/messages/404.htm, then you'll have to append /messages/404.htm to the ErrorDocument directive.

Server Aliases

- If you want to use your virtual host for more than one domain name, you can put ServerAlias directive inside the virtual host block in order to link the two domains together:

code
ServerAlias youdomain.com yourseconddomain.com
Further directives can be found on Apache's documentation page. Don't be afraid to experiment with httpd.conf, but always remember to back it up first before any reconfiguration.