Create subdomain with Apache2 on Ubuntu Server

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.

Before you get started, make sure that your subdomain is pointed towards your server. For more information about domain names, read my article about free domain names here.

In this tutorial I’m going to create the following subdomain: “sub.test1daan.tk”. I got that domain name setup in a different tutorial.

Read my article about setting up domains on Ubuntu Server here.

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:

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.

Add the following content to the file:

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.

Now it’s time to create a new virtual host file, to map the subdomain to the right folder.

We’ve copied the original, default, configuration file to “sub.test1daan.tk.conf”. Let’s open the configurations file.

The file should look like this:

1

Now edit the following properties, and save the document by pressing ctrl + X, Y and enter.

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.

The configuration of the subdomain is done. You can visit the subdomain in your favorite browser, and you’ll see this:

Knipsel

6 Comments

  1. How to create another sub domain ?
    I have created with the same tutorial and add other sub domain
    after I type sudo a2ensite sub.test2daan.tk.conf
    and sudo service apache2 restart

    i can not surf in sub.test1daan
    but i can surf sub.test1daan

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected with IP Blacklist CloudIP Blacklist Cloud