Certbot 申请 Let's Encrypt 免费证书 启用 https 安全加密

0_1483698500629_letsencrypt-logo-horizontal.png
Let`s Encrypt 提供免费,公益,安全的 https 证书。Let's Encrypt 得到许多公司和机构支持,包括 Mozilla 和 Chrome. 你可以方便的申请安全证书,拥有与付费 https 证书相同功效和安全性。

Let`s Encrypt 使用一种新式方法验证域名所有权,名为 ACEM ,简单来说是在你希望启用 https 的域名指向的服务器网站根目录放置一个验证文件,然后通过域名读取服务器上的验证文件,如果读取成功,代表这个域名就是你的,会颁发加密证书给域名。

通过 ACME Client Implementations 可以发现 Let`s Encrypt 推荐使用 Certbot 申请获取证书。

https://certbot.eff.org/

0_1483698347856_QQ截图20170106182404.png

如上图选定后在页面下方会显示出安装 Certbot 的步骤教学,按照这些步骤可以快速在服务器部署程序 Certbot 并且开始使用 https .

以下是启用 https 的流程,我使用 Nginx 和 Debian 9 系统。这篇文章是很早之前编写的已经过时所以现在修改一下,但依旧按照多年前的旧方式部署 SSL ...... 使用命令的方式,复制即可,操作迅速可控性好。

安装 Certbot

Centos7
yum install certbot

Debian
apt-get install certbot
如提示 E: The value 'stretch-backports' is invalid for APT::Default-Release as such a release is not available in the sources. 按照 https://backports.debian.org/Instructions/ 添加 backports,debian 8 需要另寻方法,具体可以查看Failed to fetch jessie backports repository 。因为我没有用 Debian 8 所以不能测试写在这里...

启动域名验证,生成域名证书和私钥文件,这个命令会弹出界面,有两个方式,因为网站已经在启动,建议选择 "webroot" 根目录验证方式。

certbot certonly

0_1483699816019_QQ截图20170106104826.png

用命令部署

--webroot -w 指向网站根目录,-d 后面为要被验证的域名。如果服务器还有其他网站,可以添加其他网站的根目录和域名。
更多其他参数含义查看 文档

certbot certonly --webroot -w /home/nodebb/public -d set-fire.com -d www.set-fire.com  --agree-tos --email xxxx@www.set-fire.com

授权同服务器其他目录网站域名的例子。

certbot certonly --webroot -w /home/nodebb/public -d set-fire.com -d www.set-fire.com  --webroot -w /home/corps -d corps.set-fire.com --agree-tos --email xxxx@www.set-fire.com

成功后的返回结果 :

IMPORTANT NOTES:

  • Congratulations! Your certificate and chain have been saved at
    /etc/letsencrypt/live/set-fire.com/fullchain.pem. Your cert will
    expire on 2017-04-06. 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

信息提示域名证书已经放进了 /etc/letsencrypt/live/set-fire.com/ 目录,那么咱们就在添加进 nginx 配置文件来试试。

cert.pem 服务器证书文件
chain.pem 中间证书

fullchain.pem 是 cert.pem 和 chain.pem 结合文件,nginx 配置放在 ssl_certificate 参数后方。
privkey.pem 私钥文件,也就是常见的 ***.key ,nginx 配置在 ssl_certificate_key 参数后方。

具体看这个文档

以下只是配置文件的部分内容...

server {
    listen 443 ssl http2 fastopen=3;

    server_name www.set-fire.com;  
    
    ssl on;
    ssl_certificate /etc/letsencrypt/live/set-fire.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/set-fire.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    

如何给证书续命呢?

Let's Encrypt 证书的有效期是90天,也就是说必须在颁发日开始后90天到期之前续命。为证书续命可以阅读这些文档 Renewing certificates .

现在系统包方式安装的 Certbot 可以自动续订安全证书,包括 Centos 7 与 Debian 8 和 9 ,更多其他系统具体查看 Automated Renewals

监测到期日期以及是否需要续订:

certbot renew

这个命令可以更新30内到期的证书。如果想无视日期更新所有证书则增加 --force-renewal 命令。

更新:
//2019年6月20日
修改陈旧过时的内容和删除废话。

最后由 BlueBlue_Master 编辑