Certbot 免费Let's Encrypt证书配置签发续期工具
获取SSL证书
理论上,我们自己也可以手动制作一个 SSL 安全证书,但是我们自己签发的安全证书浏览器信任,所以我们需要被信任的证书授权中心( CA )签发的安全证书。而一般的 SSL 安全证书签发服务都需要付费,且价格昂贵。为了加快https 的普及, EEF 电子前哨基金会、 Mozilla 基金会和美国密歇根大学成立了一个公益组织叫 ISRG ( Internet Security Research Group ),这个组织从 2015 年开始推出了 Let's Encrypt 免费证书。这个免费证书不仅免费,而且还相当好用,所以我们就可以利用 Let's Encrypt 提供的免费证书部署 https 了。
接下来就进入远端服务器,进行安装与配置:
1、获取certbot-auto
wget https://dl.eff.org/certbot-auto chmod a+x ./certbot-auto ./certbot-auto --help
2、生成证书
泛域名证书不包含根域名,所以下面两次 -d 参数。
./certbot-auto certonly \ -d '*.xiximiao.com' \ -d "xiximiao.com" \ --manual \ --preferred-challenges dns-01 \ --server https://acme-v02.api.letsencrypt.org/directory
一路 输入几次 Y 到如下信息
Please deploy a DNS TXT record under the name _acme-challenge.xiximiao.com with the following value: glIui8kU9GHFQlg_QJN1UIoc1t6W94uaw1MLJfHds3Q Before continuing, verify the record is deployed. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Press Enter to Continue
⚠️ 去dns域名提供商添加域名记录,注意类型是TXT.
解析生效建议等待10分钟左右, 然后 nslookup 验证 多试几次.
nslookup -type=txt _acme-challenge.xiximiao.com Server: 100.100.2.138 Address: 100.100.2.138#53 Non-authoritative answer: _acme-challenge.xiximiao.com text ="glIui8kU9GHFQlg_QJN1UIoc1t6W94uaw1MLJfHds3Q"
如上面能得到text的返回说明添加成功. 可以去command窗口,Press Enter to Continue 回车继续了。
⚠️ 如果txt记录没有生效就Enter,前面的步骤就需要重来,急不得。
Waiting for verification... Cleaning up challenges Subscribe to the EFF mailing list (email: xxxxx@qq.com). IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/xiximiao.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/xiximiao.com/privkey.pem Your cert will expire on 2020-12-05. To obtain a new or tweaked version of this certificate in the future, simply run certbot-auto again. To non-interactively renew *all* of your certificates, run "certbot-auto 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
添加完之后去下面 路径检查一下密钥是否生成:
如上,成功申请通配符证书,过期时间是90天后
证书文件存放 /etc/letsencrypt/live/
生成 dhparams
openssl dhparam -out /etc/ssl/certs/dhparams.pem 2048
配置虚拟主机:
在 /etc/nginx/conf.d 路径下新建一个 ssl.conf 配置如下:
# ssl on; # listen 443 ssl; ssl_certificate /etc/letsencrypt/live/xiximiao.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/xiximiao.com/privkey.pem; # openssl dhparam -out /etc/ssl/private/dhparam.pem 2048 ssl_protocols TLSv1.3 TLSv1.2; ssl_ciphers TLS13-AES-256-GCM-SHA384: TLS13-CHACHA20-POLY1305-SHA256: TLS13-AES-128-GCM-SHA256: TLS13-AES-128-CCM-8-SHA256: TLS13-AES-128-CCM-SHA256: TLS_AES_256_GCM_SHA384: TLS_CHACHA20_POLY1305_SHA256: TLS_AES_128_GCM_SHA256: ECDHE-ECDSA-AES256-GCM-SHA384: ECDHE-RSA-AES256-GCM-SHA384: ECDHE-ECDSA-CHACHA20-POLY1305: ECDHE-RSA-CHACHA20-POLY1305: ECDHE-ECDSA-AES128-GCM-SHA256: ECDHE-RSA-AES128-GCM-SHA256: ECDHE-ECDSA-AES256-SHA384: ECDHE-RSA-AES256-SHA384: ECDHE-ECDSA-AES128-SHA256: ECDHE-RSA-AES128-SHA256; ssl_prefer_server_ciphers on; # Forward Secrecy ssl_session_cache shared:SSL:10m; ssl_session_timeout 30m;
⚠️要注意 ssl_certificate 和 ssl_certificate_key 分别是上面生成证书和密钥路径
打开同在 /etc/nginx/conf.d 下的 default2.conf:
把listen改成 443 ssl,最后保存退出,重启一下nginx
到Chrome浏览器去查看配置情况,配置了这个证书之后, 所有二级域名,都将通用,无需再次单独配
这可比腾讯阿里云的证书好多了,因为腾讯云每个二级域名都是需要配置证书的
过期时间的问题,证书每三个月会过期,但是可以无限续签,邮箱会通知你
首先使用命令行看看证书是否过期了
先看看手动续签,使用命令行:
./certbot-auto renew # 如果提示未到期,cert not due for renewal ./certbot-auto renew --force-renew #强制更新 ./certbot-auto renew -d xiximiao.com #指定更新下 # 看到success表示成功了
自动续签:
#每两个月更新一次
45 2 */2 * * cd /opt/certbot&& ./certbot-auto renew && /etc/init.d/nginx reload
另外可以写 shell > renew-cert.sh
#!/bin/bash
nginx -s stop # 停止nginx
# 续签
/root/certbot-auto renew --force-renew
# 重启nginx
nginx -s reload
chmod a+x renew-cert.sh #给所有的用户添加执行 renew-cert.sh 这个文件的权限
自动更新https证书
0 4 1 */2 * /root/renew-cert.sh >/root/crontab.log 2>&1
certbot 官网 https://certbot.eff.org/about/
免费泛域名SSL证书 Let's Encrypt 申请
Let's Encrypt是目前免费ssl证书最广泛,免费而且证书也被众多机构认可的。
免费申请网址 https://www.sslforfree.com 用certbot这个就不详细写了.
WordPress ssl 迁移
我站使用wordpress mutisite 多站点模式, 在配置到证书之后 可能还需要对旧的数据进行替换和检查。使用前注意备份。
迁移后 数据库 替换插件 https://wordpress.org/plugins/velvet-blues-update-urls/ http://wp101.net/plugins/better-search-replace/ 全站 ssl 检查插件 https://wordpress.org/plugins/really-simple-ssl/ https://wordpress.org/plugins/ssl-insecure-content-fixer/ https://wordpress.org/plugins/wp-force-ssl/ https://wordpress.org/plugins/https-redirection/