Is this correct way to set cron for renewal of Let's Encrypt cert in Apache2 ? I use Ubuntu 16.04.
@monthly letsencrypt renew && service apache2 reload
Is this correct way to set cron for renewal of Let's Encrypt cert in Apache2 ? I use Ubuntu 16.04.
@monthly letsencrypt renew && service apache2 reload
Monthly is not frequent enough.
This script should run at least weekly, and preferably daily. Remember that certs don't get renewed unless they are near to expiration, and monthly could cause your existing certs to occasionally be expired already before they get renewed.
The name of the program is
certbot
, which was renamed fromletsencrypt
. If you are still usingletsencrypt
, you need to update to the current version.Aside from those issues, it's about the same as my cron jobs.
Note: in 18.04 LTS the
letsencrypt
package has been (finally) renamed tocertbot
. It now includes asystemd
timer which you can enable to schedulecertbot
renewals, withsystemctl enable certbot.timer
andsystemctl start certbot.timer
. However, Ubuntu did not provide a way to specify hooks. You'll need to set up an override forcertbot.service
to overrideExecStart=
with your desired command line, until Canonical fixes this.I recently (October 2017) installed and ran certbot on an Ubuntu 16.04 server and a renewal cron job was created automatically in
/etc/cron.d/certbot
.Here's the cron job that was created:
It would be a good idea to check, if this file already exists before creating a crontab entry.
The certbot documentation recommends running the script twice a day:
As Michael Hampton mentions the name has changed to certbot, but they still provide the -auto option that keeps itself updated. The
certbot-auto
command need root priviledges to run, so the line in your cron script should look something like this:In my own case the
certbot-auto
script is placed in the git-user's home directory. The exact command is thenNote that the example in the documentation corresponds to a relative path, as indicated by the dot which can be confusing:
./path/to/certbot-auto renew --quiet
Be sure to testrun the renew command in a shell beforehand to test the path, if the certificate isn't due for renewal nothing will happen (run this test without the
--quiet
flag to see what is happening).It is not strictly necessary to reload the server when the certificate is renewed in this way, since the path to the live certificate doesn't change if set up correctly.
This is true if you are running apache - for nginx, consider adding a renew hook, such as:
In a docker environment (edit 2020-09-18)
While the above is still true to the best of my knowledge, if your application is running in a docker environment you can let this proxy network take care of all your certificates - both locally and in a live environment. I'm not affiliated with the project, but I've been using it happily for a few years now and haven't touched cron (for this task) or certbot-scripts since.
It has the added benefit of forcing traffic through port 443 automatically (if you enable it) so you don't have to fiddle with apache or nginx configuration - the container serving the web application just need to serve port 80 and the proxy takes care of the rest.
You shouldn't have to set up anything. Any recent Debian/Ubuntu install of certbot should install a systemd timer and a cron job (and the cron job will only run
certbot
if systemd is not active, so you don't get both running).systemd timer
You can check your systemd timers using command
systemctl list-timers
(orsystemctl list-timers --all
if you also want to show inactive timers). Something like this:The certbot timer should be here
/lib/systemd/system/certbot.timer
and it will execute the command specified in/lib/systemd/system/certbot.service
certbot.timer
will execute the `certbot.service at 12 am and 12 pm, after a random delay of up to 12 hours (43200 seconds).and
certbot.service
will execute the renew command.cron job
As others have mentioned, there is also a cron job installed in
/etc/cron.d/certbot
:This is doing:
test -x /usr/bin/certbot -a \! -d /run/systemd/system
- check if/usr/bin/certbot
is an executable file and that/run/systemd/system
is not a directory. Only continue to the next bit if this check succeeds.perl -e 'sleep int(rand(43200))'
- sleep a random amount between 0 seconds and 12 hours (43200 = 12 x 60 x 60).certbot -q renew
check your certificates and renew any if required. The-q
flag is "quiet" - don't produce any output unless there is an error.I was originally confused by the cron job as it wasn't going to run due to systemd, so how would certbot be run? I found the answer in this forum post which is what I based this answer on.
Other members already provided lot more detailed answers. But looks like I should mention it here.
As of certbot version 0.21.1
--renew-hook
flag is changed to--deploy-hook
Make sure you're not using deprecated flag.For LetsEncrypt certificate renewal, I generally use getssl. It is a very handy shell wrapper which can even install certificate on other machines via SSH connection.
The cron entry is the following:
01 23 * * * root /root/scripts/getssl/getssl -u -a -q >>/var/log/getssl.log 2>&1 ; /usr/sbin/apache2ctl graceful
As already suggested, you should run it daily or, even better, twice a day.
As already mentioned by glaux:
Source: https://certbot.eff.org/all-instructions/#debian-8-jessie-apache
So i ended up using this (running is twice a day, at 01:00 and at 13:00 everyday):
or even better:
I didn't test but this should work also:
Source: https://certbot.eff.org/docs/using.html
Since the preferred install method of certbot was modified to
snap
, the path to the timer is now/etc/systemd/system/snap.certbot.renew.timer
.You may also check the configuration via running
systemctl show certbot.timer
This is what I use:
gives output as:
And its saying that apache is restarted already, so no need to do it over again. If I run it again:
therefore it's not problem to renew certificate daily, my cron is then:
I use script to tweak logging to separate file, so here is my cronautorenew.sh:
If your Certbot certificate was obtained using the
standalone
plugin then you might need to stop your web server in order for the renewal to succeed. This can be done in cron using the--pre-hook
and--post-hook
operators: Certbot documentation.