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:
2. Set up folder for serving files
Our main domain
example.ca is serving files in this folder:
Let’s make a folder to serve files for our subdomain:
sudo 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
<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 </VirtualHost> <Directory /var/www/staging/> Require all granted </Directory>
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
3.2 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 located in
/etc/apache2/sites-enabled/ . We need to do this so that Apache knows which conf files to take into account:
sudo ln -s /etc/apache2/sites-available/staging.conf
Alternatively, we can also use the Apache site enabling command
sudo a2ensite /etc/apache2/sites-available/staging.conf
3.4 Create Apache log files directory
Last thing on the list is to create our log file directory or Apache will throw us an error:
sudo mkdir /var/log/apache2/staging
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 our 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
Let’s get to uploading and let us know in the comments if you encountered any errors along the way.