ubuntu - 如何使用 certbot 自动更新 TLS 证书?

标签 ubuntu ssl-certificate certbot manual auto-renewing

我有一个带有 Nginx docker 容器的应用程序,其 TLS 证书是在部署应用程序的主机(使用 Ubuntu 操作系统)中使用以下命令手动生成的:

certbot certonly --manual --manual-public-ip-logging-ok --preferred-challenges dns -d my.app.com
当证书过期时,我必须更新它们。
但我不能使用以下 certbot renew用于此目的的命令,因为它会给出错误:
$ sudo certbot renew

Failed to renew certificate my.app.com with error: The manual plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.')
所以,我现在要做的是再次创建证书(使用之前使用的相同 certbot certonly 命令)而不是更新它们。
如何使用 certbot renew 修复错误命令?
如何自动化此设置?

最佳答案

这是我的设置。它涉及生活在 nginx 和 certbot 之间共享的 docker 卷中的 LE secret ,以及 nginx 将续订请求代理到 certbot,因此您不必在 certbot 进行验证时停止 nginx。
nginx 设置
将 LE 验证代理到 certbot 后端
端口 80 上的 letencrypt 验证请求被转发到 certbot,其他任何东西都被重定向到 https。
(如果您想知道为什么我将代理传递后端定义为变量,请参阅 this SO answer )

  server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;

    location /.well-known/acme-challenge {
      resolver 127.0.0.11 valid=30s;
      set $upstream letsencrypt;
      proxy_pass http://$upstream:80;
      proxy_set_header Host            $host;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header X-Forwarded-Proto https;
    }

    location / {
      return 301 https://$host$request_uri;
    }
  }
SSL 设置
这里有很多标准的东西:
  server {
    listen 443 ssl;
    server_name ${DOMAINNAME};

    ssl_certificate /etc/letsencrypt/live/${DOMAINNAME}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/${DOMAINNAME}/privkey.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1.2;
    ssl_ciphers 'EECDH+AESGCM: EDH+AESGCM:AES256+EECDH:AES256+EDH';
    ssl_prefer_server_ciphers on;

    ssl_session_cache shared:SSL:10m;
    ssl_dhparam dhparam.pem;

    ... your lcoation block goes here ...

}
docker 撰写魔术
证书机器人
有一个特殊的“docker-compose-LE.yml”来单次运行 certbot:
version: '3.4'

services:

  letsencrypt:
    image: certbot/certbot:latest
    command: sh -c "certbot certonly --standalone -d ${DOMAINNAME} --text --agree-tos --email you@example.com --server https://acme-v02.api.letsencrypt.org/directory --rsa-key-size 4096 --verbose --keep-until-expiring --preferred-challenges=http"
    entrypoint: ""
    volumes:
      - "letsencrypt:/etc/letsencrypt"
    environment:
      - TERM=xterm

volumes:
  letsencrypt:
    name: letsencrypt_keys
通过运行“docker-compose -f docker-compose-LE.yml up”,您将创建并验证证书。您可以使用相同的命令来更新证书,certbot 就是这么聪明。您可以(每天)尽可能频繁地运行此命令,因为它只会在您的证书即将到期时更新您的证书。
在第一次运行此命令之前,请参阅下面的“警告”。
nginx
在 docker-compose.yml 中安装卷中的证书。该卷已由letsencrypt创建,因此将其声明为外部。
services:
  nginx:
    image: nginx:1.18
    restart: always
    volumes:
      - letsencrypt:/etc/letsencrypt:ro

volumes:
  letsencrypt:
    external:
      name: letsencrypt_keys
警告
这种方法在第一次创建证书时会导致鸡蛋问题:没有证书文件,nginx 将无法启动并且无法代理 LE 验证。没有 nginx 意味着没有证书,没有证书意味着没有 nginx。
要解决这个问题,您必须在没有 nginx 的情况下首次调用 certbot 并使用 certbots 暴露的内部 http 服务器。因此,第一次运行 certbot 时,将这些行添加到 docker-compose-LE.yml:
  letsencrypt:
    ports:
      - "80:80"
证书更新
只需在每日 cronjob 中运行这两个命令:
docker-compose -f docker-compose-LE.yml up
将检查证书并在到期后开始续订过程。现在正在运行的 nginx 会将认证验证代理到 certbot。
docker-compose exec nginx nginx -s reload
一旦证书在 docker 卷 certbot 和 nginx 共享中就地更新,只需向 nginx 发送 SIGHUP 即可重新加载证书文件而不会中断服务。

关于ubuntu - 如何使用 certbot 自动更新 TLS 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66638368/

相关文章:

ubuntu - 如何将 certbot 添加到另一个端口

nginx - Http 不重定向到 https nginx

ruby-on-rails - 如何在 ubuntu 上自动运行 rails?

mysql - mysql服务器ubuntu远程连接的端口转发

linux - 针对不同安装类型的 Ubuntu 进行编程

amazon-web-services - 在本地运行 AWS SAM 项目出现错误

php - 使用 Zend_Http_Client 的客户端 ssl 证书

android - Android 4.x 中的 SSL 客户端身份验证

go - 使用 Nginx 添加 SSL 证书后 Hugo 站点 CSS 未加载

docker - 入口 nginx digital ocean 上的 ssl 终止