Skip to content

Cron job to auto renew Letsencrypt SSL certificate using Lego in Lightsail

0

I have a Lightsail instance running Wordpress and need help setting up a cron job that will auto renew my Letsencrypt SSL certificate every 90 days. I use lego to install and manage the certificate. I found some instruction for using certbot but nothing for lego. Please advise.

asked 2 years ago659 views

4 Answers
0

To set up a cron job for automatically renewing your Let's Encrypt SSL certificate using Lego on your Lightsail WordPress instance, you can follow these steps:

  1. Open the crontab editor for the bitnami user by running the following command:

    sudo crontab -e -u bitnami

  2. In the crontab file, add the following line:

    0 0 * * * sudo /opt/bitnami/letsencrypt/lego --path /opt/bitnami/letsencrypt --email="YOUR-EMAIL-ADDRESS" --domains="YOUR-DOMAIN" --domains="*.YOUR-DOMAIN" --dns YOUR-DNS-PROVIDER renew >> /var/log/letsencrypt.log 2>&1 && sudo /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -k graceful

    Make sure to replace "YOUR-EMAIL-ADDRESS", "YOUR-DOMAIN", and "YOUR-DNS-PROVIDER" with your actual values.

  3. Save and exit the crontab editor.

This cron job will run daily at midnight (0 0 * * *) and attempt to renew the certificate. The Lego tool will automatically check if renewal is necessary (certificates are valid for 90 days and can be renewed 30 days before expiry). If a renewal is performed, it will gracefully restart the Apache web server to apply the new certificate.

The command logs its output to /var/log/letsencrypt.log, which you can check for any issues or confirmation of successful renewals.

Remember to ensure that your DNS provider is supported by Lego and that you have the necessary credentials set up for DNS challenge authentication.

After setting up the cron job, it's a good practice to also configure HTTPS redirection for your WordPress site to ensure all traffic uses the secure connection.
Sources
Install a wildcard SSL certificate in Lightsail Bitnami | AWS re:Post

answered 2 years ago

EXPERT

reviewed 2 years ago

  • I'm wondering if it's wise to run this every day https://letsencrypt.org/docs/rate-limits/#new-certificates-per-exact-set-of-hostnames

    Up to 5 certificates can be issued per exact same set of hostnames every 7 days. This is a global limit, and all new order requests, regardless of which account submits them, count towards this limit. The ability to request new certificates for the same exact set of hostnames refills at a rate of 1 certificate every 34 hours.

    I know that certbot will only renew certs that are close to expiry (unless you use the --force flag) but I'm not sure about lego. You may want to err on the side of caution and run it weekly instead of daily.

0

You can refer to Bitnami documentation on using Lego client, i.e. Generate and Install a Let's Encrypt SSL Certificate for a Bitnami Application, Step 5: Renew the Let’s Encrypt certificate. It provides instructions for cron script creation.

AWS
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

0

So I ended up creating a shell script (see below) and this is my crontab entry:

10 01 1 */3 * /opt/bitnami/scripts/letsencryptlego.sh
/opt/bitnami/ctlscript.sh stop
sleep 60
/opt/bitnami/letsencrypt/lego --tls --email="YOUR EMAIL ADDRESS" --domains="YOUR DOMAIN(S)" --path="/opt/bitnami/letsencrypt" renew --days 90
sleep 300
sudo /opt/bitnami/ctlscript.sh start

I'll try to remember to follow up and let you guys know if it worked in 90 days.

answered 2 years ago

0

Hi there,

For the simplest and most reliable way to manage SSL certificates on your Lightsail WordPress instance, I strongly recommend using Lightsail's built-in SSL setup: https://docs.aws.amazon.com/lightsail/latest/userguide/amazon-lightsail-quick-start-guide-wordpress.html#quick-start-guide-set-up-wordpress-instance-website

This official method automatically handles certificate renewal and is fully supported by AWS, making it much more maintainable than custom solutions like Lego.

If you need more control over your SSL configuration, Bitnami also provides comprehensive documentation for managing Let's Encrypt certificates: https://docs.bitnami.com/aws/how-to/generate-install-lets-encrypt-ssl/

Let me know if you need help with setting up either of these approaches!

AWS

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.