Automated TLS certificate rotation with certbot #1404
-
Hello, I'm trying to think about automatic tls cert rotation with my nginx-unit server. I have read the instructions at https://unit.nginx.org/howto/certbot/ but I don't understand the "temporary route" for the webroot method. How is it temporary? In particular, how can I update my TLS cert without shutting down the server? Port 80 is already serving my litestar app. In this letsencrypt forums answer you can see how to use certbots I was hoping I could use this hook without having to shut down my server. Is this possible? Is it a bad idea? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Hi there, Thanks for getting in touch! I'm going to tackle different parts of your questions in sections. The temporary routeThe word "temporary" there is sort of a misnomer. The reason it's temporary is because technically we only need this until certbot is happy that it can reach the token file on that route and issues the certificates. After that we could remove the routing to the Certbot uses Let's Encrypt's HTTP-01 challenge here. In order to do this without shutting down Unit:
Using the
|
Beta Was this translation helpful? Give feedback.
-
Just to say it's better to run the job weekly, that way if it randomly fails for some reason, you won't end up with an expired certificate. (That's certainly what I do...) |
Beta Was this translation helpful? Give feedback.
-
@javorszky I was finally able to test your suggestion. I noticed that the Unit docs now have a howto (was it always there?) for Certbot. I followed it and it worked. I want to add the following comments here for those who want to automate Certbot for their NGINX Unit. Instead of using
#!/bin/sh
# Activate the ACME route.
curl -X PUT \
--unix-socket /var/run/control.unit.sock \
--data-binary '"routes/acme"' \
"http://localhost/config/listeners/*:80/pass"
#!/bin/sh
# Name of the certificate bundle without the suffix .pem.
bundle=bundle
bundlepath="/root/${bundle}.pem"
# This directory is created by Certbot the first time you
# run Certbot to produce your first certificate.
dir=/etc/letsencrypt/live/www.example.com
# Create the new bundle.
(umask 177; shred -fu "$bundlepath" 2>/dev/null; touch "$bundlepath")
cat "${dir}/privkey.pem" \
"${dir}/fullchain.pem" \
>> "$bundlepath"
# Upload the new bundle to NGINX Unit with a temporary name.
curl -X PUT \
--unix-socket /var/run/control.unit.sock \
--data-binary "@$bundlepath" \
"http://localhost/certificates/${bundle}_tmp"
# Configure the port 443 listener to use the uploaded bundle
# with its temporary name.
curl -X PUT \
--unix-socket /var/run/control.unit.sock \
--data-binary "\"${bundle}_tmp\"" \
"http://localhost/config/listeners/*:443/tls/certificate"
# Delete the old bundle.
curl -X DELETE \
--unix-socket /var/run/control.unit.sock \
"http://localhost/certificates/$bundle"
# Replace it with the new bundle (upload again).
curl -X PUT \
--unix-socket /var/run/control.unit.sock \
--data-binary "@$bundlepath" \
"http://localhost/certificates/$bundle"
# Configure the port 443 listener to use the new bundle with the original name.
curl -X PUT \
--unix-socket /var/run/control.unit.sock \
--data-binary "\"$bundle\"" \
"http://localhost/config/listeners/*:443/tls/certificate"
# Delete the temporary bundle.
curl -X DELETE \
--unix-socket /var/run/control.unit.sock \
"http://localhost/certificates/${bundle}_tmp"
# Securely remove the bundle file.
shred -fu "$bundlepath"
#!/bin/sh
# Deactivate the ACME route.
curl -X PUT \
--unix-socket /var/run/control.unit.sock \
--data-binary '"routes/static"' \
"http://localhost/config/listeners/*:80/pass"
{
"listeners": {
"*:80": {
"pass": "routes/static"
},
"*:443": {
"pass": "routes/static",
"tls": {
"certificate": "bundle"
}
}
},
"routes": {
"acme": [
{
"match": {
"uri": "/.well-known/acme-challenge/*"
},
"action": {
"share": "/var/www/www.example.com$uri"
}
},
{
"action": {
"pass": "routes/static"
}
}
],
"static": [
{
"action": {
"share": "/home/myuser/www/hello_world.txt"
}
}
]
}
} The first time you run Certbot the Thanks again. |
Beta Was this translation helpful? Give feedback.
Hi there,
Thanks for getting in touch! I'm going to tackle different parts of your questions in sections.
The temporary route
The word "temporary" there is sort of a misnomer. The reason it's temporary is because technically we only need this until certbot is happy that it can reach the token file on that route and issues the certificates. After that we could remove the routing to the
.well-known
path. We don't have to, because it's useful to keep it around when renewal and rollover comes around.Certbot uses Let's Encrypt's HTTP-01 challenge here.
In order to do this without shutting down Unit: