阿里云域名自动签发泛域名证书配置

鸿辰 Linux 908 0

概述

本文分享内容作者亲自在多台 centos7 机器上实践过,适用于域名服务商是阿里云的用户参考,如果你想要的是单域名证书签发,可以参考 在CentOS 7上配置Certbot以自动签发Nginx的HTTPS证书

1. 安装Certbot (如果已经安装过,可直接参考第2部分)

首先,您需要安装Certbot。由于CentOS 7默认的软件源可能不包含最新版本的Certbot,因此推荐使用Snap或直接从源代码安装。

1.1 安装Snapd

sudo yum install epel-release
sudo yum install snapd
sudo systemctl enable --now snapd.socket

1.2 通过Snap安装Certbot

sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

如果这里报错 error: too early for operation, device not yet seeded or device model not acknowledged ,只需要执行下面的命令

sudo ln -s /var/lib/snapd/snap /snap

安装完成后再次执行命令通过Snap安装Certbot即可。

2. 安装 certbot-dns-aliyun 插件

该插件的开源地址是 https://github.com/tengattack/certbot-dns-aliyun

2.1 使用 snapd 方式安装

因为上方已经安装过 Snapd ,可以直接使用 snapd 方式安装,依次执行下面的命令即可

sudo snap install certbot-dns-aliyun
sudo snap set certbot trust-plugin-with-root=ok
sudo snap connect certbot:plugin certbot-dns-aliyun

2.2 验证是否安装成功

/snap/bin/certbot plugins

如果出现如下图中的内容,则表示安装成功

阿里云域名自动签发泛域名证书配置-第1张图片-鸿辰个人分享站

3. 授权阿里云解析权限

3.1 登录你的阿里云管理平台赋权

3.2 生成配置文件

创建 credentials.ini 文件,并加入下面的内容,注意将信息改成你自己的

dns_aliyun_access_key = 你的accessKey_id
dns_aliyun_access_key_secret = 你的accessKey_secret

设置文件权限 chmod 600 credentials.ini

4. 颁发证书

4.1 使用命令申请颁发证书

sudo certbot certonly \
--authenticator=dns-aliyun \
--dns-aliyun-credentials='/path/to/credentials.ini' \
-d "*.example.com,example.com"
  • --authenticator: 固定值为 dns-aliyun
  • --dns-aliyun-credentials: 在 3.2 步骤中创建的配置文件全路径
  • -d: 需要颁发的域名,支持泛域名,多个域名使用英文逗号分割

后续按提示输入邮箱和同意协议即可完成颁发

4.2 查看已颁发的证书

sudo certbot certificates

5. 生成nginx配置需要用的文件

因为在上方生成时,泛域名容易干扰原有的nginx配置文件,故而在生成时,不指定nginx插件

5.1 生成 options-ssl-nginx.conf 文件

该文件是 ssl 证书的参数配置文件,建议在 /etc/letsencrypt/ 目录下创建 名为 options-ssl-nginx.conf 的文件,并加入以下内容

# This file contains important security parameters. If you modify this file
# manually, Certbot will be unable to automatically provide future security
# updates. Instead, Certbot will print and log an error message with a path to
# the up-to-date file that you will need to refer to when manually updating
# this file. Contents are based on https://ssl-config.mozilla.org

ssl_session_cache shared:le_nginx_SSL:10m;
ssl_session_timeout 1440m;
ssl_session_tickets off;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;

ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";

5.2 生成 Diffie-Hellman 参数文件 (可忽略)

在配置nginx时,有可能需要配置 ssl_dhparam, ssl_dhparam 的值是 Diffie-Hellman 参数文件地址,它在 SSL/TLS 握手过程中使用 Diffie-Hellman 密钥交换算法时起到关键作用。Diffie-Hellman 算法允许服务器和客户端在不直接传输密钥的情况下,共同生成一个共享的秘密密钥,这个密钥随后可以用于加密和解密通信。

执行以下命令生成

openssl dhparam -out /etc/letsencrypt/ssl-dhparams.pem 2048

6. 配置nginx

在你的nginx配置文件中,修改 server 配置下的监听端口、引入证书配置即可,如下

listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;# 这行可以不配置

修改完成后可使用 nginx -t 测试,无误后重启即可生效

7. 续签证书

7.1 检查Certbot的续签配置

sudo certbot renew --dry-run

7.2 手动续签

sudo certbot --force-renewal

7.3 使用计划任务自动续签

如每天的凌晨2点运行,自动续签证书

# 平滑重启(建议)
sudo tee /etc/cron.d/certbot <<-'EOF'
0 2 * * * root certbot renew --quiet --post-hook "systemctl reload nginx"
EOF
# 强制重启
sudo tee /etc/cron.d/certbot <<-'EOF'
0 2  * *  *  root certbot renew --quiet --no-self-upgrade --renew-by-default --standalone --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx
EOF

#推荐阅读

标签: https nginx