Creating Subdomains With Apache 2

In my last post we covered how to host multiple websites from the same server. This is really useful if you have several domains or are hosting websites for others and don’t want to invest in multiple lines and more hardware, but what if you just wanted to create a subdomain for a site you already own?

For example, if your main site was located at ‘www.randymarsh.com’ and you wanted to add a blog, but didn’t want to move away from your domain to access it, you can add a subdomain – ‘blog.randymarsh.com’, for example – at which it can be read.

This is an extension of the longer post about multiple site hosting, so if any parts are not explained, please read the previous entry linked above for a better understanding.

It’s a pretty simple process which can make a single-domain site much more flexible.

Create a New Apache Configuration File

Navigate to your ‘sites-available’ folder and find the configuration file for the site you wish to subdomain. We’re going to work with the first example – ‘randymarsh.com’ and ‘blog.randymarsh.com’:

$ sudo cp randymarsh.com blog.randymarsh.com

Let’s edit the new file to respond to the subdomain and point it to the files needed for the new site.

$ sudo nano blog.randymarsh.com

In addition to changing the commented section at the head of the file to match the new filename, several other changes have been made to the file.

  • The ServerAlias entry has been removed – after all, ‘www.’ is not required for subdomains.
  • ServerName has been changed to represent the subdomain we want to create
  • DocumentRoot now points to a different directory, the one where Randy will keep his blog site.

As with any changes to Apache configuration, we now need to enable the site, reload the configuration files and restart the web server daemon:

$ sudo a2ensite blog.randymarsh.com

$ sudo /etc/init.d/apache2 reload

$ sudo /etc/init.d/apache2 restart

And we’re done – you can use this trick to create as many subdomains as you like, perhaps for a blog, webmail or similar site additions.

 

One Response to “Creating Subdomains With Apache 2”

  1. […] the more basic configurations detailed in previous tutorials, you may occasionally find odd results being returned to your clients. For example, if someone were […]

Leave a Reply