This guide covers the situation where you have already bought a domain and using it on a web server. You want to allocate a subdomain on a folder in the same web server.

1. Setting up the DNS

We want to serve files on our subdomain staging.example.ca

First, let’s add an A record for the subdomain to the DNS in our domain name provider.

We point the subdomain to the same IP Address as our main domain’s web server:

HostnameIP AddressTTL
@123.456.789.111Default
staging123.456.789.111Default

2. Set up folder for serving files

Our main domain example.ca is serving files in this folder:

/var/www/html/

Let’s make a folder to serve files for our subdomain:

mkdir /var/www/staging

3. Configure Apache for the subdomain

3.1 Create the Apache conf file

Let’s now head to /etc/apache2/sites-available/ and create staging.conf

<Directory /var/www/staging/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
<VirtualHost *:80>
    ServerName staging.example.ca
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/staging/
    ErrorLog /var/log/apache2/staging/error.log
    CustomLog /var/log/apache2/staging/access.log combined
    <files xmlrpc.php>
    order allow,deny
    deny from all
    </files>
RewriteEngine on
RewriteCond %{SERVER_NAME} =staging.example.ca
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

We’re creating a new Virtual Host in Apache with this and listening for port 80. We’re also instructing Apache to serve files for staging.example.ca from /var/www/staging/ . Finally, we’re telling Apache to redirect with RewriteEngine to https because we will be setting up a certificate at the same time.

3.2 Create the SSL Apache conf file

Let’s now create staging-le-ssl.conf in the same folder, this will contain our Virtual Host for port 443 for HTTPS encryption:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName staging.example.ca
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/staging/
    ErrorLog /var/log/apache2/staging/error.log
    CustomLog /var/log/apache2/staging/access.log combined
    <files xmlrpc.php>
    order allow,deny
    deny from all
    </files>

SSLCertificateFile /etc/letsencrypt/live/staging.example.ca/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/staging.example.ca/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf

</VirtualHost>
</IfModule>

This almost does the same thing as our first conf file but this Virtual Host listens to port 443 for HTTPS instead. It also tells Apache where the SSL keys are going to be located.

3.3 Create symlinks for the Apache conf files

The next step in configuring Apache for the subdomain is to register a Symbolic Link from the files staging.conf and staging-le-ssl.conf located in /etc/apache2/sites-available/ to /etc/apache2/sites-enabled/ . We need to do this so that Apache knows which conf files to take into account:

cd /etc/apache2/sites-enabled/
sudo ln -s /etc/apache2/sites-available/staging.conf
sudo ln -s /etc/apache2/sites-available/staging-le-ssl.conf

3.4 Create Apache log files

Last thing on the list is to create our log files or Apache will throw us an error:

mkdir /var/log/apache2/staging
touch /var/log/apache2/staging/error.log
touch /var/log/apache2/staging/access.log

3.5 Restart Apache

Finally, let’s restart Apache so our changes take effect:

sudo service apache2 restart

4. Register our subdomain for an SSL certificate

Before doing this step, we should wait for our DNS changes to propagate. This can take anywhere from an hour to a day. You will know when the DNS changes are in effect if you try to go to staging.example.ca and your browser hits you with an unsafe warning.

Now, let’s register our subdomain with certbot and we will be in business! You should read the guide on how to install certbot on your server if you don’t have it already.

We invoke certbot and expand with our new subdomain:

sudo certbot -d example.ca,staging.example.ca --expand

5. We’re in business!

If everything is setup properly, you can start serving files at /var/www/staging/ and access them from staging.example.ca

Let’s get to uploading and let us know in the comments if you encountered any errors along the way.

Leave a Reply