Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Cronjob to renew Let’s Encrypt Certificate

In this post I have described how to manually renew Let’s Encrypt SSL certificate. Here I describe how to configure a cronjob to automatically renew my certificates when they are about to expire.

Setup default AMI editor to nano:

export EDITOR=nano </pre>  <pre class="lang:sh decode:true " > crontab -e

Add the following:

17 */12 * * * mv /var/www/html/.htaccess /var/www/html/.htaccess_ && /usr/local/letsencrypt/letsencrypt-auto certonly --webroot -w /var/www/html -d lucaslouca.com -d www.lucaslouca.com --config /etc/letsencrypt/config.ini --agree-to --quiet && mv /var/www/html/.htaccess_ /var/www/html/.htaccess && rm -rf /var/www/html/.well-known/

The && operator makes sure the commands are run sequentially.

The --quiet flag is to silence all output except errors. Since certificates are only renewed when they’re determined to be near expiry, the command can run on a regular basis, like every week or every day). That is why you want to use the -q or --quiet quiet flag to silence all output except errors.

This cron job will run twice a day, it will check if your will expire in less than 30 days, if it doesn’ it will do nothing, if it will expire in less than 30 days it will try to issue a new cert using the same paramaters as the first time you issued your cert.

Scheduling Tasks with Cron Jobs