Secure Nginx with Let's Encrypt on Debian 10 Linux

Updated on

5 min read

Let's Encrypt for Nginx on Debian 10

Let’s Encrypt is a free, automated, and open certificate authority developed by the Internet Security Research Group (ISRG) that provides free SSL certificates.

Certificates issued by Let’s Encrypt are trusted by all major browsers and valid for 90 days from the issue date.

This tutorial shows how to install a free Let’s Encrypt SSL certificate on Debian 10, Buster running Nginx as a web server. We’ll also show how to configure Nginx to use the SSL certificate and enable HTTP/2.


Ensure the following prerequisites are met before proceeding with the guide:

  • Logged in as root or user with sudo privileges .
  • The domain for which you want to obtain the SSL certificate must point to your public server IP. We’ll use
  • Nginx installed .

Installing Certbot

We’ll use the certbot tool to obtain and renew the certificates.

Certbot is a fully-featured and easy to use tool that automates the tasks for obtaining and renewing Let’s Encrypt SSL certificates and configuring web servers to use the certificates.

The certbot package is included in the default Debian repositories. Run the following commands to install certbot:

sudo apt updatesudo apt install certbot

Generating Dh (Diffie-Hellman) Group

Diffie–Hellman key exchange (DH) is a method of securely exchanging cryptographic keys over an unsecured communication channel.

We’re going to generate a new set of 2048 bit DH parameters to strengthen the security:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

You can also change the size up to 4096 bits, but the generation may take more than 30 minutes depending on the system entropy.

Obtaining a Let’s Encrypt SSL certificate

To obtain an SSL certificate for the domain, we’re going to use the Webroot plugin. It works by creating a temporary file for validating the requested domain in the ${webroot-path}/.well-known/acme-challenge directory. The Let’s Encrypt server makes HTTP requests to the temporary file to validate that the requested domain resolves to the server where certbot runs.

We’re going to map all HTTP requests for .well-known/acme-challenge to a single directory, /var/lib/letsencrypt.

Run the following commands to create the directory and make it writable for the Nginx server:

sudo mkdir -p /var/lib/letsencrypt/.well-knownsudo chgrp www-data /var/lib/letsencryptsudo chmod g+s /var/lib/letsencrypt

To avoid duplicating code, we’ll create two snippets that will be included in all Nginx server block files.

Open your text editor and create the first snippet, letsencrypt.conf:

sudo nano /etc/nginx/snippets/letsencrypt.conf
location ^~ /.well-known/acme-challenge/ {
  allow all;
  root /var/lib/letsencrypt/;
  default_type "text/plain";
  try_files $uri =404;

The second snippet ssl.conf includes the chippers recommended by Mozilla , enables OCSP Stapling, HTTP Strict Transport Security (HSTS), and enforces few security‑focused HTTP headers.

sudo nano /etc/nginx/snippets/ssl.conf
ssl_dhparam /etc/ssl/certs/dhparam.pem;

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;

ssl_stapling on;
ssl_stapling_verify on;
resolver valid=300s;
resolver_timeout 30s;

add_header Strict-Transport-Security "max-age=63072000" always;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;

Once done, open the domain server block file and include the letsencrypt.conf snippet as shown below:

sudo nano /etc/nginx/sites-available/
server {
  listen 80;

  include snippets/letsencrypt.conf;

Create a symbolic link to the sites-enabled directory to enable the domain server block:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Restart the Nginx service for the changes to take effect:

sudo systemctl restart nginx

You’re now ready to obtain the SSL certificate files by running the following command:

sudo certbot certonly --agree-tos --email --webroot -w /var/lib/letsencrypt/ -d -d

If the SSL certificate is successfully obtained, the following message will be printed on your terminal:

 - Congratulations! Your certificate and chain have been saved at:
   Your key file has been saved at:
   Your cert will expire on 2020-02-22. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:
   Donating to EFF:          

Edit the domain server block and include the SSL certificate files as follows:

sudo nano /etc/nginx/sites-available/
server {
    listen 80;

    include snippets/letsencrypt.conf;
    return 301 https://$host$request_uri;

server {
    listen 443 ssl http2;

    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;
    ssl_trusted_certificate /etc/letsencrypt/live/;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    return 301$request_uri;

server {
    listen 443 ssl http2;

    ssl_certificate /etc/letsencrypt/live/;
    ssl_certificate_key /etc/letsencrypt/live/;
    ssl_trusted_certificate /etc/letsencrypt/live/;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    # . . . other code

The configuration above tells Nginx to redirect from HTTP to HTTPS and from www to non-www version.

Restart or reload the Nginx service for the changes to take effect:

sudo systemctl restart nginx

Open your website using https://, and you’ll notice a green lock icon.

If you test your domain using the SSL Labs Server Test , you’ll get an A+ grade, as shown in the image below:


Auto-renewing Let’s Encrypt SSL certificate

Let’s Encrypt’s certificates are valid for 90 days. To automatically renew the certificates before they expire, the certbot package creates a cronjob and a systemd timer. The timer will automatically renew the certificates 30 days before its expiration.

When the certificate is renewed we also have to reload the nginx service. Open the /etc/letsencrypt/cli.ini and add the following line:

sudo nano /etc/letsencrypt/cli.ini
deploy-hook = systemctl reload nginx

Test the automatic renewal process, by running this command:

sudo certbot renew --dry-run

If there are no errors, it means that the renewal process was successful.


Having an SSL certificate is a must nowadays. It secures your website, increases SERP ranking position, and allows you to enable HTTP/2 on your web server.

In this tutorial, we have shown you how to generate and renew SSL certificates using the certbot script. We have also shown you how to configure Nginx to use the certificates.

To learn more about Certbot, visit the Certbot documentation .

If you have any questions or feedback, feel free to leave a comment.

This post is a part of the How to Install LEMP Stack on Debian 10 series.
Other posts in this series: