A Python script to automatically renew LetsEncrypt SSL certificates on PythonAnywhere.
If you're using LetsEncrypt for SSL certificates on PythonAnywhere webapps, you probably know that your certificate expires every 90 days. You probably also know that you can't configure an autorenewal since you need to email the PA staff and let them know you have a new certificate to be installed.
The script will check an SSL certificate for the domain of your choice to see if it's going to expire in 29 days or less. If it is, it will automatically run the LetsEncrypt renewal code and then email the PythonAnywhere support staff letting them know you have an updated certificate you'd like them to install.
THIS WILL ONLY WORK IF YOU'VE ALREADY SET UP AND RUN LETSENCRYPT ONCE! If you haven't, follow this to do so.
- You'll need a transactional email service such as Mandrill or Mailgun in order to send emails. I'm using Mandrill.
- Make sure the Python Sender package is installed. If it isn't, use
pip install sender
. - Edit
renew.py
: Set all of the variables in lines 14-36, and copy to your PythonAnywhere home directory. - Edit
renew.sh
: ReplaceYOUR_TLD_HERE
with your webapp's top-level domain and copy to theletsencrypt/
directory. - Edit
check.sh
: ReplaceYOUR_TLD_HERE
with your webapp's top-level domain and copy to theletsencrypt/
directory. (You may need tochmod +x
renew.sh
andcheck.sh
after you've copied them if you're having trouble.) - Make a PythonAnywhere scheduled task pointing to
renew.py
. Daily is fine; it will only renew and send the emails when the certificate is close to expiration.