SSL Certificate Auto-Renewal

DevOps
Published Aug 27, 2023 ยท 2 min read
In this article we will go over a simple procedure to automatically renew the SSL certificate generated by Let's Encrypt

 

In a previous article, I went over the steps to generate an SSL certificate using Let's Encrypt's certbot run in a docker container.

Problem

Let's Encrypt is a non-profit open certificate authority, and it generates certificates valid for 90 days only. So, you're required to renew your certificates every 3 months, which sounds like a lot of manual work to do ;)

Cron Jobs

In Linux systems, a corn job is a task that executes every defined amount of time. It is easy to set up and use. The syntax for it is a bit strange at first, so we're not going to dig deep into it, but you can check out some online references for more details about it. 

To address our problem, we're going to set up a cron job that runs on the 1st and 15th of every month or so at 00:00 AM and 12:00 PM. It sounds like a lot of unnecessary executions, but considering that Let's Encrypt's certbot allows you to renew your certificates only in the margin of 30 days before their expiry dates, we're trying to keep some safety margin here.

In your Linux machine, run the following command to open the list of cron jobs that you have defined:

crontab -e

Add a new line at the end of the file and add the following task:

0 0,12 1,15 */2 * docker run --rm -p 80:80 -p 443:443 -v /root/nginx/letsencrypt:/etc/letsencrypt certbot/certbot certonly -d {your_domain} --standalone -m {your_email} --agree-tos

The first part of the command: (0 0,12 1,15 */2 *) is the cron task timer that we mentioned earlier. You can check out some online cron timer editor to help you understand and write such syntax.

The rest of the command runs the certbot in a docker container to generate the new certificate.

Once you finish with the file, you can save it and close it. Now your cron job will run at the specified times :)