自动申请通配符 wildcard 域名 HTTPS 免费证书,并自动续期
本文内容测试环境为 Ubuntu 24.04
使用通配符 wildcard 域名 HTTPS/TLS/SSL 证书的好处是:
- 一个证书通用于主域名如
zzyyx.com
和所有的*.zzyyx.com
子域名 - 任何时候你添加一个子域名,都不用再次手动申请证书
要注意的是,通过类似下面的手动模式和 DNS 验证来申请通配符 HTTPS/TLS/SSL 证书,后续续期也需要手动完成:
sudo certbot --nginx -d "*.zzyyx" -d zzyyx.name --register-unsafely-without-email --manual --preferred-challenges dns
手动续期自然是比较麻烦的,本文讲一种自动申请 HTTPS/TLS/SSL 通配符免费证书并自动续期的方法
注册、申请 Digital Ocean 的 DNS API token
当然,你的域名要用 Digital Ocean 的控制面板进行解析
把得到的 DNS token 保存在 VPS 上,如 path/to/do.conf
,内容类似下面:
dns_digitalocean_token=6666666666666666666555555555555555555444444444444433333333322222
运行命令申请通配符 https 免费加密证书
sudo apt install certbot python3-certbot-nginx python3-certbot-dns-digitalocean
sudo certbot --dns-digitalocean --dns-digitalocean-credentials path/to/do.conf --dns-digitalocean-propagation-seconds 120 --installer nginx -d *.zzyyx.com -d zzyyx.com --agree-tos --register-unsafely-without-email
有几点需要注意:
- 一个命令申请一个域名的 wildcard https 加密证书
- 如果让 certbot 安装证书到 Nginx 配置文件,会让你选择要安装的域名,要正确选择
如果不需要 certbot 安装证书,就移除命令中的 -i
(install) 选项并加上 certonly
子命令:
sudo certbot certonly --dns-digitalocean --dns-digitalocean-credentials path/to/do.conf --dns-digitalocean-propagation-seconds 120 -d *.zzyyx.com -d zzyyx.com --agree-tos --register-unsafely-without-email
子命令 certonly
表示 Obtain or renew a certificate, but do not install it
手动安装 HTTPS 证书到 Niginx 域名配置文件的示例
实际上,我们第一次让 certbot 帮我们安装证书到 Nginx 网站配置文件里,以后就可以按照范例自己修改配置文件,特别是域名较多的情况下这样更加方便
server {
server_name zzyyx.com;
listen [::]:443 ssl http2; # managed by Certbot
listen 443 ssl http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/zzyyx.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/zzyyx.com/privkey.pem; # managed by Certbot
#ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host ~ ^[^.]+\.software-download\.name$) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name zzyyx.com;
}
上面带 # managed by Certbot
行就是 certbot 安装的
如果你有了 HTTPS 证书,希望 certbot 帮你代劳安装,那么可以运行下面命令:
certbot install --cert-name zzyyx.com
按照本文方法成功申请 HTTPS/TLS/SSL 加密证书的域名,以后 certbot 会自动帮我们续期
2024-12-21