Website with Nginx and free HTTPS (Let's Encrypt)

Nginx is one of the fastest and most widely used web servers in the world, and Let's Encrypt issues free SSL certificates that renew automatically. In this tutorial we will publish a website with HTTPS on a VPS running Ubuntu or Debian, using Certbot to set everything up.

Before you start

The domain must point to the server: in your domain's DNS panel, create A records for yourdomain.com and www.yourdomain.com with the server IP (it is in your account panel). If a firewall is active, allow ports 80 and 443.

Installing Nginx and Certbot

# apt update
# apt install -y nginx certbot python3-certbot-nginx

Creating the website

Create the website folder with a test home page:

# mkdir -p /var/www/meusite
# echo "<h1>My website</h1>" > /var/www/meusite/index.html
# nano /etc/nginx/sites-available/meusite
Paste the configuration below, replacing the domain with yours:

server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/meusite;
    index index.html index.php;
}
Enable the site, remove the Nginx default site and reload:

# ln -s /etc/nginx/sites-available/meusite /etc/nginx/sites-enabled/
# rm -f /etc/nginx/sites-enabled/default
# nginx -t && systemctl reload nginx
Open http://yourdomain.com and you will see the test page.

Enabling HTTPS with Let's Encrypt

# certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot asks for an email address (used for expiry notices) and for you to accept the terms. Then it issues the certificate, configures Nginx and starts redirecting HTTP to HTTPS. Done: open https://yourdomain.com.

Automatic renewal

Let's Encrypt certificates are valid for 90 days, and Certbot renews them on its own through a system timer. To check that renewal works:

# certbot renew --dry-run
If you see "all simulated renewals succeeded", everything is fine. Put your website files in /var/www/meusite; for PHP websites, install php-fpm and add the PHP block to the site configuration.
If you have any difficulties, open a support ticket: Open ticket