Let's Encrypt 免费SSL证书申请

1,340 阅读3分钟

Let's Encrypt 是一家具有颁发TLS证书的非盈利性机构。

用户通过使用ACME协议的客户端从Let's Encrypt获取CA证书,官方推荐的支持AMCE协议的客户端叫Certbot

Certbot

支持操作系统

  • Windows
  • Linux

必须环境

  • Python 2.7/3.5
  • root 权限

安装Certbot

下载certbot-auto

$ wget https://df.eff.orgg/cert-auto
$ ./certbot-auto --help

官方提醒

尽可能使用Certbot客户端自动管理您得证书。

概述

Certbot使用许多不同命令请求特定的操作。

Certbot客户端支持获取和安装证书。

当使用certonly命令,仅获取证书。

使用run命令,手动指定不同的验证国企和安装程序。

PluginAuthInstallNotesChallenge types (and port)
apacheYY自动化获取并安装证书tls-sni-01 (443)
webrootYN已经有运行的服务,通过验证webroot目录来获取证书http-01 (80)
nginxYY使用nginx自动获取和安装证书tls-sni-01 (443)
standaloneYN建立一个standalone WEB服务,需要80或者443端口可用,如果你没有类似nginx和apache等服务,这很有用http-01 (80) or tls-sni-01 (443)
DNS pluginsYN通过修改dns服务器的text记录,来获取证书,野卡证书只能通过此方式获取dns-01 (53)
manualYN通过自己给指令获取证书,支持添加定制脚本来完成任务http-01 (80), dns-01 (53) or tls-sni-01 (443)

Certbot采用以下两种方式获取证书

  • --standalone Certbot启动Web Server来验证对域名的所有权
  • --webroot 利用已经启动的Web Server验证用户是否拥有这个域名

使用方法

Webroot

格式

certbot certonly \
  --webroot \        -- 插件使用方式
  -w/--webroot-path  -- 目录

例如申请www.example.comexample.comother.example.netanother.other.example.net 这4个域名证书

$ certbot certonly --webroot -w /var/www/example -d www.example.com -d example.com -w /var/www/other -d other.example.net -d another.other.example.net

-w-d 配合使用,域名使用 -w 指定目录。

验证机制

在域名对应的目录下创建临时文件.well-known/acme-challenge用于验证。

Nginx服务器本身禁止访问.开头的文件夹

验证的时候自动向${webroot-path}/.well-known/acme-challenge目录写一个临时文件,然后发送一个请求,去验证是否可以正常访。

standalone

该插件会启动一个Web Server,使用--preferred-challenges http参数的话,使用80端口;使用--preferred-challenges tls-sni参数对应使用443端口。

使用方法:

$ certbot certonly --standalone -d example.com -d www.example.com

manual

如果还没有Web服务器,可以采用DNS验证,使用--manual参数,方法如下

$ certbot certonly --manual --preferred-challenges dns

在使用自定义验证,可利用--manual-auth-hook--manual-cleanup-hook验证前后钩子添加自定义行为完成验证。

证书管理

查看已申请证书

$ certbot-auto certificates

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Found the following certs:
  Certificate Name: archerwong.cn-0001
    Domains: *.archerwong.cn archerwong.cn
    Expiry Date: 2019-03-17 13:24:57+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/archerwong.cn-0001/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/archerwong.cn-0001/privkey.pem
  Certificate Name: archerwong.cn
    Domains: *.archerwong.cn
    Expiry Date: 2019-03-17 12:34:52+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/archerwong.cn/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/archerwong.cn/privkey.pem

重新创建和更新证书

可以使用certonlyrun命令来创建一个新证书,即使已经存在具有该域名的证书,也没有关系,那么certbot会更新该证书,否则会创建一个新证书,一句话存在就更新,不存在就创建。

runcertonly 的区别:

  • run 默认的方式,获取,并安装一个证书。
  • certonly 是创建或者更新一个证书,但是不安装该证书。

在recreate证书的时候,可以指定具体行为,可以使用--force-renewal, --duplicate, --expand来限定操作,如果你没有具体指定,certbot可能会问你具体意图。

  • --force-renewal 当请求一个已经存在并有相同域名的证书时,需要把每一个域名通过-d来指定,不管过期与否,强制更新证书。
  • --duplicate 告诉certbot不管有没有旧证书,都创建一个独立的新证书
  • --expand 告诉certbot更新一个已经存在的证书,需要使用-d参数包含所有旧的域名,并添加新的域名。
$ certbot --expand -d existing.com,example.com,newdomain.com

改变证书域名

使用--cert-name方式来更改证书的域名,方法如下

$ certbot certonly --cert-name example.com -d example.com
$ certbot certonly --cert-name example.com -d example.org,www.example.org

撤销证书

使用revoke命令撤销证书,撤销证书使用证书请求文件进行撤销,通常情况下名为cert.pem,方法如下

$ certbot revoke --cert-path cert.pem

使用--reason指定证书撤销原因

$ certbot revoke --cert-path cert.pem --reason keycompromise

默认原因有以下几种

  • keycompromise
  • affiliationchanged
  • superseded
  • cessationofoperation

如果使用测试服务器申请的证书需要指定--staging--test-cert参数

删除证书

证书撤销后,使用delete 命令删除证书,删除证书使用--cert-name指定证书域名即可。

$ certbot delete --cert-name example.com

证书撤销后,使用renew命令更新证书时任然被更新。

更新证书

Let's Encrypt证书仅支持90天的有效期,所以需要在过期前再次更新证书。使用方法如下

certbot renew

renew命令会尝试更新所有证书,查找到期天数低于30天证书,然后更新。

renew命令在Linux平台通过在操作系统上面创建定时任务进行证书更新,主要使用cron表达式来完成。

renew命令在Windows平台通过创建计划任务来进行证书更新。

证书在更新时,可以利用前后钩子进行额外操作,如证书申请时候使用--standalone参数,该方式会启动Web Server,若和您的服务器冲突,可以使用钩子--pre-hook "service nginx stop"和更新完成后使用--post-hook "service nginx start"。证书更新完成后使用--deploy-hook完整证书更新。

参考文章: