Skip to content

Commit 15912c3

Browse files
authored
feat: add auto cert renewal docs, config, and hook (#29)
* feat: add auto cert renewal docs, config, and hook * fix: update location handling * feat: add acme location to prod config
1 parent 5afde4e commit 15912c3

7 files changed

Lines changed: 82 additions & 2 deletions

File tree

‎docs/deployment/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Deployment
22

3+
- [Automatic Certificate Renewal](./certificate-renewal.md)
4+
35
## Deploying to production
46
`Deploy to Production` GitHub Action provides a semi-automatic way to deploy new ACC III version, implemented [here](../../.github/workflows/deploy.yml). It connects to the server via SSH and rebuilds the configured containers using `docker compose`. It is triggered on `workflow_dispatch` (i.e., manual trigger).
57

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Certificate Renewal
2+
The following doc describes a way to automatically renew SSL certificates using `certbot` and `systemd`.
3+
4+
## Systemd Configuration
5+
Certbot relies on `certbot.timer` and `certbot.service` services to automatically renew certificates. Verify that both are running on your system:
6+
7+
```bash
8+
$ sudo systemctl status certbot.timer
9+
$ sudo systemctl status certbot.service
10+
11+
# List running certbot timers:
12+
$ sudo systemctl list-timers | grep certbot
13+
```
14+
15+
### Webroot Configuration
16+
So that we don't have to stop the nginx container to free up the port 80 (used by certbot for certificate renewal), we can configure it to use `webroot` authenticator instead.
17+
18+
Use the configuration from [domain.conf](/deployment/certificates/renewal/domain.conf) in `/etc/letsencrypt/renewal/<your-domain>.conf` on your system.
19+
20+
This will use the already running nginx for verification, instead of spinning up a temporary HTTP server in the case of `standalone` authenticator.
21+
22+
## Reloading Nginx
23+
Once the certificate is renewed (deployed) we need to reload Nginx configuration. This can be achieved with the `deploy` certbot hook. Add the [reload-nginx.sh](/deployment/certificates/renewal-hooks/deploy/reload-nginx.sh) script to the `renewal-hooks` directory on your system:
24+
```bash
25+
$ cp deployment/certificates/renewal-hooks/deploy/reload-nginx.sh /etc/letsencrypt/renewal-hooks/deploy
26+
$ chmod +x /etc/letsencrypt/renewal-hooks/deploy
27+
```
28+
29+
> _Note:_ Don't forget to adjust the path to the `docker-compose.yml` file on your system, if it differs.
30+
31+
## Testing
32+
To test if the setup works, you can force the certificate renewal:
33+
34+
```bash
35+
$ sudo certbot renew --cert-name <your-domain> --force-renew
36+
```
37+
38+
If everything worked as expected, you should see the new certificate (e.g. when navigating to the page).
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
NGINX_CONTAINER_NAME=$(docker ps --format "{{.Names}}" | grep nginx)
6+
7+
if [[ -z "$NGINX_CONTAINER_NAME" ]]; then
8+
echo "No nginx container found"
9+
exit 1
10+
fi
11+
12+
if docker exec "$NGINX_CONTAINER_NAME" nginx -s reload 2> /dev/null; then
13+
echo "Successfully reloaded nginx"
14+
else
15+
echo "Unable to reload nginx"
16+
exit 1
17+
fi
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Options used in the renewal process
2+
[renewalparams]
3+
authenticator = webroot
4+
webroot_path = /var/www/certbot

‎src/deployment/docker-compose.prod.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ services:
77
- "443:443"
88
volumes:
99
- /etc/letsencrypt:/etc/letsencrypt
10+
- /var/www/certbot:/var/www/certbot
1011
- /home/ubuntu/AtomicChargeCalculator/src/deployment/nginx/${NGINX_CONFIG_FILE:-nginx.prod.conf}:/etc/nginx/nginx.conf
1112
- /home/ubuntu/AtomicChargeCalculator/src/deployment/nginx/snippets:/etc/nginx/snippets
1213
networks:

‎src/deployment/nginx/nginx.dev.conf‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,17 @@ http {
5959
server {
6060
listen 80;
6161
server_name acc-dev.biodata.ceitec.cz;
62+
63+
##
64+
# Handling of certificate renewal
65+
##
66+
location /.well-known/acme-challenge/ {
67+
root /var/www/certbot;
68+
}
6269

63-
return 301 https://acc-dev.biodata.ceitec.cz$request_uri;
70+
location / {
71+
return 301 https://acc-dev.biodata.ceitec.cz$request_uri;
72+
}
6473
}
6574

6675
server {

‎src/deployment/nginx/nginx.prod.conf‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ http {
6060
listen 80;
6161
server_name acc.biodata.ceitec.cz acc2.ncbr.muni.cz;
6262

63-
return 301 https://acc.biodata.ceitec.cz$request_uri;
63+
##
64+
# Handling of certificate renewal
65+
##
66+
location /.well-known/acme-challenge/ {
67+
root /var/www/certbot;
68+
}
69+
70+
location / {
71+
return 301 https://acc.biodata.ceitec.cz$request_uri;
72+
}
6473
}
6574

6675
server {

0 commit comments

Comments
 (0)