Almost every website uses subdomains, my website as well for example. When you enter “http://daanlenaerts.com” in the address bar of your browser, you will get to my website. And when you type “http://blog.daanlenaerts.com“, you will be redirected to my blog, where you’re currently reading this article.
In this tutorial I’m going to create the following subdomain: “sub.test1daan.tk”. I got that domain name setup in a different tutorial.
To start with the configuration, sign in on your Ubuntu machine and create a folder which will be used for the subdomain. for me it’ll look like this:
|
1 |
sudo mkdir -p /var/www/test1daan.tk/public_html/sub |
You might have to enter your password to continue.
The folder is created, time to create an index file, so we know we’re on the right track when we visit the subdomain.
|
1 |
sudo nano /var/www/test1daan.tk/public_html/sub/index.html |
Add the following content to the file:
|
1 |
Welcome to my subdomain. |
Save the document by pressing ctrl + X, Y and enter.
We’re also going to set some permissions for the sub folder, so the contents can be accessed by other users.
|
1 |
sudo chmod -R 755 /var/www/test1daan.tk/public_html/sub |
Now it’s time to create a new virtual host file, to map the subdomain to the right folder.
|
1 |
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/sub.test1daan.tk.conf |
We’ve copied the original, default, configuration file to “sub.test1daan.tk.conf”. Let’s open the configurations file.
|
1 |
sudo nano /etc/apache2/sites-available/sub.test1daan.tk.conf |
The file should look like this:
Now edit the following properties, and save the document by pressing ctrl + X, Y and enter.
|
1 2 3 4 |
ServerName sub.test1daan.tk ServerAdmin me@daanlenaerts.com DocumentRoot /var/www/test1daan.tk/public_html/sub |
Save the file by pressing ctrl + X, Y and enter. The only thing we have to do after this is enable the new virtual hosts and restart the apache2 service.
|
1 2 |
sudo a2ensite sub.test1daan.tk.conf sudo service apache2 restart |
The configuration of the subdomain is done. You can visit the subdomain in your favorite browser, and you’ll see this:


Thx for your tutorial! Great! 🙂
Excellent article loved it.
Thanks!