Manage subdomains with Apache

Introduction

I spent more time that I'm comfortable admitting trying to get subdomains to work on the server you're currently browsing this page over. Very ucomfortable, especially considering that I had already done it on the previous occasion I had published content here, which was a very long time ago. So this is as much a little configuration gist, as it is a resurrection for this born-dead blog.

TL;DR: You need to declare an A record on your domain provider's website, to redirect all subdomains to your IP. Then, having a minimal Apache configuration is enough for it to work for any number of subdomains.

Set the DNS A Record

This was my big waste of time, thinking that I didn't need to register anything DNS wise in order to make this work. Or rather that if there's a way, I didn't find it out, but this makes it so very easy and straight-forward. You can get a more exhaustive list of records here, but you should have an easier way of editing them through you're domain's provider. In my case, I'm planning on having a bunch of different subdomains, and I want all of them to point to my Apache server. In order to do that, you just have to add an A record that looks like this:

*.yourdomain.name

And gives as an answer your server's IP. Once registered, you can check that it works using the dig command, and trying a random or specigic subdomain, depending on how you set the A record, and to see what it answers. In my case, it looks like this:

dig subdomain.platyshell.me


; <<>> DiG 9.16.7 <<>> subdomain.platyshell.me
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 35605
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1460
; COOKIE: 8dfda19b91bb4825cc325d605f85d3ba6996b4bd84abd470 (good)
;; QUESTION SECTION:
;subdomain.platyshell.me.   IN  A

;; ANSWER SECTION:
subdomain.platyshell.me. 300    IN  A   139.162.142.115

;; Query time: 1053 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Tue Oct 13 18:20:10 CEST 2020
;; MSG SIZE  rcvd: 96

Apache configuration

The only thing left for us to do is use the virtual host from the apache configuration that we had for our main domain, and substitute the domain name for the subdomain name. It can be as basic as this without SSL:

<VirtualHost *:80>
        ServerName sub.mydomain.com
        DocumentRoot /var/www/html/sub/
        <Directory /var/www/html/sub/>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>
</VirtualHost>

And that means that you can basically have a new configuration file that has this kind of structure for each of your subdomain. And the only thing needed once you declared the record, is create the folder the Apache <VirtualHost> is pointing to, enable it with sudo a2ensite sub-mydomain.conf and restart Apache (systemctl restart apache2.service).

links

social