let's encrypt证书自动维护:certbot + name api + nginx

现状

  • docker
    • nginx 连接证书
    • certbot 维护证书:到期手动维护,手动调用name api修改dns配合验证,手动重载nginx
    • ghost 使用证书
    • trilium 使用证书

目标

  • 目标一:在宿主机完成所有操作,并且简化手动命令
  • 目标二:让 Certbot 自动续期 证书,不需要人工干预。

自动维护-证书配置

  • name配置
    • token配置文件:docker exec -i certbot sh -c "echo '{\"namecom\": {\"username\": \"your_namecom_username\", \"token\": \"your_api_token\"}, \"waitsec\": 15}' > /etc/certbot-dns-namecom.config.json"
    • 配置文件权限:docker exec -it certbot chmod 600 /etc/certbot-dns-namecom.config.json
    • 白名单:将服务器ip加入name api的白名单
  • 维护certbot
    • 查看版本:docker exec -it certbot certbot --version
    • 脚本升级:docker exec -it certbot pip install --upgrade certbot
    • 容器升级:修改docker-compose.yml里的certbot版本号,down容器,再up -d
  • 增加name脚本
    • 升级PIP:docker exec -it certbot /usr/local/bin/python -m pip install --upgrade pip
    • 拉取脚本:docker exec -it certbot sh -c “apk add git && git clone https://github.com/laonan/certbot-dns-name-com /opt/certbot-dns-name-com”
    • 设置脚本:docker exec -it certbot sh -c “cp /opt/certbot-dns-name-com/src/certbot-dns-namecom.py /usr/bin/certbot-dns-namecom.py”
    • 脚本权限:docker exec -it certbot chmod +x /usr/bin/certbot-dns-namecom.py
    • 测试脚本:docker exec -it certbot sh -c “CERTBOT_DOMAIN=ghost.atibm.com CERTBOT_VALIDATION=test_validation /usr/bin/certbot-dns-namecom.py add”
  • 申请证书
    • 验证1个域名:docker exec -it certbot certbot certonly --manual --preferred-challenges dns --manual-auth-hook "/usr/bin/certbot-dns-namecom.py add" --manual-cleanup-hook "/usr/bin/certbot-dns-namecom.py clean" --cert-name ghost.atibm.com -d test.atibm.com --agree-tos --email cheanty@gmail.com --non-interactive --server https://acme-v02.api.letsencrypt.org/directory --dry-run
    • 验证5个域名:docker exec -it certbot certbot certonly --manual --preferred-challenges dns --manual-auth-hook "/usr/bin/certbot-dns-namecom.py add" --manual-cleanup-hook "/usr/bin/certbot-dns-namecom.py clean" --cert-name ghost.atibm.com -d ghost.atibm.com -d atibm.com -d www.atibm.com -d trilium.atibm.com -d test.atibm.com --agree-tos --email cheanty@gmail.com --non-interactive --server https://acme-v02.api.letsencrypt.org/directory --dry-run
    • 正式申请:docker exec -it certbot certbot certonly --manual --preferred-challenges dns --manual-auth-hook "/usr/bin/certbot-dns-namecom.py add" --manual-cleanup-hook "/usr/bin/certbot-dns-namecom.py clean" --cert-name ghost.atibm.com -d ghost.atibm.com -d atibm.com -d www.atibm.com -d trilium.atibm.com -d test.atibm.com --agree-tos --email cheanty@gmail.com --non-interactive --server https://acme-v02.api.letsencrypt.org/directory
  • 查看证书:docker exec -it certbot certbot certificates
  • 证书文件:docker exec -it certbot ls /etc/letsencrypt/live/
  • 证书期限:docker exec -it certbot openssl x509 -in /etc/letsencrypt/live/ghost.atibm.com/fullchain.pem -noout -dates

自动维护-手动更新

  • 手动续期:docker exec -it certbot certbot renew && docker exec nginx nginx -s reload

自动维护-cron定时执行

  • 依赖一:定时任务服务
    • cron任务列表:crontab -l
    • cron任务编辑:crontab -e
    • cron服务状态:systemctl status crond
    • cron服务启动:sudo systemctl enable --now crond
    • cron服务重启:systemctl restart crond
    • 验证:crontab -e 添加* * * * * echo "Cron is working at $(date)" >> /var/log/cron_test.log,几分钟后再cat /var/log/cron_test.log看是否有内容,确认cron服务可用
  • 依赖二:ghost用户的docker权限
    • 检查用户权限:id ghost
    • 增加组权限:sudo usermod -aG docker ghost
    • 确认docker的执行权限:ls -l /var/run/docker.sock 有docker组
  • 测试任务
    • crontab -e 写入* * * * * echo "$(date) - Cron is running" >> /home/ghost/cron_certbot_test.log; docker exec certbot certbot renew >> /home/ghost/cron_certbot_test.log 2>&1; echo "$(date) - Running nginx reload" >> /home/ghost/cron_certbot_test.log; docker exec nginx nginx -s reload >> /home/ghost/cron_certbot_test.log 2>&1
    • cron执行日志:cat /home/ghost/cron_certbot_test.log
  • 生产任务
    • crontab -e 写入0 3 * * 0 echo "$(date) - Cron is running" >> /home/ghost/cron_certbot.log; docker exec certbot certbot renew >> /home/ghost/cron_certbot.log 2>&1; echo "$(date) - Running nginx reload" >> /home/ghost/cron_certbot.log; docker exec nginx nginx -s reload >> /home/ghost/cron_certbot.log 2>&1
    • cron执行日志:cat /home/ghost/cron_certbot.log