使用acme自动管理部署ssl

具体操作流程

第一步:备份现有配置

备份nginx配置

复制代码
cp /etc/nginx/conf.d/yunche.conf /etc/nginx/conf.d/yunche.conf.backup.$(date+%Y%m%d)

备份现有SSL证书

复制代码
cp -r /data/nginx/ssl /data/nginx/ssl.backup.$(date +%Y%m%d)

第二步:安装acme.sh

复制代码
  curl https://get.acme.sh | sh -s email=你的邮箱@example.com
  source ~/.bashrc

第三步:配置腾讯云DNS API

复制代码
  export Tencent_SecretId="你从腾讯云获取的SecretId"
  export Tencent_SecretKey="你从腾讯云获取的SecretKey"

保存到环境变量

复制代码
  echo 'export Tencent_SecretId="你的SecretId"' >> ~/.bashrc
  echo 'export Tencent_SecretKey="你的SecretKey"' >> ~/.bashrc

第四步:申请Let's Encrypt证书

复制代码
  # 先测试模式
  ~/.acme.sh/acme.sh --issue --test \
    -d wx.fengyunyunche.com \
    --dns dns_tencent

  # 测试成功后申请正式证书
  ~/.acme.sh/acme.sh --issue \
    -d wx.fengyunyunche.com \
    --dns dns_tencent

第五步:部署证书到你的目录

复制代码
  ~/.acme.sh/acme.sh --install-cert -d wx.fengyunyunche.com \
    --key-file /data/nginx/ssl/wx.fengyunyunche.com.key \
    --fullchain-file /data/nginx/ssl/wx.fengyunyunche.com_bundle.crt \
    --reloadcmd "nginx -t && nginx -s reload"

第六步:验证自动续期

复制代码
  # 查看续期任务是否已设置
  crontab -l | grep acme

  # 测试续期(dry run,不会真的续期)
  ~/.acme.sh/acme.sh --cron --home ~/.acme.sh --force

操作要点

  1. 无需修改nginx配置:证书路径保持不变,只是证书内容会自动更新
  2. 不影响现有服务:整个过程只是替换证书文件,不重启服务
  3. 保留原文件结构:继续使用/data/nginx/ssl/目录
  4. 自动续期:90天有效期,60天后自动续期

需要你提供的信息

  1. 腾讯云API密钥:
    • 登录腾讯云控制台
    • 访问管理 → API密钥管理
    • 新建密钥获取SecretId和SecretKey
  2. 邮箱地址:用于Let's Encrypt通知

安全提醒

  • 操作前确保域名的DNS确实在腾讯云解析
  • 每一步操作后都用nginx -t测试配置
  • 如果出错,可以快速恢复备份:
    cp /data/nginx/ssl.backup.$(date +%Y%m%d)/* /data/nginx/ssl/
    nginx -s reload