How To Secure Nginx with Let's Encrypt on ubuntu | Ram Laxman Yadav

How To Secure Nginx with Let's Encrypt on ubuntu

Hello All, Today I am gona discuss how to secure you website with free ssl provided by Let's Encrypt with nginx web server on an ubuntu machine.Here are some few steps you have to follow to do this:

Install Let’s Encrypt Client

You have to install Let's Encrypt Client to generate a ssl certificate for your site. The best way to install Let’s Encrypt Client is download it from the EFF’s download site.

  • cd /usr/local/sbin
  • sudo wget

You should now have a copy of certbot-auto in the /usr/local/sbin directory.`

  • Make the script executable by running command sudo chmod a+x /usr/local/sbin/certbot-auto

Setup nginx to get a certificate

The Webroot plugin works by placing a special file in the /.well-known directory within your document root, which can be opened (through your web server) by the Let's Encrypt client for validation. Depending on your configuration, you may need to explicitly allow access to the /.well-known directory.

To ensure that the directory is accessible to certbot-auto for validation, change the nginx configuration, as like:

  • suno nano /etc/nginx/site-enabled/(default or your config file)

  • Add following block of code inside the file.

           location ~ /.well-known {
                      allow all;

  • Run sudo nginx -t to test that nginx configs are right or not.

  • Restart nginx by running sudo service nginx restart

Generate a certificate

  • Run certbot-auto certonly -a webroot --webroot-path=PATH_OF_APP_ROOT -d -d
        #Note: The certbot-auto software requires superuser privileges, so you will be required to enter your password if you haven't used sudo recently.
  • Enter email when prompt for email.
  • Select agreement when it ask.
  • After obtaining the cert, you will have the following PEM-encoded files.

Generate Strong Diffie-Hellman Group

  • Run sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
  • This may take a few minutes but when it’s done you will have a strong DH group at /etc/ssl/certs/dhparam.pem.

Configure SSL certificate on nginx

  • Run sudo nano /etc/nginx/sites-available/(default or your config file)

  • Make some changes like:

        server {
                              listen 443 ssl;
                              listen ssl;
                              # SSL configuration
                              ssl on;
                              ssl_certificate /etc/letsencrypt/live/;
                              ssl_certificate_key /etc/letsencrypt/live/;
                              ssl_prefer_server_ciphers On;
                              ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                              # this below line is required to produce A+ certificate
                              ssl_dhparam /etc/ssl/certs/dhparam.pem;
                              location ~ /.well-known {
                                              allow all;
                              # Applications root
                              root YOUR_APP_ROOT;
                              location ^~ /assets/ {
                              gzip_static on;
                              expires max;
                              add_header Cache-Control public;
                              server {
                              listen 80;
                              rewrite ^/(.*) https://$host$request_uri permanent;

  • Run sudo service nginx restart to restart nginx.

Verify SSL certificate

  • Open in your browser to verify rating of certificate.


Let’s Encrypt has some limitations like:

  • It’s not work for subdomains.
  • It’s only for 90 days. If you want to secure you site for long time then you have to setup auto-renewal for your site. You can easily configure `Let’s Encrypt auto-renewal.

Setup auto renewal

  • Enable crontab sudo crontab -e
  • 30 2 * * 1 /usr/bin/letsencrypt renew >> /var/log/le-renew.log
  • 35 2 * * 1 /bin/systemctl reload nginx

For more detailed explanation, visit the Link or Link{:target => ‘_blank’}

Ram Laxman Yadav
Ram Laxman Yadav
Ruby on Rails Developer