How To Secure Nginx with Let's Encrypt on Ubuntu 18.04

Updated on November 11, 2021
How To Secure Nginx with Let's Encrypt on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04


Let’s Encrypt is a Certificate Authority (CA) that provides a way to obtain and install free TLS/SSL certificates, thereby enabling encrypted HTTPS on web servers. It streamlines the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx.

In this tutorial, you will use Certbot to obtain a free SSL certificate for Nginx on Ubuntu 18.04 and set up your certificate to renew automatically.

This tutorial will use a separate Nginx server block file instead of the default file. We recommend creating new Nginx server block files for each domain because it helps to avoid common mistakes and maintains the default files as a fallback configuration.


To follow this tutorial, you will need:

  • One Ubuntu 18.04 server set up by following this initial server setup for Ubuntu 18.04 tutorial, including a sudo non-root user and a firewall.

  • A fully registered domain name. This tutorial will use your_domain throughout. You can purchase a domain name on Namecheap, get one for free on Freenom, or use the domain registrar of your choice.

  • Both of the following DNS records set up for your server. You can follow this introduction to DigitalOcean DNS for details on how to add them.

    • An A record with your_domain pointing to your server’s public IP address.
    • An A record with www.your_domain pointing to your server’s public IP address.
  • Nginx installed by following How To Install Nginx on Ubuntu 18.04. Be sure that you have a server block for your domain. Again, this tutorial will use /etc/nginx/sites-available/your_domain as an example.

Step 1 — Installing Certbot

The first step to using Let’s Encrypt to obtain an SSL certificate is to install the Certbot software on your server.

The Certbot project recommends that most users install the software through snap, a package manager originally developed by Canonical (the company behind Ubuntu) and now available on many Linux distributions:

  1. sudo snap install --classic certbot

Your output will display the current version of Certbot and successful installation:

certbot 1.21.0 from Certbot Project (certbot-eff✓) installed

Next, create a symbolic link to the newly installed /snap/bin/certbot executable from the /usr/bin/ directory. This will ensure that the certbot command can run correctly on your server. To do this, run the following ln command. This contains the -s flag which will create a symbolic or soft link, as opposed to a hard link:

  1. sudo ln -s /snap/bin/certbot /usr/bin/certbot

Certbot is now ready to use, but in order for it to configure SSL for Nginx, you need to verify some of Nginx’s configuration.

Step 2 — Confirming Nginx’s Configuration

Certbot needs to be able to find the correct server block in your Nginx configuration for it to be able to automatically configure SSL. Specifically, it does this by searching for a server_name directive that matches the domain you request a certificate for.

If you followed the recommended server block set up step in the Nginx installation tutorial, you will have a server block for your domain at /etc/nginx/sites-available/your_domain with the server_name directive already set appropriately.

To check, open the server block file for your domain using nano or your favorite text editor:

  1. sudo nano /etc/nginx/sites-available/your_domain

Find the existing server_name line. It should look like the following:

server_name your_domain www.your_domain;

If it does, exit your editor and move on to the next step.

If it doesn’t, update it to match. Then save the file and quit your editor. If you’re using nano you can do this by pressing CTRL + X then Y and ENTER.

Now verify the syntax of your configuration edits:

  1. sudo nginx -t

If you get an error, reopen the server block file and check for any typos or missing characters. Once your configuration file’s syntax is correct, reload Nginx to load the new configuration:

  1. sudo systemctl reload nginx

Certbot can now find the correct server block and update it.

Next, you’ll update the firewall to allow HTTPS traffic.

Step 3 — Allowing HTTPS Through the Firewall

If you have the ufw firewall enabled, as recommended by the prerequisite guides, you’ll need to adjust the settings to allow for HTTPS traffic. Luckily, Nginx registers a few profiles with ufw upon installation.

You can check the current setting by running the following:

  1. sudo ufw status

You should receive output like this, indicating that only HTTP traffic is allowed to the web server:

Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Nginx HTTP ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6)

To let in additional HTTPS traffic, allow the Nginx Full profile and delete the redundant Nginx HTTP profile allowance:

  1. sudo ufw allow 'Nginx Full'
  2. sudo ufw delete allow 'Nginx HTTP'

Now when you run the ufw status command it will reflect these new rules:

  1. sudo ufw status
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Nginx Full ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx Full (v6) ALLOW Anywhere (v6)

Next, you’ll run Certbot and fetch your certificates.

Step 4 — Obtaining an SSL Certificate

Certbot provides a variety of ways to obtain SSL certificates through plugins. The Nginx plugin will take care of reconfiguring Nginx and reloading the configuration whenever necessary. To use this plugin, run the following:

  1. sudo certbot --nginx -d your_domain -d your_domain

This runs certbot with the --nginx plugin, using -d to specify the names you’d like the certificate to be valid for.

If this is your first time running certbot, you will be prompted to enter an email address and agree to the terms of service. After doing so, certbot will communicate with the Let’s Encrypt server to request a certificate for your domain. If successful, you will receive the following output:

Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem Key is saved at: /etc/letsencrypt/live/your_domain/privkey.pem This certificate expires on 2022-01-27. These files will be updated when the certificate renews. Certbot has set up a scheduled task to automatically renew this certificate in the background. Deploying certificate Successfully deployed certificate for your_domain to /etc/nginx/sites-enabled/your_domain Successfully deployed certificate for www.your_domain to /etc/nginx/sites-enabled/your_domain Congratulations! You have successfully enabled HTTPS on https://your_domain and https://www.your_domain - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - If you like Certbot, please consider supporting our work by: * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate * Donating to EFF: https://eff.org/donate-le - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Your certificates are downloaded, installed, and loaded. Try reloading your website using https:// and notice your browser’s security indicator. It should indicate that the site is properly secured, usually with a green lock icon. If you test your server using the SSL Labs Server Test, it will get an A grade.

Now that you’ve obtained your SSL certificate, the final step is to test the renewal process.

Step 5 — Verifying Certbot Auto-Renewal

Let’s Encrypt’s certificates are only valid for ninety days. This is to encourage users to automate their certificate renewal process. The certbot package you installed takes care of this by adding a renew script to /etc/cron.d. This script runs twice a day and will automatically renew any certificate that’s within thirty days of expiration.

To test the renewal process, you can do a dry run with certbot:

  1. sudo certbot renew --dry-run

If you don’t receive errors, you’re all set. When necessary, Certbot will renew your certificates and reload Nginx to pick up the changes. If the automated renewal process ever fails, Let’s Encrypt will send a message to the email you specified, warning you when your certificate is about to expire.


In this tutorial, you installed the Let’s Encrypt client certbot, downloaded SSL certificates for your domain, configured Nginx to use these certificates, and set up automatic certificate renewal. If you have further questions about using Certbot, their documentation is a good place to start.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us

About the authors
Default avatar

staff technical writer

hi! i write do.co/docs now, but i used to be the senior tech editor publishing tutorials here in the community.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

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!

Let’s Encrypt now supports wildcard certificates. It would be great if you add a section to this tutorial about obtaining a wildcard certificate.

As of January 2020, TLS 1.0 and 1.1 are deprecated, and you will no longer receive an A grade from the SSL Labs Server Test with Nginx out of the box, like you would originally in this guide.

To receive an A, you must edit your Nginx configuration to allow only TLS 1.2 and 1.3. The three files you must change are /etc/nginx/nginx.conf, /etc/nginx/sites-available/example.com, and /etc/letsencrypt/options-ssl-nginx.conf.

There will be a line with ssl_protocols. It will probably say:

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

Edit this line in all 3 files to the following:

ssl_protocols TLSv1.2 TLSv1.3;

Restart with sudo systemctl restart nginx and run the SSL Labs Server Test again, and you should get an A.

Under Step 1 - Installing Cerbot after adding the repository. sudo add-apt-repository ppa:certbot/certbot You’ll probably need to run sudo apt update.

If have - like me - your domains (and associated dns, nameservers) sit behind a service like cloudflare, the above setup might need one more little tweak to avoid the headache of a redirect loop.

Basically I have the non-httpS endpoint redirect to httpS, and it got stuck in a redirect loop. But when I commented out the certbot and ssl stuff from my nginx server block, all was good. The root cause - at least for me - was that cloudflare provides its own ssl cert…so as Joe Fleming stated on his recent blog post: within cloudflare you need to “…switch the crypto config to Full SSL or Full SSL (Strict)…” (by default it usually is configured as “Flexible” (or something similar).
Reference: https://joefleming.net/posts/nginx-ssl-and-cloudflare/

Once I did that tweak in cloudflare all was good, and I was able to stop pulling my hair out, and live a happier day.

Big props to Joe Fleming for posting this solution on his blog (I’m surprised google didn’t bring up any other hits on this topic except for Joe’s)…and as always props to the digital ocean folks for posting these instructions!

Make sure you get A Record for both versions of your site www.example.com and example.com!

If you test your server using the SSL Labs Server Test, it will get an A grade.

I get a B :/

sudo certbot renew --dry-run

Attempting to renew cert (****com) from /etc/letsencrypt/renewal/****.conf produced an unexpected error: Problem binding to port 80: Could not bind to IPv4 or IPv6.. Skipping.

sudo certbot renew --dry-run  --nginx


The problem is the “The certbot package we installed takes care of this for us by adding a renew script to /etc/cron.d” uses the first command and not the second one.

For step 5 (checking if auto-renewal is set up correctly) I had to restart nginx for a successful dry-run.

Great tutorial! I am happy I found Digitalocean services and their amazing documentation. Keep up the good work guys!

Edit suggestion: Add step after adding the repository to sudo apt update. I had to run that for Ubuntu to see the python-certbot-nginx package

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel