How to set up subdomains with Apache 2

When I created this blog I decided to set up a “blog” subdomain for it so now the blog is accessible via blog.davidecoppola.com instead of www.davidecoppola.com/blog/.

A simple thing to do, but a neat feature for your website.

To set up the subdomain I started creating a new CName field in my DNS panel:

set up subdomain as CName field in DNS zone file

the “Host” value has be the subdomain (“blog” in this case) and it has to “Point to” the main IP address associated with the domain (in my panel “@” is an alias for it). The “TTL” field is used by DNS server to control caching of values and I used the default value for it.

After that I connected to my server via SSH and edited the .conf file associated to this website adding the following lines at the end:

<VirtualHost *:80>
    DocumentRoot "/path/to/www/subdomaindir"
    ServerName subdomain.domain.com
</VirtualHost>

When using Apache 2 the .conf file should be in /etc/apache2/sites-enabled/ or /etc/apache2/sites-available/ (if your website is not enabled yet) and obviously you need to replace those string with the proper data.

Finally you need to restart Apache to get the changes working:

# apache2ctl restart 

or

# service apache2 restart 

Leave a Comment

Your email address will not be published. Required fields are marked *