Add Sub Domain in Digital Ocean Cloud Hosting Droplet Ubuntu 20.04

Sub domain are useful to create multiple sub-websites for a website. Digital Ocean Cloud Hosting Droplet can be used to host multiple sub domain. In this article we are going to learn how to add a sub domain.

Prerequisite:

Login using putty with non root user with super user privilege

Add Domain Name to Digital Ocean Cloud Hosting Droplet

(All the chapters related cloud hosting are present below this article)

Sub Domain vs Domain

Before proceeding further to learn adding a sub domain, we must have to understand the difference between a domain, adding a domain and sub domain.

Domain is a individual website. Such as gyanol.com is an example of domain name.

At gyanol.com I share php, python, jquery etc. related topics. This one website contains several topics. So instead of one website I can create multiple website such as php.gyanol.com, python.gyanol.com, jquery.gyanol.com etc.

php.gyanol.com, python.gyanol.com are sub website of the website gyanol.com. These sub websites are known as sub domain. If I have registered a domain name then I can create any number of sub domain I want.

We can add multiple sub domain in single droplet. And similarly we can also add more than one domain in a single droplet.

Lets see how can we create a sub domain. For this tutorial, I am going to create php.gyanol.com as sub domain.

Adding Sub Domain in Digital Ocean Cloud Hosting

While adding a domain name in digital ocean cloud hosting, the first step used to click on the three dots of droplet and select add domain. But while adding sub domain to a droplet, we are not required to click add domain option. Rather we are required to create cname record in the dns section.

To find DNS Section of your website, after login into digital ocean account, it lists all the added website. Now click on the website whose subdomain you want to create. In my case, I want to create sub domain for gyanol.com. So I clicked on gyanol.com.

sub domain

Then you will be navigated to the above form. Here you need to select cname (See in the picture, there is menu list at the top A AAA CNAMe MX…, select cname)

cname

Then the above form will appear. Inside hostname, write the subdomain name. In my case, it is php. Because I want to add php.gyanol.com as sub domain. So Inside hostname box I am going to put php. Inside the other box either I can put @ or I can put the root domain name. My root domain name is gyanol.com.

Then click on create record.

Now we need to create root directory (Folder) for the sub domain. Keep in mind /var/www/html is the root directory for the website gyanol.com. So either I can create a folder inside html or inside www for the added sub domain. Whereever you create the folder for the sub domain, that path you need to set in the config file. (Which we will learn ahead).

To keep things neat, I am creating folder inside www and I am naming it php.gyanol.com. Therefore the path to this directory is /var/www/php.gyanol.com.

Note: At this moment www directory contains two directories. 1. html and 2. php.gyanol.com

So far I have created the sub domain directory. Now its time to tell the apache server about this sub domain destination. We do this by creating a config file.

(I have discussed in detail about these config file in chapter 10. All the chapter list are present below this article)

Setup config file

Use below command to create copy of 000-default.conf file.

test@tester:~$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/php.gyanol.com.conf

Now its time to edit the copied file. Use following command.

test@tester:~$ sudo nano /etc/apache2/sites-available/php.gyanol.com.conf

The config file is ready to edit.

<VirtualHost *:80>

ServerAdmin info@gyanol.com

ServerName php.gyanol.com

DocumentRoot /var/www/php.gyanol.com

<Directory /var/www/php.gyanol.com/>

Options -Indexes +FollowSymLinks

AllowOverride All

Require all granted

</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Copy the red marked text and blue marked text only into your config file. Change php.gyanol.com from above site to your desired sub domain.

Save the file and exit. (Press ctrl+x, then press y and then enter)

Its time to enable this config file so that apache server would know about it.

test@tester:~$ sudo a2ensite php.gyanol.com.conf

It will ask to restart apache. Use following command.

test@tester:~$ sudo systemctl restart apache2

That’s it. Now you are ready to upload all the required files in the sub domain directory.

Conclusion

I hope you have understood from this article how to add sub domain for Apache Server Ubuntu 20.04 Digital Ocean Cloud Hosting.