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

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

2. Set up folder for serving files

Our main domain is serving files in this folder:


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
<VirtualHost *:80>
    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
RewriteEngine on
RewriteCond %{SERVER_NAME}
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

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 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>
    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

SSLCertificateFile /etc/letsencrypt/live/
SSLCertificateKeyFile /etc/letsencrypt/live/
Include /etc/letsencrypt/options-ssl-apache.conf


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 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, --expand

5. We’re in business!

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

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

Leave a Reply