Tuesday 28 May 2013

Creating Subdomains with Apache Virtual Hosts | Apache Subdomain Virtual Host Example

Set Up Subdomains with Apache Virtual Hosts

Some people are unaware that the "www" part of the URL is actually a subdomain (http://www.example.com), and can (in theory) point to a different location than http://example.com - it is not advised that you use www as an active sudomain, as most people expect both example.com and www.example.com to have the same content.

This is actually the basis for canonicalisation, but that's another blog post altogether

So how do we set up subdomains using Apache virtual hosts? It's actually very simple, we just need to configure two things, follow this guide for a very simple apache subdomain virtual host example:

  1. Apache Virtual Host Configuration File
  2. Your DNS settings
Setting up your Apache Virtual Hosts file for subdomains is very easy. In fact, you can pretty much copy and paste my config file, just change to your domain and point "DocumentRoot" to wherever you store your site files. If you have set up a virtual host on your server already, you will recognise the layout of the following apache subdomain virtual host example.



<VirtualHost *:80>
        # Admin email, Server Name (domain name) and any aliases
        ServerAdmin contact@randoof.info
        ServerName  www.randoof.info
        ServerAlias randoof.info

        # Index file and Document Root (where the public files are located)
        DirectoryIndex index.html index.php
        DocumentRoot /srv/www/randoof.info/public_html/

        # Custom log file locations
        LogLevel warn
        ErrorLog /srv/www/randoof.info/logs/error.log
        CustomLog /srv/www/randoof.info/logs/access.log combined
</VirtualHost>

<VirtualHost *:80>
        # Admin email, Server Name (domain name) and any aliases
        ServerAdmin contact@randoof.info
        ServerName  dev.randoof.info
        DocumentRoot /srv/www/randoof.info/dev/

        # Custom log file locations
        LogLevel warn
        ErrorLog /srv/www/randoof.info/logs/dev.error.log
        CustomLog /srv/www/randoof.info/logs/dev.access.log common
</VirtualHost>

Depending on how who your domain is set up with, the actual way you change your DNS settings will vary, but the underlying principle to create subdomains with Apache Virtual Hosts is simple. Create a new "A Record" with the left side ("host name") set to "dev" and the right side pointing to your server's IP address.

Don't forget to reload Apache after setting up your virtual host subdomain (sudo service apache2 reload).

That's basically it, there's not much to the apache subdomain virtual host example, probably the hardest part is the DNS configuration, but generally even that is simple.

6 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. I really like your take on the issue. I now have a clear idea on what this matter is all about..
    free vps

    ReplyDelete
  3. This code helps me to perform DIY in creating a VPS Hosting server for my business. Really Web Hosting is a very technical task and have to face a lots of challenges every day.

    ReplyDelete
  4. It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it. vps

    ReplyDelete
  5. Very good written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can’r wait to read more posts. vps barata

    ReplyDelete