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.
your_domain
pointing to your server’s public IP address.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.
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:
- sudo snap install --classic certbot
Your output will display the current version of Certbot and successful installation:
Output
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:
- 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.
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:
- 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:
- 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:
- 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.
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:
- sudo ufw status
You should receive output like this, indicating that only HTTP traffic is allowed to the web server:
OutputStatus: 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:
- sudo ufw allow 'Nginx Full'
- sudo ufw delete allow 'Nginx HTTP'
Now when you run the ufw status
command it will reflect these new rules:
- sudo ufw status
OutputStatus: 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.
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:
- 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:
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.
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
:
- 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.
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.
http://schalk.net pointed to my Nginx reverse proxy server that interfaced with my WebSockets server at ws://localhost:3055 inside my droplet. It ran smoothly in Firefox and Chrome, but I wanted more security; so I followed your excellent tutorial.
I was thrilled to see https://schalk.net running smoothly in my Chrome browser, but when I tried to load it in Firefox I got nothing. I pressed F12 and looked at the console. The first screenshot (below) shows an astonishing message that left me momentarily flabbergasted. By encrypting my site, I made it such a tremendous security risk - according to Firefox - that it was too dangerous to load in the browser.
I clicked the message and it took me to line 24 of my sources.js file, a file that Firefox apparently ignored on grounds that everything in the file must be a security risk. Line 24 read, " var socket = new WebSocket(‘ws://localhost:3055/’);". If I change that to “var socket = new WebSocket(‘wws://localhost:3055/’);” Firefox sees no security risk but WebSocket messaging becomes impossible since neither my Haskell backend server nor my Cycle.js front end is set up for WebSocket Secure messaging. I let the Nginx reverse proxy server take care of encryption.
My Bugzilla Mozilla report is at https://bugzilla.mozilla.org/show_bug.cgi?id=1468944. This bug is in versions 62 nightly builds, 61, 60 and I don’t know how far before that. I’m not holding my breath waiting for Firefox to do something, and it won’t help me with web surfers using pre-62 versions.
So, can you suggest any workarounds short of encrypting traffic between my Haskell server and Cycle.js front end? I took a stab at doing that and got frustrated. That’s why I was so overjoyed by your wonderful tutorial - so clear, so thorough, with magic results. Alternatively, can you steer me toward information about using Letsencrypt to secure my WebSocket server and Cycle.js front end?
Any help that you or the community can give me will be much appreciated. Thanks.
Wow! The procedure you presented was easy to follow and worked like a charm. Very impressive.
I do have a problem, though. Firefox, which loaded my page before I secured the Nginx reverse proxy server, now refuses to load it because of security concerns. Firefox found the file where I define my unencrypted WebSocket server running on localhost and concluded that it is a security threat. This is absurd, of course, but it might be a widespread problem. Every website with a non-secured WebSocket server secured by an encrypted reverse proxy server might be unavailable to people using Firefox browsers. The problem exists in Firefox version 62-daily-builds, 61, and 60. I don’t know about earlier versions.
Do you have any suggestions for a workaround? My application uses a Haskell server behind a Cycle.js front end bundled into the executable file by a Haskell compiler and uploaded to my droplet where it runs on localhost. The thought of transforming it into a WebSocket Secure application is daunting. I shouldn’t have to do that. My secure site loads and functions perfectly in Chrome.
I wrote a comment with specific details, including the URL for my site, but it was deleted by some amateurish spam detection system. Instead of flagging my comment for review, the system deleted it and gave me an email address where I could register an appeal. Treating customers with such disrespect is bad for business. I like Digitalocean and hate to see it messing up like that.
My comment has three screenshots that I dare not attach now lest I upset the automatic comment eraser. Maybe you can retrieve my detailed comment from wherever it went.
Any help from you or the community will be much appreciated. Thanks.
Make sure you get A Record for both versions of your site www.example.com and example.com!
I’m replying to bboucheron. Pressing “Re;ply” didn’t do anything.
I changed proxy_pass http://localhost.:3055; to proxy_pass http://schalk.net:3055; “socket” is defined as:
MDN’s “Writing WebSocket client applications” states: “In the above examples ws has replaced http, similarly wss replaces https. Establishing a WebSocket relies on the HTTP Upgrade mechanism, so the request for the protocol upgrade is implicit when we address the HTTP server as ws://www.example.com or wss://www.example.com.” – https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications". In context, it is clear that they mean “ws” suffices even for WebSocket Secure sites.
https://schalk.net functions perfectly in Chrome. Firefox has a new concern. In the Webpack bundle;js file, String.prototype.split is defined as native JavaScript String.prototype.split unless Regex is used in calling it. In that case, it appears that MIT licensed “Cross Browser Split 1.1.1” by Steven Levithan is used. Well, you guessed it, my application doesn’t finish loading in Firefox and the console explains “SecurityError: The operation is insecure”, as shown in the screenshot I will attempt to upload. Clicking the error line causes page 1818 of Bundle.js to be displayed. This is shown in another screenshot and also, in more detail, in the text file that I will try to upload.
I write “try to upload” because when I first left my two redundant messages they were immediately deleted by the spam robot. Digital Ocean was very good about republishing them, but I doubt that they have had sufficient time to substantially improve their spam software.
I am very grateful to you for helping me. I guess I should tell Webpack that Firefox thinks that altering String.prototype can be an “insecure” operation. I’m feeling a little insecure about developing websites that have to work in Firefox browsers. Do you or anyone else reading this think a hacker could make use of Webpack’s modified version of String.protocol.split?
I edited the bundle.js file produced by Webpack so String.prototype.split is no longer modified. Now loading of my app in Firefox stops when the definition of “socket” is reached. It is ordinary “ws”, not WebSocket Secure. My bundled Haskell server and Cycle.js front-end exchange unencrypted WebSocket messages inside my droplet. Users reach the app with the URL https://schalk.net, the address of my secure reverse-proxy Nginx server.
Chrome runs smoothly, but Firefox won’t finish loading. Here is the definition of “socket”: socket = “MozWebSocket” in window ? new MozWebSocket(‘ws://127.0.0.1:3055/’) : new WebSocket(‘ws://127.0.0.1:3055/’);
I’m not the only one having this problem, as is evident from:
https://www.google.com/search?ei=lysvW6efB6WLjwT4uYfICQ&q=mixed+content+blocking+https+“ws”&oq=mixed+content+blocking+https+“ws”&gs_l=psy-ab.12...21871.23660.0.24677.2.2.0.0.0.0.103.182.1j1.2.0....0...1.1.64.psy-ab..0.0.0....0.DopyQx_CZug
CAUTION
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!
Good news. My site ( https://schalk.net) works flawlessly in Chrome AND FIREFOX. Firefox was complaining about Webpack’s definition of String.prototype.split and secure Nginx reverse proxy server in front of unsecured WebSocket communication between my Haskell WebSockets server and Cycle.js. I thought this is what I originally had in /etc/nginx/sites-enabled/schalk.net but I don’t know what else I changed. I had a Warp server behind Index.html with scripts including the Webpack bundle.js file but that is currently stopped. Anyway, thank you for a great tutorial and here’s /etc/nginx/sites-enabled/schalk.net:
Automatically inserted by Certbot:
I know very little about system administration. l uploaded my WebSockets/Cycle.js executable, put a little Upstart script in /etc/init, and my site went live on the Internet. It was like magic. I went back to writing project code and didn’t give Upstart a second thought.
Trying to run my executable, which is a Haskell-compiled WebSocket server / Cycle.js app combination, was a different story. The magic was gone. While trying to make it go live on the Internet, I wrote some nonsensical comments below. I meant well, but now I think reading my comments would be a waste of time. I will delete them when I figure out how.
Edit suggestion: Add step after adding the repository to
sudo apt update
. I had to run that for Ubuntu to see thepython-certbot-nginx
package