SSL/TLS Part 2 - Let’s Encrypt

Search for a command to run...

No comments yet. Be the first to comment.
In this series, I'll talk about what SSL/TLS certificates are and why they're needed (using an analogy you'll definitely relate to), and I'll also help you set up Let's Encrypt on your own web server
Let's use a story to help explain this more intuitively
Let's use a story to help explain this more intuitively

How to get the 🔒 locally

How to get exactly what you need

In a project I'm working on, part of our decision-making logic uses data from a PLC (Programmable Logic Controller). Without getting very technical, the PLC communication interface we use (Node OPCUA) asks the PLC to monitor certain tags, i.e., we ge...
Let's Encrypt is a free, automated, and open certificate authority - if you want to learn more, you can visit letsencrypt.org, but in this blog post I'm going to break down how to actually issue the certificate.
Here's the steps to issue an SSL certificate on your server using Let's Encrypt
A recordYOUR_SUBDOMAINAYOUR_SERVER_IP_ADDRESSThis ensures that when a user visits YOUR_SUBDOMAIN.YOUR_DOMAIN.COM, the IP address resolves to YOUR_SERVER_IP_ADDRESS. This helps you prove to Let's Encrypt that you own the domain and the server that you're requesting the certificate on.
According to their website,
Certbot is a free, open source software tool for automatically using Let’s Encrypt certificates on manually-administrated websites to enable HTTPS.
The following command installs Certbot:
apt install certbot
We can now use this utility to install a Let's Encrypt certificate on our machine.
# if no webserver is currently running on your server, run the following command
certbot certonly --standalone -d YOUR_DOMAIN
# else, run this command
certbot certonly --webroot -d YOUR_DOMAIN
The former command starts up a temporary webserver on port 80 in order to verify ownership of the domain / subdomain, so if you already have a webserver running, use the latter command.
Once you run the commands, Certbot will generate an SSL certificate and an SSL certificate private key, providing links for the same which you can use to configure HTTPS on your website.
const privKey = fs.readFileSync('/etc/letsencrypt/live/cryptic-one.nandanv.com/privkey.pem', 'utf-8')
const certificate = fs.readFileSync('/etc/letsencrypt/live/cryptic-one.nandanv.com/fullchain.pem', 'utf-8')
const credentials = { key: privKey, cert: certificate }
const httpsServer = https.createServer(credentials, app)
httpsServer.listen(port, () => console.log(`server active on port ${port}`))
It may be necessary to restart your server once the certificates have been generated. If you're running Ubuntu, you can use the following command
reboot now
If everything worked out just fine, you should see a 🔒 when you visit your domain.
Congrats 🍻 🎆
Conventionally, we run HTTP on port 80 and HTTPS on port 443. The app I set up is quite simple and listens on port 80 and port 443, forwarding all port 80 requests (http) to port 443 (https). Here's the code that makes that happen
const httpApp = express()
.all('*', (req, res) => res.redirect(300, `https://cryptic-one.nandanv.com}`))
const httpServer = http.createServer(httpApp)
httpServer.listen(80, () => console.log('listening for http'))
If you want to learn more about SSL/TLS and why it's used, check out my previous post!