Easiest way to get HTTPS SSL on nginx with Certbot

Mike Sun
2 min readJan 25, 2021

--

Photo by Markus Winkler on Unsplash

I’m writing this article because I just spent 4 hours on the web trying to make my website work. There were bits and pieces of information everywhere but none seemed to hit the right spot with my stack. Now that it’s working, I would like to share what I found out along the way so that you can save the 4 hours.

Before I begin, let me explain my set up:

AWS ec2 with security group as such

HTTP |TCP|80|0.0.0.0/0

HTTP |TCP|80|::/0

HTTPs |TCP|443|0.0.0.0/0

HTTPS |TCP|443|::/0

I then set up my google DNS, such that the A records points to the AWS server public ipv4 address

note: @ just means it is example.com without any prefix

I am also using Django with basic html rendering

Alright, with those out of the way, here is what you should do.

  1. ssh into your remote server and go into your settings.py for your core project. In there, put in your allowed host for both example.com and www.example.com
  2. I assume you already have nginx set up, if not, please follow these sub steps

2.1 Run command

sudo nano /etc/nginx/sites-available/your_project

server { 
listen 80;
listen [::]:80;
server_name savourapp.co www.savourapp.co;
location = /favicon.ico { access_log off; log_not_found off; }
location / { include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}

2.2 run command

sudo ln -s /etc/nginx/sites-available/your_project /etc/nginx/sites-enabled

3. Get all the required stuff from certbot

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

4. run command

sudo certbot — nginx -d example.com -d www.example.com

Follow the instructions and then select redirect all traffic to https, which is option 2 for one of the choices

And you are done!! It’s so simple, with these instructions, you would probably only need 10mins, instead of 4 hours of pain…

Bonus, auto renewal: credits to medium.com/@jgefroh

Command:

sudo crontab -e

17 7 * * * certbot renew — post-hook “systemctl reload nginx”

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Mike Sun
Mike Sun

Written by Mike Sun

Random tech blog for my fellow peers troubleshooting stuff. Things I wished I knew without needing to spend hours/days digging...

No responses yet

Write a response