Apache Virtual Hosts – Creating Subdomains

by / Monday, 04 August 2014 / Published in Apache

More often than not I require more apps than just the regular “Website” for my clients. These apps could range from anything between a custom CMS, an accounting application, a forum or a QMS system. The way in which I address this I by creating a unique sub domain for each application. This tutorial will aid you in achieving just that.

This tutorial will guide you through the steps required to setup sub domains on a Centos 6.5 VPS hosted by Digital Ocean but in general this will work on all linux servers with httpd as a service.

As a prerequisite I always ensure that I’m logged into my server as root and this tutorial assumes you are also root.

Before configuration can begin I would like to explain how Apache deals with any subdomains. A subdomain is in essence a virtual host. There are two types of virtual hosts one can setup in Apache.

  1. A named virtual host and
  2. An IP based virtual host.

What this means to you as an administrator is that you have the ability to serve http content on either a unique name basis or a server IP basis. For the purposes of this tutorial I will be creating a subdomain on my master webserver, so no IP based virtual hosts will need to be setup. I will only be setting up name based virtual servers.

An additional note to consider. If you have already been using your webserver with its default configuration, you will need to ensure that you setup virtual hosts for your current content explicitly.

Step 1: Copy your current webserver folder & change your apache config file

I do this to differentiate between different applications so your folder structure may differ. I ensure that each application sits in its own root folder within the /var/www folder on my webserver.

Now you have setup apache to server up the files in the /var/www/website folder and your website will work as it did before.

We aren’t going to delete the original copy of your website in /var/www/html yet. We need to continue, check and verify before we can do that.

Step 2: Add a subdomain / Virtual Host

Your new applications files can now be placed in /var/www/subdomain and if you type in subdomain.yourdomain.com you will see the content.

Step 3: Restart Apache

Step 4: Remove previous default DocumentRoot folder
 

All setup. Enjoy your newly created subdomain and have a cup of coffee.

TOP