Install and configure nginx

sudo apt install nginx

sudo systemctl enable nginx

sudo systemctl start nginx

Open port 80 and 443

# Enable port 80 and 443 through iptables

sudo iptables -I INPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

sudo iptables -I OUTPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate ESTABLISHED -j ACCEPT

# Save it to persistent

sudo netfilter-persistent save

conf.d vs sites-enabled

Some guide tells you to create symbolic link from sites-available to sites-enabled. But it appeared too bothersome for me. So we're going to make {my-domain.com}.conf and import it to nginx.conf.

my-domain.com.conf

Create configuration for {my-domain.com} /etc/nginx/conf.d/{my-domain.com}.conf

server {
    listen 80;
    server_name {my-domain.com} {www.mydomain.com};
    # `server_tokens off` hides nginx version from client
    server_tokens off;

    location / {
        root /var/www/{my-domain.com};
    }
}

Create default html page for {my-domain.com} /var/www/{my-domain.com}/index.html

<html>
    <head>
        <title>Welcome to {my-domain.com}!</title>
    </head>
    <body>
        <h1>The {my-domain.com} server block is working!</h1>
    </body>
</html>

Change permission for /var/www/{my-domain.com}

sudo chown -R 755 /var/www/{my-domain}

Restart nginx

The below command should return successful

sudo nginx -t
sudo nginx -s reload

# -t is for testing configuration file. Dry run.
# -s {signal}. -s flag sends {signal} to nginx. Reload nginx in our case

Certbot

TODO : install certbot via snap, install certbot-dns-cloudflare, get cloudflare api key

Remove previous version of certbot

sudo apt remove certbot