How to Create a Subdomain on Digital Ocean and Host a Website

If you’re using Digital Ocean, looking to learn how to create a subdomain in your droplet and host a website on it, this guide is for you. We will learn how to create a subdomain like example.domain.com on your Digital Ocean droplet and set it up so that it redirects to the webpage you’re wanting to host on it.

Step 1: Create A Record for Your Subdomain.

Digital Ocean Create a Subdomain

Digital Ocean Create a Subdomain

Log into your Digital Ocean account and go to: Networking >> Domains and select your preferred domain.

There you will see the DNS settings for your domain. All you have to do here is create an A record by typing in the name of your new subdomain in the “Hostname” field and select your main domain’s droplet in the “Will Direct to” section. Keep TTL as default.

Step 2: Create a Directory for Your Subdomain

Log into your droplet via SSH and:

cd /var/www

mkdir example.domain.com

Step 2: Create a new Virtualhost/Server Block file on your Apache or Nginx Server

In the next step, we have to tell our web server what to do if it receives a request for example.domain.com (your subdomain). So, why do we do that? We have to create a Virtualhost file if you’re using Apache, or it’s called a Server Block file if you’re using Nginx as your web server; and configure it so that when the server receives a request for your subdomain, it redirects to the correct subdirectory where your website files are stored. Here are the step by instructions for how to go about doing this on Apache and Nginx web servers:

Digital Ocean Edit Virtualhosts

Digital Ocean Edit Virtualhosts

Create a Virtualhost on Apache 2:

  • cd /etc/apache2/sites-available
  • cp 000-default.conf example.domain.com.conf
  • sudo nano example.domain.com.conf
  • It will look something like this:
    <VirtualHost *:80>
    ServerAdmin webmaster@localhostServerName $domain
    ServerAlias www.$domain

    DocumentRoot /var/www/html

    <Directory /var/www/html/>
    Options FollowSymLinks
    AllowOverride All
    Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

  • Just change “$domain” with your subdomain and change the directory path such as this:
    <VirtualHost *:80>
    ServerAdmin webmaster@localhostServerName example.domain.com
    ServerAlias www.example.domain.com

    DocumentRoot /var/www/example.domain.com

    <Directory /var/www/html/>
    Options FollowSymLinks
    AllowOverride All
    Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

  • Ctrl + O and then Ctrl + X to save the file in Nano.
  • Enable the config file using: sudo a2ensite example.domain.com.conf
  • Restart your Apache server: sudo systemctl restart apache2 or sudo service apache2 restart

Create a Server Block file on Nginx:

  • cd /etc/nginx/sites-available/
  • cp default example.domain.com
  • sudo nano /etc/nginx/sites-available/example.domain.com
  • The file would look something like this:server {
    listen 80;
    listen [::]:80;
    root /var/www/apps;
    index index.html index.htm index.nginx-debian.html;
    server_name apps.example.com;
    }
  • Change it accordingly:server {
    listen 80;
    listen [::]:80;
    root /var/www/example.domain.com;
    index index.html index.htm index.nginx-debian.html;
    server_name example.domain.com;
    }
  • Restart Nginx: sudo service nginx restart

That’s it, that’s how you create a subdomain in your DigitalOcean droplet and host a website on it. Now go visit your subdomain and see if it’s working. Good luck!

Leave a Reply