Certbot自动化部署:Shell脚本与系统服务配置指南

Certbot自动化部署:Shell脚本与系统服务配置指南

【免费下载链接】certbot Certbot is EFF's tool to obtain certs from Let's Encrypt and (optionally) auto-enable HTTPS on your server. It can also act as a client for any other CA that uses the ACME protocol. 【免费下载链接】certbot 项目地址: https://gitcode.com/gh_mirrors/le/letsencrypt

一、Certbot自动化部署概述

Certbot是EFF开发的免费工具,可自动从Let's Encrypt获取SSL证书并配置HTTPS。本文将详细介绍如何通过Shell脚本实现证书申请与续期的自动化,并配置系统服务确保长期稳定运行。

二、证书申请脚本编写

2.1 生成CSR脚本示例

Certbot提供了生成证书签名请求(CSR)的示例脚本,可用于手动证书申请流程。以下是简化版的实现逻辑:

#!/bin/sh
# 生成多域名SAN证书的CSR文件
if [ "$#" -lt 1 ]; then
  echo "Usage: $0 domain [domain...]" >&2
  exit 1
fi

domains="DNS:$1"
shift
for x in "$@"; do
  domains="$domains,DNS:$x"
done

# 使用OpenSSL生成DER格式的CSR
SAN="$domains" openssl req -config openssl.cnf \
  -new -nodes -subj '/' -reqexts san \
  -out csr.der -keyout key.pem \
  -newkey rsa:2048 -outform DER

完整示例文件:certbot/examples/generate-csr.sh

2.2 自动化申请脚本

结合Certbot命令行参数,可以创建一键式证书申请脚本:

#!/bin/bash
# 证书申请自动化脚本
DOMAINS=("example.com" "www.example.com")
EMAIL="admin@example.com"
WEBROOT_PATH="/var/www/html"

certbot certonly \
  --webroot \
  --webroot-path "$WEBROOT_PATH" \
  --email "$EMAIL" \
  --agree-tos \
  --no-eff-email \
  --domains "${DOMAINS[*]}" \
  --rsa-key-size 4096

三、配置文件优化

3.1 CLI配置文件示例

通过配置文件预设常用参数,可简化Certbot命令复杂度。典型配置文件如下:

# 全局默认配置 [certbot/examples/cli.ini](https://link.gitcode.com/i/e16a356bcddebaf2a8007834d665096b)
# 使用ECC密钥
key-type = ecdsa
elliptic-curve = secp384r1

# RSA密钥长度(如需使用RSA)
rsa-key-size = 4096

# 默认验证方式
authenticator = webroot
webroot-path = /usr/share/nginx/html

# 自动同意条款
agree-tos = true

# 联系邮箱
email = admin@example.com

3.2 配置文件路径说明

  • 全局配置:/etc/letsencrypt/cli.ini
  • 用户配置:~/.config/letsencrypt/cli.ini
  • 自定义路径:--config cli.ini参数指定

四、系统服务配置

4.1 Windows任务计划配置

Windows系统可通过PowerShell脚本配置自动续期任务:

# [windows-installer/assets/renew-up.ps1](https://link.gitcode.com/i/d40dbbaf3250a30b51ede4c09b9d85b7)
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' `
  -Argument '-NoProfile -WindowStyle Hidden -Command "certbot renew"'
$triggerAM = New-ScheduledTaskTrigger -Daily -At 12am -RandomDelay (New-TimeSpan -Hours 12)
$triggerPM = New-ScheduledTaskTrigger -Daily -At 12pm -RandomDelay (New-TimeSpan -Hours 12)
Register-ScheduledTask -Action $action -Trigger $triggerAM,$triggerPM `
  -TaskName "Certbot Renew Task" -Description "自动续期SSL证书"

4.2 Linux系统服务配置

在Linux系统中,可通过systemd服务实现自动续期:

# /etc/systemd/system/certbot-renew.service
[Unit]
Description=Certbot证书自动续期服务

[Service]
Type=oneshot
ExecStart=/usr/bin/certbot renew --quiet --no-self-upgrade
# /etc/systemd/system/certbot-renew.timer
[Unit]
Description=Certbot证书自动续期定时器

[Timer]
OnCalendar=*-*-* 03:00:00
RandomizedDelaySec=3600
Persistent=true

[Install]
WantedBy=timers.target

启用定时器命令:

sudo systemctl enable --now certbot-renew.timer

五、自动化部署最佳实践

5.1 脚本安全加固

  • 限制脚本权限:chmod 700 /path/to/script.sh
  • 使用绝对路径引用命令和文件
  • 添加错误处理机制:
# 错误处理示例
set -euo pipefail

# 检查Certbot是否安装
if ! command -v certbot &> /dev/null; then
  echo "Certbot未安装,请先安装Certbot"
  exit 1
fi

5.2 日志与监控配置

Certbot操作日志默认存储在/var/log/letsencrypt/目录,可配置日志轮转防止磁盘空间耗尽。同时建议添加监控告警,及时发现续期失败问题。

六、常见问题解决

6.1 证书续期失败排查

  1. 检查Web服务器状态及防火墙规则
  2. 验证域名解析是否正确
  3. 查看详细日志:certbot renew --debug-challenges

6.2 多域名证书管理

使用通配符证书或多SAN证书简化管理,示例配置:

certbot certonly --webroot -w /var/www/html \
  -d example.com -d *.example.com \
  --agree-tos --email admin@example.com

七、总结

通过本文介绍的Shell脚本与系统服务配置方法,可实现Certbot的全自动化部署与维护。关键步骤包括:

  1. 使用CSR生成脚本准备证书请求
  2. 配置CLI参数文件简化命令
  3. 设置系统定时任务实现自动续期
  4. 遵循安全最佳实践确保稳定运行

完整的配置示例和更多高级用法可参考项目官方文档:certbot/docs/using.rst

【免费下载链接】certbot Certbot is EFF's tool to obtain certs from Let's Encrypt and (optionally) auto-enable HTTPS on your server. It can also act as a client for any other CA that uses the ACME protocol. 【免费下载链接】certbot 项目地址: https://gitcode.com/gh_mirrors/le/letsencrypt

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值