How do I install and secure Nginx with Let’s Encrypt on Ubuntu 18.04 with DNS validation API? My domain DNS hosted with Cloudflare. How do I install Let’s Encrypt to create SSL certificates with Nginx web server running on an Ubuntu Linux 18.04 LTS server?

Introduction: Let’s Encrypt is an SSL certificate authority. One can get a free SSL/TLS certificate with it. Let’s Encrypt root, ISRG Root X1 directly adopted by Microsoft, Google, Apple, Mozilla, Oracle, Blackberry and other vendors. This page shows how to secure Nginx with Let’s Encrypt on Ubuntu 18.04 and use DNS to validate your domain to obtain an SSL/TLS certificate.

Secure Nginx with Let’s Encrypt on Ubuntu 18.04 with DNS Validation

The procedure to install Let’s Encrypt to create SSL certificates is as follows:

  1. Install client
    git clone
  2. Configure Nginx for SSL/TLS
  3. Configure UFW (firewall) to open port 443
  4. Issue SSL/TLS certificate for your domain using DNS validation method
  5. Install SSL/TLS certificate and restart Nginx server
  6. Validate cron job that will renew certificate automatically

Let us see all steps in details.

Step 1. Install client

Naturally, the first step is to install the software to get an SSL certificate. Install requied software using the apt command or apt-get command:
$ sudo apt-get install git bc wget curl
Install software dependencies on Ubuntu 18.04

Clone the repo

$ cd /tmp/
$ git clone

Install client

$ cd
$ sudo -i
# ./ --install

How to install Let's Encrypt client on Ubuntu 18.04 LTS
After install, you must close current terminal and reopen again to make the alias take effect. Or simply type the source command:
$ sudo -i
# source ~/.bashrc
# acme --version

Find out client version

Step 2. Configure Nginx server for SSL/TLS

Use the mkdir command to create directories to store certificate for our domain named
# mkdir -pv /etc/nginx/ssl/letsencrypt/

Generate dhparams.pem file

You are going to use a strong Diffie-Hellman (DH) group, regardless of the server software. Run the openssl command to speed up dhparams generation on Ubuntu 18.04 LTS:
# cd /etc/nginx/ssl/letsencrypt/
# openssl dhparam -out dhparams.pem -dsaparam 4096

Create a strong Diffie-Hellman (DH) group dhparams.pem file

Configure TLS/SSL on Nginx web Server

Update a file named using a text editor such as nano command or vim command for both port 80 and 443:
# nano /etc/nginx/sites-available/
# vim /etc/nginx/sites-available/
Append the following config:

## domain
## redirect all http traffic to https version
server { listen 80; listen [::]:80; server_name; # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response. return 301 https://$host$request_uri;
## domain
## Add all config here like root domain, log files, php config and more
server { listen 443 ssl http2; listen [::]:443 ssl http2; ## my server/domain name and webroot where files are stored server_name; root /home/lighttpd/http; ## certs sent to the client in SERVER HELLO are concatenated in ssl_certificate ssl_certificate /etc/nginx/ssl/letsencrypt/; ssl_certificate_key /etc/nginx/ssl/letsencrypt/; ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_session_tickets off; # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits ssl_dhparam /etc/nginx/ssl/letsencrypt/; # intermediate configuration ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS'; ssl_prefer_server_ciphers on; # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months) add_header Strict-Transport-Security max-age=15768000; # OCSP Stapling --- # fetch OCSP records from URL in ssl_certificate and cache them ssl_stapling on; ssl_stapling_verify on; resolver; # log files access_log /var/log/nginx/cms.cyberciti.biz_access.log; error_log /var/log/nginx/cms.cyberciti.biz_error.lg; ############################ # Add rest of config below # ############################

Step 3. Issue and create an SSL Certificate on Ubuntu for Nginx using DNS method

DNS method allows you to issue an SSL/TLS certificate when having multiple web server running behind a load balancer. You need to use API provided by your DNS service provider to use the DNS validation method with Let’s Encrypt. Here is a list of supported DNS providers:

  1. GoDaddy, Cloudflare, Azure DNS, PowerDNS
  2. OVH/kimsufi/soyoustart/runabove, DNSMadeEasy
  3. AWS Route53, ISPConfig, Linode, Gandi, DigitalOcean, CloudDNS and more
  4. See complete list here

Cloudflare DNS example

For demonstration purpose, I am going to use Cloudflare DNS. First, get your Cloudflare API keys by visiting this page. Type the following commands:


Let’s issue a cert for domain

The syntax is:
# --issue --dns dns_cf -d
# --issue --dns dns_cf -d -d
## wild card certicate
# --issue --dns dns_cf -d *
# --issue --dns dns_cf -d

Secure Nginx with Let’s Encrypt on Ubuntu 18.04 with DNS Validation

Install the issued certificate to Nginx web server

It is time to install certificate and reload the nginx server:
# --installcert -d \
--keypath /etc/nginx/ssl/letsencrypt/ \
--fullchainpath /etc/nginx/ssl/letsencrypt/ \
--reloadcmd 'systemctl reload nginx'

Install Let's Encrypt Free SSL and TLS certificate in Ubuntu for Nginx

Test it

Open Nginx server tcp port # 443 if not already opened using the ufw command based firewall:
$ sudo ufw allow https comment 'Open all to access Nginx port 443'
Fire a web browser and type the url:
Or visit SSL labs to test your TLS/SSL config: SSL labs report

How do I renew a certificate?

# --renew -d

A note about cron job

A cron job will try to do renewal a certificate for you too. This is installed by default as follows (no action required on your part):

2 0 * * * "/root/"/ --cron --home "/root/" > /dev/null

How do I upgrade client?

# --upgrade


There you have it, you just secure Nginx with Let’s Encrypt on Ubuntu 18.04 with DNS validation. You installed Let’s Encrypt free SSL/TLS certificate. You learned how to install client, issue SSL certificates for given domain name, configured Nginx. For more info see client project page here.

Get 15% off on Linux Foundation certified SysAdmin, Progamming, Kubernetes/Containers and Open Stack certification & course. Use “CYBER15” coupon code.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Facebook Comments

More Stuff

How to Find and Install Software Applications in Fedora Linux There are countless software packages are available to install on Fedora Linux distribution from the repository provided by Fedora project. You can al...
5 Cool New Projects to Try in Fedora Linux In this article, we will share five cool new projects to try in Fedora Linux distribution. Note that some of these projects may also be work on other ...
How to Install Memcached (Caching Server) on CentOS 7 Memcached is an open source distributed memory object caching program that allows us to improve and speed up the performance of dynamic web applicatio...
How to Install Adobe Flash Player 32 on Fedora Linux Adobe Flash is a web browser plug-in used to display interactive web pages, online games, and to playback video and audio content. Flash displays text...
Spread the love

Posted by News Monkey