Let’s Encrypt 颁发的是标准的域名验证型(DV)证书,且不收取任何费用,我们可以使用 ACME 客户端 Certbot 从 Let’s Encrypt 获得证书,为网站启用 HTTPS 连接。
服务器环境:
- 操作系统:
Centos - web server:
Nginx
Webroot 方式
通过在 Web 服务器的
http://<你的域名>/.well-known/acme-challenge/<TOKEN>(用提供的令牌替换<TOKEN>)路径上放置指定文件,来确认对域名的所有权。
- 安装Certbot:
yum install certbot。Docker方式安装,可查看官方文档 - 在对应域名的server{}中增加配置:
server {
...
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /usr/share/nginx/html;
}
...
}
重载Nginx使配置生效:`nginx -s reload`
- 生成证书
certbot certonly --webroot --email username@qq.com -d a.test.com --webroot-path /usr/share/nginx/html --debug- –email 邮件地址用于接收域名到期提醒
- –webroot-path 与server中新增location配置的root路径一致
- 配置证书
server {
...
listen 443 ssl http2;
ssl_certificate "/etc/letsencrypt/live/a.test.com/fullchain.pem"; # 证书链路径
ssl_certificate_key "/etc/letsencrypt/live/a.test.com/privkey.pem"; # 私钥路径
...
}
重载Nginx使配置生效:`nginx -s reload`
- 定时更新证书,执行
crontab -e,新增一条计划任务- 更新所有证书
0 0 1 */2 * certbot renew --force-renewal --webroot --email username@qq.com --webroot-path /usr/share/nginx/html --post-hook "nginx -s reload" --quiet - 更新单个证书
0 0 1 */2 * certbot renew --force-renewal --cert-name a.test.com --webroot --email username@qq.com --webroot-path /usr/local/nginx/html --post-hook "nginx -s reload" --quiet
- 更新所有证书
DNS方式
通过在一条名为_acme-challenge.<你的域名> 的TXT记录中放置特定值来确认对域名的DNS控制权,可用于颁发通配符证书。
手动验证
- 安装Certbot:
yum install certbot - 执行命令
certbot certonly --preferred-challenges dns --manual -d *.test.com --server https://acme-v02.api.letsencrypt.org/directory,提示如下:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Cert is due for renewal, auto-renewing...
Renewing an existing certificate
Performing the following challenges:
dns-01 challenge for test.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.
Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.test.com with the following value:
nI0DhzH-vn0W7STVuLi2O-oIKuFNlqQx5EnjB-zewvs
Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
- 添加TXT记录
登录域名服务商,添加一条name为_acme-challenge.test.com,value为nI0DhzH-vn0W7STVuLi2O-oIKuFNlqQx5EnjB-zewvs的TXT解析记录。
验证DNS解析是否生效的方法:nslookup -qt=txt _acme-challenge.test.comdig -t txt _acme-challenge.test.com
DNS解析生效后,按下Enter键通过验证,申请成功会看到以下信息:
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/test.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/test.com/privkey.pem
Your cert will expire on 2024-01-08. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
- 配置证书
server {
...
listen 443 ssl http2;
ssl_certificate "/etc/letsencrypt/live/test.com/fullchain.pem"; # 证书链路径
ssl_certificate_key "/etc/letsencrypt/live/test.com/privkey.pem"; # 私钥路径
...
}
重载Nginx使配置生效:nginx -s reload
使用
--manual创建的证书不支持自动续订,除非通过--manual-auth-hook与身份验证hook脚本
结合使用以自动设置所需的 HTTP 文件或 DNS TXT记录。
自动验证
上面的方式需要手动到DNS服务商更改解析通过验证,Certbot提供了一些官方的插件实现自动更新DNS,但是没有针对国内 DNS服务商的插件,这里找到一个针对阿里云 DNS、腾讯云 DNS、华为云 NDS、GoDaddy的身份验证hook脚本。以华为云为例:
- 下载
$ git clone https://github.com/ywdblog/certbot-letencrypt-wildcardcertificates-alydns-au
$ cd certbot-letencrypt-wildcardcertificates-alydns-au
$ chmod 0777 au.sh
- 配置
- domain.ini
如果domain.ini文件没有你的根域名,请自行添加。 - 编辑au.sh文件,配置 DNS API 密钥:
HWY_KEY 和 HWY_TOKEN:华为云 API 密钥官方申请文档
- domain.ini
- 申请证书
- 验证是否有错误
certbot certonly -d *.test.cn -m username@qq.com --manual --preferred-challenges dns --manual-auth-hook "./au.sh python hwy add" --manual-cleanup-hook "./au.sh python hwy clean" --dry-run - 申请证书
certbot certonly -d *.test.cn -m username@qq.com --manual --preferred-challenges dns --manual-auth-hook "./au.sh python hwy add" --manual-cleanup-hook "./au.sh python hwy clean"
- 验证是否有错误
- 自动续约
执行crontab -e,新增一条计划任务
0 0 1 */2 * certbot certonly -d *.test.cn -m username@qq.com --manual --preferred-challenges dns --manual-auth-hook "/path/to/au.sh python hwy add" --manual-cleanup-hook "/path/to/au.sh python hwy clean" --post-hook "nginx -s reload" --non-interactive- certonly:获取续订证书,但不安装
- –config-dir:Configuration directory. (default: /etc/letsencrypt)
- 更多参数设置可查看官方文档
3313

被折叠的 条评论
为什么被折叠?



