How to add a domain for a static site for Ubuntu and Nginx

Adding a new domain for a static site for the Nginx / Ubuntu combination might look like a piece of cake but if you're not doing this sort of thing regularly, you will forget it. Why not make a blog post about it then?

For a more complete tutorial, assuming you are some poor soul who landed on this website, you can check out this page How to configure Multiple Domains with Nginx on Ubuntu. If you're future me or a similar person who only needs some quick pointers, continue reading!

  1. Create the folder

Your new site will need a folder inside /var/www
sudo mkdir -p /var/www/my.domain.tld/public_html

  1. Create the conf file
    The conf file for a standard version will look something like this and should be named my.domain.tld.conf

sudo touch /etc/nginx/sites-available/my.domain.tld.conf

Its content should look something like this:

server {
     listen 80;
     listen [::]:80;
     server_name my.domain.tld www.my.domain.tld;

     root /var/www/my.domain.tld/public_html;

     index index.html index.htm;

     location / {
          try_files $uri $uri/ =404;
     }
}
  1. Enable the site
    Assuming you know what you're doing and you didn't mess up the conf file's syntax, you can enable the site by creating a symbolic link of the site configuration file in the sites-enabled directory.
    sudo ln -s /etc/nginx/sites-available/my.domain.tld.conf /etc/nginx/sites-enabled/my.domain.tld.conf

  2. Add a SSL certificate
    Thanks to the wonderful service that is Let's Encrypt, Certbot does all the heavy lifting for you. Just run the following command and select the appropriate case:
    sudo certbot --nginx

And that's it! With these simple 4 steps, that I forgot, I managed to move a couple of domains and subdomains from DigitalOcean to Hetzner. I was the same person that set them up on the previous host but hey, my bad for not making a blog post back then, right?