January 6 2017
Subdomains are useful extensions to an already existing domain name. One use-case for subdomains is the creation of memorable web addresses for unique content on your website. Instead of going to www.yourdomain.com/example, a subdomain could allow users to travel to www.example.yourdomain.com.
This guide will walk you through the process of manually setting up subdomains for the Apache webserver via ssh. I’ll assume that you already have a domain name and website which already/plans to use Apache as its webserver, that you are comfortable using ssh from the command line and that you have a web host that allows you to use ssh as a method of administration.
Login to your remote server via ssh as a user with sudo privileges from the terminal:
$ ssh email@example.com
Next, create the folder that you want your subdomain to point to. For Apache, this is typically done in your
$ sudo mkdir /var/www/example
We’ll want to now create an index.html file inside of /var/www/example as a test to see if Apache will serve up the content after the subdomains Virtual Host has been created:
$ cd /var/www/example $ sudo touch index.html
I use vim as my text editor when working in ssh, but many find nano to be more user friendly. Use whichever you prefer to edit the newly created index.html file:
$ sudo vim index.html
Then, edit its contents such that it has some sample text:
Success! Your subdomain serves up content!
Save and exit
Here we’ll be actually setting up the Apache Virtual Host to serve index.html in our
/var/www/example directory. So, navigate to your Apache sites-available directory:
$ cd /etc/apache2/sites-available
Now we’ll copy the
.conf file of whatever is in use / enabled by your current domain. In this example, we’ll copy
000-default.conf and edit it to point to our subdomains root directory created in step one. By convention we’ll name the new file after our subdomains title:
$ sudo cp 000-default.conf example.conf
And we’ll be making some changed to it, so:
$ sudo vim example.conf
Inside the Virtual Host configuration file, we need to change every instance of yourdomain.com to example.yourdomain.com, as well as change the DocumentRoot to the directory created in step one. Then, we need to enable
example.conf so that it appears in
/etc/apache2/sites-enabled. From inside the sites-available directory we type:
$ sudo a2ensite example.conf
And reload Apache to activate the configuration changes:
$ service apache2 reload
Now all that’s left is setting up the subdomain with your domain registrar. This process will vary depending on the particular registrar you go through, but essentially it involves logging in, going to the DNS records settings and adding a new record. For this, I typically create a new CNAME record, give the host name field just the subdomain name (in this case, “example”), plug in the IP Address, and that’s it.
If using a VPS such as DigitalOcean, you may need to do a set up phase that mirrors your registrar DNS records as well.
Finally, if you are having some trouble with your new subdomain simply redirecting back to the main domain, be sure to check your
/etc/apache2/sites-enabled directory for any interfering
.conf files or even any Rewrite settings that may already be in place. To remove any extra
.conf files, type:
$ sudo a2dissite extrafile.conf
extrafile.conf is the file you wish to disable. Then, reload via
service apache2 reload.
A simple guide, to be sure. But it was something that had me confused when first setting up this website and because of it’s relative ease, felt it was unnecessarily difficult to find a step by step guide.
You should now have a working subdomain!