Raspberry Pi Apache2 Web Server on Raspbian, Part 3.5

In our last tutorial we registered a domain name and set up dynamic DNS. Today we’re going to go over the relatively easy task of adding a subdomain to our website.

Part 1Part 2Part 3

So, what’s a subdomain?

Strictly speaking, subdomains are anything that comes before our top-level domain (TLD). In my case, shadycrzy is a subdomain of the TLD .com. Taking that a step further, www is a subdomain of shadycrzy.com. This is what is usually meant when people talk about subdomains: everything that comes before our domain name.

I decided to set up a subdomain for my friend at friend.shadycrzy.com but feel free to name your subdomain whatever you like (e.g. blog.yourdomain.com, photos.yourdomain.com). To do this, we need to do two things.

First, we need to modify our Apache server configuration file. If we followed along with Part 1 of the Apache web server tutorial we can edit the file by typing in sudo nano /etc/apache2/apache2.conf.

nano is a text editor for the Linux terminal. It lets us view and modify text files on our Raspberry Pi. /etc/apache2/apache2.conf is a file path; “etc” and “apache2” are directories, the same as we would find on a Windows or Mac PC. apache2.conf is the configuration file for the Apache software. It tells the software how to behave. We’re going to change a few of the settings to set up a subdomain on our Pi.

At the bottom of the configuration file we need to add eight lines as follows:

<VirtualHost *:80>
    ServerName www.yourDomainName.com
    DocumentRoot "/var/www/html"
</VirtualHost>

<VirtualHost *:80>
    ServerName subDomain.yourDomainName.com
    DocumentRoot "/var/www/subDomain"
</VirtualHost>

VirtualHost in this case refers to the practice of having more than one website on the same server. Immediately after this is *:80. The asterisk is a stand-in for our IP address and 80 is the port the virtual host will listen to for incoming web requests. Within the VirtualHost tags, we are required to input at least two pieces of information. First is the server name. For our first virtual host (which is also the default) we put our primary domain name. For the second one, put in our new subdomain.

The second piece of required information is the root directory of the server. When we installed Apache /var/www/html was set up as the root directory of our server. If we want, we can change it, but it’s in a good place so I recommend leaving it. For our second virtual host, again, we can make its root directory anything we want but I made a new directory for mine in the /var/www directory with the same name as my subdomain.

To save and exit nano press Ctrl + x, press y to confirm we want to save the changes, then press Enter to keep the same file name. Now, let’s make that directory that we were just talking about. In the terminal navigate to the /var/www directory by typing cd /var/www. cd is a command that means “change directory.” Then type sudo mkdir subDomain. mkdir is short for “make directory.”

Back to the DNS!

Everything is set up from our end of things. Now we just need to take care of things with our domain name provider. I’ll explain how to do this with Namecheap.com but the process should be similar regardless of which domain name registrar was used.

Remember how we had to set up an A record with our domain name registrar to get the Internet to recognize our domain name? Well, we have to do the same thing for our subdomain. Log in to your Namecheap account and navigate to the management page.

From here go to the Advanced DNS tab and under the Host Records section click on Add New Record. We want to make a new A + Dynamic DNS record with our new subdomain as the host. Enter our IP address as the value and we’re all set. Just as before it will take up to 30 minutes for the changes to the DNS to propagate globally.