This document serves to show you how to create sub-domains in apache. Searching around you'll likely find lots of articles with conflicting information along side of articles which spend way too long explaining what sub-domains are and why they're used. Allow me to serve to you, the straight to the point, definitive guide to setting up an Apache sub-domain in less than ten minutes!
So here is the task list of what needs to be done to get a working sub-domain up and running:
Head to your hosting provider's DNS management interface (in my case following from my previous document I'm using Linode). You want to add a new A
record. First locate your A/AAAA
Records section in your DNS configuration page:
Then add a new record. Set the hostname to what you want the sub-domain name to be (in my example I use 'blog' which will make the final path 'blog.felixm-blog-domain.com'). Set the IP Address to whatever your primary A record is set to or the public IP of your web server:
Once this record has been added we can move onto the configuration files!
The files we need to modify are httpd.conf
and if you're using HTTPS also httpd-vhosts.conf
.
Going into httpd.conf
located in /etc/httpd/conf/httpd.conf
you should find a block that looks a little like this:
<Directory "/www/html"> Options Indexes FollowSymLinks AllowOverride All Require all granted ErrorDocument 404 /error.html ErrorDocument 403 /error.html </Directory>
This is the configuration for the primary index of your web server and sets permissions like who can access the files in that folder and which pages to redirect HTTP codes to. All we need to do is create a new folder in your /www
(or whatever path is used in your original block) to house all the content that will be stored for your subdomain. I created a the following: /www/blog
. Then copy the original block, paste it below and modify it slightly:
<Directory "/www/blog"> Options Indexes FollowSymLinks AllowOverride All Require all granted ErrorDocument 404 /error.html ErrorDocument 403 /error.html </Directory>
Now let's go to our httpd-vhosts.conf
located in /etc/httpd/conf/extra/httpd-vhosts.conf
. Find the blog that looks like this:
<VirtualHost *:80> ServerAdmin felix@example.com DocumentRoot "/www/html" ServerName felixm-blog-domain.com ErrorLog "/var/log/httpd/felixm-blog-domain.com-error_log" CustomLog "/var/log/httpd/felixm-blog-domain.com-access_log" common RewriteEngine on RewriteCond %{SERVER_NAME} =felixm-blog-domain.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>
Just like before we can copy and paste this block just below and modify it:
<VirtualHost *:80> ServerAdmin felix@example.com DocumentRoot "/www/blog" ServerName blog.felixm-blog-domain.com ErrorLog "/var/log/httpd/blog.felixm-blog-domain.com-error_log" CustomLog "/var/log/httpd/blog.felixm-blog-domain.com-access_log" common RewriteEngine on RewriteCond %{SERVER_NAME} =blog.felixm-blog-domain.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>
Once you've saved this you can now restart the Apache service which for me on Arch is systemctl restart httpd
. Before you test it's working make sure you put an index.html
file inside the new Document Root folder (which for me is /www/blog
).