Back to: Technology Guides
How to set up a subdomain on the same Apache web server as the main domain on Debian
To set up a subdomain on the same Apache web server as the main domain on Debian, point a DNS A record for the subdomain at the server, create a folder for it, add an Apache virtual host with the subdomain as its ServerName, enable it with a2ensite and reload Apache. Then secure it with a certbot SSL certificate.
The steps below use Apache 2.4 as packaged in Debian 13 (trixie), the current stable release, as of November 2026. Ubuntu 24.04 LTS and 26.04 LTS use the same Apache layout and commands. Our example adds staging.example.ca to a server that already serves example.ca from /var/www/html.
Step 1: Add a DNS record for the subdomain
Create an A record for the subdomain at your DNS provider, pointing to the same IP address as the main domain. Apache can serve both from one IP address because it picks the site from the name the browser asks for.
Hostname | Points to (A record) |
|---|---|
@ (example.ca) | 203.0.113.10 |
staging | 203.0.113.10 |
Replace 203.0.113.10 with your server’s public IP address, and add a matching AAAA record if the server has IPv6. The default TTL is fine. Check that the record resolves before you go further:
dig +short staging.example.ca
Step 2: Create a folder for the subdomain’s files
Give the subdomain its own document root next to the main site’s, and add a test page so you can tell the two sites apart:
sudo mkdir -p /var/www/staging
echo 'staging.example.ca works' | sudo tee /var/www/staging/index.html
On Debian and Ubuntu, Apache’s main configuration already allows access to everything under /var/www/, so a folder there needs no extra permission block. If you put the files somewhere else, such as /srv/staging, add a <Directory> block with Require all granted for that path.
Step 3: Create an Apache virtual host for the subdomain
A virtual host tells Apache which folder to serve for which name. Create a configuration file for the subdomain in sites-available. The file name must end in .conf, or Debian’s Apache ignores it:
sudo nano /etc/apache2/sites-available/staging.example.ca.conf
Add this configuration:
<VirtualHost *:80>
ServerName staging.example.ca
ServerAdmin webmaster@example.ca
DocumentRoot /var/www/staging
ErrorLog ${APACHE_LOG_DIR}/staging-error.log
CustomLog ${APACHE_LOG_DIR}/staging-access.log combined
</VirtualHost>
ServerName is the name this virtual host answers to, and DocumentRoot is the folder it serves, as Apache’s name-based virtual host documentation describes. ${APACHE_LOG_DIR} is /var/log/apache2 on Debian and Ubuntu. Writing the subdomain’s logs there, with their own file names, means you don’t have to create a log folder first.
The first version of this guide logged to /var/log/apache2/staging/, a folder that doesn’t exist by default. Apache refuses to start when a log folder is missing, which is an easy mistake to miss.
Step 4: Enable the site with a2ensite and reload Apache
Enable the new site, check the configuration, then reload Apache:
sudo a2ensite staging.example.ca.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
`a2ensite` creates the symbolic link from sites-available to sites-enabled for you; it takes the file name, not the full path. apache2ctl configtest should print Syntax OK. If it reports an error, fix it before reloading, so a typo never takes down the main site.
Now open http://staging.example.ca. You should see the test page from step 2. To see how Apache matched each name, list the virtual hosts it loaded:
sudo apache2ctl -S
Apache’s virtual host documentation recommends this -S switch for debugging; each name should appear with the configuration file that serves it.
Step 5: Add the subdomain to your SSL certificate
Once the subdomain loads over HTTP, secure it with a free Let’s Encrypt certificate. You can give it its own certificate or add it to the main domain’s certificate.
If certbot isn’t installed yet, follow our guide to installing an SSL certificate with certbot on Ubuntu and Apache; certbot’s own Apache instructions are the same on Debian once snapd is installed. Then choose one of these:
# Separate certificate for the subdomain
sudo certbot --apache -d staging.example.ca
# Or add it to the main certificate (list every domain)
sudo certbot --apache --cert-name example.ca -d example.ca,www.example.ca,staging.example.ca
The second command must list every domain already on the certificate. The first version of this guide used -d example.ca,staging.example.ca --expand, which leaves www.example.ca off the new certificate if the old one covered it. Our guide to adding a subdomain to an existing certificate with certbot explains when to choose each option.
Why does my subdomain show the main site?
Apache shows the main site when no virtual host matches the subdomain’s name. When nothing matches, Apache uses the first virtual host it loaded, which on Debian is 000-default.conf. Check these in order:
The
ServerNameline matches the subdomain exactly, with no typo.The site is enabled:
ls /etc/apache2/sites-enabled/listsstaging.example.ca.conf.Apache was reloaded after the change.
For HTTPS, the subdomain has its own port 443 virtual host. Certbot creates one when you add the subdomain with
--apache.
When we troubleshoot this at msmtech, we start with sudo apache2ctl -S: it shows whether the subdomain is listed at all, and which file serves it.
Frequently asked questions
Do I need a separate IP address for a subdomain?
No. With name-based virtual hosts, many sites share one IP address, and Apache chooses the site from the host name the browser sends. You only need a DNS record that points the subdomain to the server.
How long does a new subdomain take to work?
It depends on your DNS provider and on the record’s TTL. Instead of waiting a fixed time, run dig +short staging.example.ca until it returns your server’s IP address, then request the certificate.
Do these steps work on Ubuntu?
Yes. Ubuntu packages Apache the same way as Debian, with sites-available, sites-enabled, a2ensite and ${APACHE_LOG_DIR}. The commands are identical on Ubuntu 24.04 LTS and 26.04 LTS.
How do I turn off a subdomain later?
Run sudo a2dissite staging.example.ca.conf, then sudo systemctl reload apache2. The configuration file stays in sites-available, so you can enable it again with a2ensite.
Can I run WordPress on the subdomain?
Yes. Point the virtual host at the WordPress folder and give the site its own database, as in our guide to installing WordPress on an Ubuntu VPS. A VPS gives you the root access these steps need.
Need help with your Apache server? Talk to msmtech.