安装( letsencrypt)SSl 证书部署

安装 letsencrypt

本次安装对象的系统环境:

[root ~]# cat /etc/os-release
NAME="Alibaba Cloud Linux"
VERSION="3 (OpenAnolis Edition)"
ID="alinux"
ID_LIKE="rhel fedora centos anolis"
VERSION_ID="3"
VARIANT="OpenAnolis Edition"
VARIANT_ID="openanolis"
PLATFORM_ID="platform:al8"
PRETTY_NAME="Alibaba Cloud Linux 3.2104 U11 (OpenAnolis Edition)"
ANSI_COLOR="0;31"
HOME_URL="https://www.aliyun.com/"

首次安装

下面所有命令中的域名换成你自己的实际域名,我这个是生成的通配域名

安装 ACME 客户端

推荐使用 acme.sh(支持自动化续期和阿里云DNS API集成),而非 Certbot:

curl https://get.acme.sh | sh -s email=你的邮箱@example.com
source ~/.bashrc  # 激活环境变量

该工具支持 DNS API 自动化验证,适合通配符证书。

配置阿里云 DNS API 权限

获取阿里云AccessKey

  • 登录阿里云控制台,进入 AccessKey 管理 创建AccessKey(需RAM用户权限)。

  • 设置环境变量:

    export Ali_Key="你的AccessKey ID"
    export Ali_Secret="你的AccessKey Secret"
    

必须通过API密钥授权DNS记录修改。

修改默认 CA

acme.sh 脚本默认 CA 服务器是 ZeroSSL,有时可能会导致获取证书的时候一直出现:Pending,The CA is processing your order,please just wait.

只需要把 CA 服务器改成 Let's Encrypt 即可,虽然更改以后还是有概率出现 pending,但基本 2-3 次即可成功

acme.sh --set-default-ca --server letsencrypt

申请通配符证书

执行签发命令

acme.sh --issue --dns dns_ali -d 你的主域名.com -d *.你的主域名.com
  • 示例:-d example.com -d *.example.com
  • 工具会自动完成DNS TXT记录添加和验证,无需手动操作

证书生成路径

  • 证书文件默认保存在:~/.acme.sh/你的主域名.com/
  • 包含以下关键文件:
    • fullchain.cer(证书链)
    • 你的主域名.com.key(私钥)

Nginx配置示例

server {
    listen 443 ssl;
    server_name 你的主域名.com;
    ssl_certificate /etc/nginx/ssl/fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/你的主域名.com.key;
    # 其他SSL优化配置...
}

需确保文件路径权限正确(建议设为644

自动化续期配置

  1. 自动续期机制

    • acme.sh已自动创建定时任务(位于crontab -l),每天检查证书有效期。
    • 续期后会自动执行 --reloadcmd 命令重启服务。
  2. 验证续期日志

    tail -f /var/log/acme.sh.log
    

    可监控证书更新状态。

生成成功

[Fri Feb 28 05:26:51 PM CST 2025] Your cert is in: /root/.acme.sh/example.com_ecc/example.com.cer
[Fri Feb 28 05:26:51 PM CST 2025] Your cert key is in: /root/.acme.sh/example.com_ecc/example.com.key
[Fri Feb 28 05:26:51 PM CST 2025] The intermediate CA cert is in: /root/.acme.sh/example.com_ecc/ca.cer
[Fri Feb 28 05:26:51 PM CST 2025] And the full-chain cert is in: /root/.acme.sh/example.com_ecc/fullchain.cer

文件说明

  1. example.com.cer
    • 用途:主证书文件,包含你的域名 example.com 的公钥和证书信息(不含中间证书)。
    • 直接使用:不建议单独使用,需搭配中间证书(否则浏览器可能不信任)。
  2. example.com.key
    • 用途:私钥文件,用于配置 HTTPS 服务(如 Nginx/Apache)。
    • 直接使用:直接用于服务器配置,需严格保密。
  3. ca.cer
    • 用途:中间证书(Let's Encrypt 的中间 CA 证书),用于构建完整的证书链。
    • 直接使用:需与主证书合并,但 acme.sh 已为你生成合并后的 fullchain.cer
  4. fullchain.cer
    • 用途:完整的证书链文件(主证书 + 中间证书),可直接用于服务器配置。
    • 直接使用:这是你最终需要使用的证书文件!

优化建议

  1. 证书文件迁移(可选)

    • 建议将证书文件复制到 Nginx 的标准证书目录(如 /usr/local/nginx/cert),避免直接引用 .acme.sh 目录。因为 .acme.sh 目录可能在证书续期时被覆盖或清理。

    • 操作命令:

      mkdir -p /usr/local/nginx/cert
      cp /root/.acme.sh/example.com_ecc/fullchain.cer /usr/local/nginx/cert/
      cp /root/.acme.sh/example.com_ecc/example.com.key /usr/local/nginx/cert/
      
    • 配置改为:

      ssl_certificate cert/fullchain.cer;
      ssl_certificate_key cert/example.com.key;
      

验证与调试

  1. 检查证书有效性

    openssl x509 -in /etc/nginx/ssl/fullchain.cer -noout -text | grep DNS
    
    • 应显示包含 *.你的主域名.com 的SAN扩展。
  2. 在线检测工具

    • 访问 SSL Labs 测试验证配置安全性
  3. 服务器配置示例(如 Nginx)

    ssl_certificate     /root/.acme.sh/example.com_ecc/fullchain.cer;  # 使用完整证书链
    ssl_certificate_key /root/.acme.sh/example.com_ecc/example.com.key; # 使用私钥
    
    • 关键点:必须使用 fullchain.cer(已包含中间证书),避免浏览器提示 SSL证书不受信任
  4. 文件格式验证

    检查 fullchain.cer 是否包含完整的证书链(主证书 + 中间证书):

    openssl x509 -in /root/.acme.sh/example.com_ecc/fullchain.cer -noout -text | grep "Issuer:"
    

    如果输出包含 Let's Encrypt 的中间 CA(如 R3E1),则证书链完整。

手动更新证书

下面所有命令中的域名换成你自己的实际域名,我这个是生成的通配域名

1. 切换默认证书颁发机构为 Let's Encrypt

/root/.acme.sh/acme.sh --set-default-ca --server letsencrypt

2. 重新申请

/root/.acme.sh/acme.sh --issue --dns \
  -d example.com \
  -d "*.example.com" \
  --yes-I-know-dns-manual-mode-enough-go-ahead-please

成功的话会输出如下内容

[Wed Feb 25 13:30:16 CST 2026] Using CA: https://acme-v02.api.letsencrypt.org/directory
[Wed Feb 25 13:30:16 CST 2026] Multi domain='DNS:example.com,DNS:*.example.com'
[Wed Feb 25 13:30:16 CST 2026] Getting domain auth token for each domain
[Wed Feb 25 13:30:21 CST 2026] Getting webroot for domain='example.com'
[Wed Feb 25 13:30:21 CST 2026] Getting webroot for domain='*.example.com'
[Wed Feb 25 13:30:21 CST 2026] Add the following TXT record:
[Wed Feb 25 13:30:21 CST 2026] Domain: '_acme-challenge.example.com'
[Wed Feb 25 13:30:21 CST 2026] TXT value: 'xxxx-xxxx-xx'
[Wed Feb 25 13:30:21 CST 2026] Please be aware that you prepend _acme-challenge. before your domain
[Wed Feb 25 13:30:21 CST 2026] so the resulting subdomain will be: _acme-challenge.example.com
[Wed Feb 25 13:30:21 CST 2026] Add the following TXT record:
[Wed Feb 25 13:30:21 CST 2026] Domain: '_acme-challenge.example.com'
[Wed Feb 25 13:30:21 CST 2026] TXT value: 'xxxx_xxxx'
[Wed Feb 25 13:30:21 CST 2026] Please be aware that you prepend _acme-challenge. before your domain
[Wed Feb 25 13:30:21 CST 2026] so the resulting subdomain will be: _acme-challenge.example.com
[Wed Feb 25 13:30:21 CST 2026] Please add the TXT records to the domains, and re-run with --renew.
[Wed Feb 25 13:30:21 CST 2026] Please add '--debug' or '--log' to check more details.
[Wed Feb 25 13:30:21 CST 2026] See: https://github.com/acmesh-official/acme.sh/wiki/How-to-debug-acme.sh

把第二步生成的DomainTXT value添加到域名DNS解析配置里面,记录类型为TXT

3. 重新签发证书

/root/.acme.sh/acme.sh --renew --dns \
  -d example.com \
  -d "*.example.com" \
  --yes-I-know-dns-manual-mode-enough-go-ahead-please

成功的话会输出如下内容,最后面有新的证书的路径

[Wed Feb 25 13:33:47 CST 2026] Renew: 'example.com'
[Wed Feb 25 13:33:47 CST 2026] Renew to Le_API=https://acme-v02.api.letsencrypt.org/directory
[Wed Feb 25 13:33:48 CST 2026] Using CA: https://acme-v02.api.letsencrypt.org/directory
[Wed Feb 25 13:33:48 CST 2026] Multi domain='DNS:example.com,DNS:*.example.com'
[Wed Feb 25 13:33:48 CST 2026] Getting domain auth token for each domain
[Wed Feb 25 13:33:49 CST 2026] Verifying: example.com
[Wed Feb 25 13:33:53 CST 2026] Pending, The CA is processing your order, please just wait. (1/30)
[Wed Feb 25 13:33:58 CST 2026] Success
[Wed Feb 25 13:33:58 CST 2026] Verifying: *.example.com
[Wed Feb 25 13:33:59 CST 2026] Pending, The CA is processing your order, please just wait. (1/30)
[Wed Feb 25 13:34:05 CST 2026] Success
[Wed Feb 25 13:34:05 CST 2026] Verify finished, start to sign.
[Wed Feb 25 13:34:05 CST 2026] Lets finalize the order.
[Wed Feb 25 13:34:05 CST 2026] Le_OrderFinalize='https://acme-v02.api.letsencrypt.org/acme/finalize/3094851516/484146689456'
[Wed Feb 25 13:34:10 CST 2026] Downloading cert.
[Wed Feb 25 13:34:10 CST 2026] Le_LinkCert='https://acme-v02.api.letsencrypt.org/acme/cert/0523cbd70b61064ab4f7b7274210c1e240f4'
[Wed Feb 25 13:34:12 CST 2026] Cert success.
-----BEGIN CERTIFICATE-----
MIIE+TCCA+GgAwIBAgISBSPL1wthBkq097cnQhDB4kD0MA0GCSqGSIb3DQEBCwUA
......
4ihTML4QQb1QZ3JSomQ+r1vE9A3IhDE75avl4u4=
-----END CERTIFICATE-----
[Wed Feb 25 13:34:12 CST 2026] Your cert is in: /root/.acme.sh/example.com/example.com.cer
[Wed Feb 25 13:34:12 CST 2026] Your cert key is in: /root/.acme.sh/example.com/example.com.key
[Wed Feb 25 13:34:12 CST 2026] The intermediate CA cert is in: /root/.acme.sh/example.com/ca.cer
[Wed Feb 25 13:34:12 CST 2026] And the full chain certs is there: /root/.acme.sh/example.com/fullchain.cer

4. 更换证书

最后别忘了将老的证书替换为新的

posted @ 2026-02-25 14:04  热血文码士  阅读(83)  评论(0)    收藏  举报