Certbot是一个开源工具,专门用于自动化 HTTPS 证书的申请、安装和续期,特别是支持与 Let’s Encrypt 证书机构的集成。Let’s Encrypt 提供免费的 SSL/TLS 证书,Certbot 简化了这一过程,使得网站管理员能够方便地申请并管理SSL证书。
1.yum安装certbot
yum -y install certbot python2-certbot-dns-cloudflare python2-certbot
2. 生成证书
certbot certonly --dns-cloudflare --dns-cloudflare-credentials /etc/nginx/cloudflare_credentials.ini -d wxspace.cn -d www.wxspace.cn
- certonly:只申请证书,并不会自动配置 Web 服务器。
- –dns-cloudflare:使用Cloudflare的DNS插件进行DNS-01验证。
- –dns-cloudflare-credentials:指定了存储 Cloudflare API密钥的凭证文件路径。
/etc/nginx/cloudflare_credentials.ini文件需要包含用于访问 Cloudflare API的账户电子邮件地址和API密钥
dns_cloudflare_email = 你的Cloudflare账户邮箱
dns_cloudflare_api_key = 你的Cloudflare API密钥
- -d wxspace.cn:为
wxspace.cn
域名申请证书。 - -d www.wxspace.cn:为
www.wxspace.cn
域名申请证书。
命令执行成功后会在/etc/letsencrypt/live/wxspace.cn/下生成ssl证书。
ls /etc/letsencrypt/live/wxspace.cn/
cert.pem chain.pem fullchain.pem privkey.pem README
3. Nginx配置证书
ssl_certificate /etc/letsencrypt/live/wxspace.cn/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/wxspace.cn/privkey.pem;
这里只提示实际用到的证书是fullchain.pem和privkey.pem。
4. 访问验证
在浏览器输入网址域名进行验证。此时可以看到已经通过HTTPS建立WEB连接。
5. 自动续期SSL证书
添加crontab定时任务
0 3 * * * certbot renew --quiet --deploy-hook "/usr/sbin/nginx -s reload" >> /etc/nginx/logs/renew.log 2>&1
每天凌晨3点运行SSL续期命令。