Report this

What is the reason for this report?

Setup up a subdomain for my blog

Posted on June 1, 2023

There’s a great guide on how to set up a WordPress droplet on Digitalocean:

https://www.digitalocean.com/community/tutorials/how-to-use-the-wordpress-one-click-install-on-digitalocean-2

… but it stops short of indicating how to set up a subdomain that points to that WordPress instance.

What I’d like to do is set up a subdomain, blog.mydomain.com, which points to the home page (index.html) of that WordPress instance.

Yes, there’s an article on how to set up a subdomain to a domain, but it’s unclear what modifications (if any) to this process are required when working specifically with WordPress:

https://docs.digitalocean.com/products/networking/dns/how-to/add-subdomain/

Thanks for any help you can provide. And apologies in advance if this my question is already answered somewhere.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hey @alsargent,

First, as you mentioned you need to create a DNS record for your subdomain. Depending on where your DNS is managed at, go there and create two A records. One for blog.mydomain.com and another for www.blog.mydomain.com. Both records should be pointing to your Droplet.

Once that is done, SSH to your Droplet. I’ll assume you’re using Apache or NGINX, as they are the most common web servers. Here are some basic steps for both.

Apache

  1. Create a new virtual host in your Apache configuration. This file could be located in different places depending on your OS, but commonly it’s in /etc/apache2/sites-available. You could create a new file such as subdomain.example.com.conf:
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName subdomain.example.com
    DocumentRoot /var/www/subdomain
    <Directory /var/www/subdomain/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

In this case, the WordPress files should be located in /var/www/subdomain.

Please also note, you’ll need to update the settings inside accordingly to your domain.

  1. Enable this new site:
sudo a2ensite subdomain.example.com
  1. Reload Apache to apply the changes:
sudo systemctl reload apache2

NGINX

  1. Create a new server block in your NGINX configuration. This file could be located in different places depending on your OS, but commonly it’s in /etc/nginx/sites-available. You could create a new file such as subdomain.example.com:
server {
    listen 80;
    server_name subdomain.example.com;
    root /var/www/subdomain;
    index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }
}
  1. Enable this new site:
sudo ln -s /etc/nginx/sites-available/subdomain.example.com /etc/nginx/sites-enabled/
  1. Test NGINX configuration to make sure there’s no syntax error:
sudo nginx -t
  1. If the test is successful, reload NGINX to apply the changes:
sudo systemctl reload nginx

In both cases, don’t forget to adjust the paths, domain names and PHP versions according to your environment. Also remember to configure DNS for your subdomain to point to the correct server.

Before you start, make sure you have a backup of your server, or at least your current configurations. If something goes wrong, you can always revert to the previous state.

Next, you’ll just need to install WordPress in the directory you have configured in your webservice.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.