Subdomain on Digital Ocean Cloud in Simple Steps

Here are the steps to add a subdomain and get it working from a sub-folder in less than 20 minutes.

Step 1: Create an ‘A’ record for the subdomain.

To enable the subdomain we need to first add a subdomain host. Go to your DNS settings page and open the records where you have main domain DNS settings. Now add an ‘A’ record with subdomain in ‘Enter Name’ field (only add subdomain part. For example if you are going to create a subdomain blog.example.com then only enter blog and in the IP address field, enter the droplet IP.

 

 

DigitalOcean Control Panel

 

Step 2: Create a virtual host file for the subdomain.

– Now navigate to your /etc/apache2/sites-available folder.
– Create a file named subdomain.domain.com.conf. You need to replace the subdomain and domain with yours.

– Paste the following code in the file. You can use nano command to create the file.

nano subdomain.domain.com.conf

It will create a document and open it for editing. Replace the subdomain and domain in the code as needed.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/subdomain
    ServerAlias   www.subdomain.domain.com  
    ServerName subdomain.domain.com

    <Directory /var/www/subdomain>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHELOGDIR}/error.log
    CustomLog ${APACHELOGDIR}/error.log/access.log combined

</VirtualHost>

 

Step 3: Enable the subdomain.

We need to enable the subdomain before it can start showing up. Without this step the subdomain will show the main website.

a2ensite subdomain.domain.com

We’ll also need to reload apache with new subdomain configuration.

service apache2 reload

 

Leave a Reply:

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