How to set up and enable a subdomain in apache

.The general process for adding a subdomain to your web host, configuring the virtual host, and enabling the domain

apache,ubuntu,virtual host,subdomain

01/10/2017

I have to do this a lot, and I always forget how to set up subdomains, so I figured I'd document it for myself as much as others. Say you want to have a website with its own root directory, but you don't plan on purchasing a separate top level domain for it (TLD). For example, subdomain.mikeheavers.com. The first thing you need to do is create an A record with your web host. They usually have an online interface for doing this. So, you log in to your control panel, and look for something like "domains" or, in my case with digital ocean, "networking". In there, there should be something like "subdomains", or, in my case "create a new record".

You want to create an A record. What is an A record? And why didn't they just call it a subdomain record? Not exactly sure, but I think it might stand for alias. You're essentially telling the server - when someone enters this url, send them to this server.

An A record has two components: a hostname, and a value. For my subdomain, the hostname is subdomain.mikeheavers.com, and the value is the server's IP address - say, 101.102.103.00. This confused me at first, because it seems like saying: send all traffic from subdomain.mikeheavers.com to the root IP / TLD (mikeheavers.com), but that's not the case - you'll set up the server folder for the subdomain next.

Once you've got the A record set up - give it a bit. It could take an hour, maybe longer, to configure everything.

Next thing you need to do is ssh into your server console and go to wherever your server config files are stored. For me, that's /etc/apache2/sites-available. Note that apache has two directories for this process - sites-available and sites-enabled. When you set up a new config file for a new subdomain, apache checks it out, and if things look ok, it moves it over to sites enabled. We'll set that config file up now.

In my host configuration, they provide a template file to make your config files from. Mine's called 000-default.conf. If you don't have one, no big deal, the template will look more or less like this:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/subdomain/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Copy that file and name it according to your subdomain, for example subdomain.conf.

Next, open it in whatever editor you choose. In my case, using vim:

sudo vim subdomain.conf

Change the document root to the directory on your server where you want to put all your files. Note that some servers like to have the public files in a directory called "public_html", others in "www", others in "html". Take a look at your main domain - whatever convention is used there, use it here. Then save the file and close it.

Next you should actually make that directory on your server. Either through FTP, or through the console, go to the lowest directory level you specified above in server root, and make the folder

cd var/www
mkdir subdomain && cd subdomain
mkdir public_html

Now that you have the directory structure in place, give yourself permission to write to it:

sudo chown -R $USER:$USER /var/www/subdomain/public_html

Now, of course, you have to put some html in there. Upload your site, or put a test index.html file in there so you'll know it's working.

Now everything's ready to enable the site in apache. Go back to your config file directory

cd /etc/apache2/sites-available

And run the enable command:

sudo a2ensite subdomain.conf

And restart apache:

sudo service apache2 reload

With any luck, apache will move that config file over from the sites-available to the sites-enabled folder, and if you visit subdomain.mikeheavers.com (or whatever you set up), you'll see the content you uploaded.