Secure Django & Nginx with Let’s Encrypt on Ubuntu 18.04

KwekuQ
5 min readFeb 28, 2020

--

https://letsencrypt.org/

A while back I wrote a guideline on how to deploy a django app to Ubuntu. (Though it has been a while)As much as that was a while back, that guide is still very relevant, I referenced back those steps recently and they still apply to the T. Let me kick off by saying,

if you are looking for some free Digital Ocean credits click here and sign up. You will instantly get $100 credited to your account.

Lets Encrypt is a nonprofit Certificate Authority providing TLS certificates to 180 million websites. If you wish to contribute please head here.

So the next step would be to secure your app with a free SSL certificate. There are many ways to secure your app, but as I take that everyone is trying to save some bucks this guide will show you how to get some security for free.

Here are the steps we are going to follow:

Step 1 — Installing Certbot

Step 2 — Allowing HTTPS Through the Firewall

Step 3 — Obtaining an SSL Certificate

Step 4 — Verifying Certbot Auto-Renewal

Let the fun stuff begin.

Step 1 — Installing Certbot

We kick off with installing the Certbot software on your server. We first add the repository:

sudo add-apt-repository ppa:certbot/certbot

You will be prompted if you want to continue adding the repository, to which you will press [ENTER]

This is the PPA for packages prepared by Debian Let's Encrypt Team and backported for Ubuntu.
Note: Packages are only provided for currently supported Ubuntu releases.
More info: https://launchpad.net/~certbot/+archive/ubuntu/certbot
Press [ENTER] to continue or Ctrl-c to cancel adding it.

Once this is done, we can then continue the install.

sudo apt install python-certbot-nginx

Step 2 — Allowing HTTPS Through the Firewall

We need to allow for https traffic, so let us see if Nginx has registered it.

sudo ufw status

If the status is inactive, we do not need to worry about this step. However if it is then run the following commands to enable HTTPS traffic. I should point out, it is advisable to block all other ports apart from the ones you want to have traffic through.

sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'

Step 3 — Obtaining an SSL Certificate

If you followed my django ubuntu guideline, then you can follow the instructions as they are. Else you might just need to reference the exact file you created.

Let us view the nginx config file.

sudo nano /etc/nginx/sites-available/prod_project.conf

We will then be presented by the config file we created and we need to grab the server_name (prodproject.net).

server {
listen 80;
server_name prodproject.net;

location = /favicon.ico {
access_log off;
log_not_found off;
}
location ^~ /static {
root /home/prod_user/prod_ject;
}

location / {
include proxy_params;
proxy_pass http://unix:/home/prod_user/prod_env/run/gunicorn.sock;
}
}

Now let us obtain the ssl certificate.

sudo certbot --nginx -d prodproject.net

You will then be prompted to provide an email, then press [ENTER]

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):

Next you will need to accept the terms of service.

Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel:

Next you will need to decide if you want to opt into some digital marketing.

Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:

The next step is the actual certificate generation followed by the option to redirect all traffic or make no further changes to the nginx config file. Idealy I would advise to go with option 2 to redirect all traffic.

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for prodproject.net
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/prod_project.conf
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

A message will now be displayed confirming the certifcate installation etc.

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/prod_project.conf- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled
https://prodproject.net
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=prodproject.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/prodproject.net/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/prodproject.net/privkey.pem
Your cert will expire on 2020-05-27. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- 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

You can further confirm your certificate installation by heading to:

https://www.ssllabs.com/ssltest/analyze.html?d=prodproject.net

Remember to change prodproject.net to your actual domain.

Step 4 — Verifying Certbot Auto-Renewal

Let’s Encrypt’s certificates are only valid for ninety days, this is meant to force users to automate renewal of the certificates. Luckily certbot takes care of this for us. However we still need to verify this.

sudo certbot renew --dry-run

We will get a message confirming the new certificate being renewed.

Saving debug log to /var/log/letsencrypt/letsencrypt.log- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/prodproject.net.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator nginx, Installer nginx
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for demoproject.net
Waiting for verification...
Cleaning up challenges
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of nginx server; fullchain is
/etc/letsencrypt/live/prodproject.net/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/prodproject.net/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.

So now if you head over to your domain, it should hopefully be secured with a Lets Encrypt ssl certificate for free…

I hope you enjoyed this guide and it was helpful to you.

Good luck to you!

--

--

KwekuQ
KwekuQ

Written by KwekuQ

Friend — Developer — Entrepreneur — Ninja

Responses (2)