“若是能把 SSL 证书附加在域名上,那该多酷啊!” —— 凯丽html
Certbot 是 Let's Encrypt 的官方工具。nginx
本文主要介绍如何使用 Let's Encrypt 的 Certbot 工具免费生成、修改、更新和撤销 SSL 证书。web
简略介绍一点证书的配置。面试
本文没有说起泛域名证书的生成。apache
要求网站已经正确配置了服务器、DNS 等,已经可以正常访问。bash
本文操做环境为 Ubuntu。服务器
可部分参照官网。
在官网首页,选择服务器和操做系统后,会给出相应的安装以及生成证书的命令。app
这里只看安装的部分:dom
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot
复制代码
查看是否安装成功:ide
certbot --version
复制代码
或
letsencrypt --version
复制代码
输出的都是 certbot 的版本号:
certbot 0.25.0
复制代码
重要:在开始以前,先看一下本文末尾关于频率限制的说明。
在前一节里,官网给出安装命令后,也给出了生成证书的命令,好比:
为 Nginx 服务器生成:
sudo certbot --nginx
复制代码
为 Apache 服务器生成:
sudo certbot --apache
复制代码
在生成证书后,能够选择让 certbot 自动修改对应服务器软件的配置文件,包括将 80
端口的请求重定向至 443
,启用相应模块,激活配置文件等。
自动修改后配置文件的写法是值得参考一下的。
可是!为了更深刻地了解 Let's Encrypt,也为了可以更灵活地操做证书,咱们不采用这种快捷方式。而是仅仅生成证书,并手动配置。
方法有 standalone
和 webroot
两种。
不管哪一种方式,certbot 都须要验证域名,可是实现的方式不一样,这也是二者有区别的根本缘由。
standalone
方法生成和更新证书的时候,certbot 须要使用 443 或者 80 端口来验证域名,所以致使须要暂时中止服务器。这种方式不须要给出网站根目录webroot
方法没有上述问题。不过 webroot
方法配置略微复杂,而且须要给出网站根目录webroot
方式之因此不须要 80
或 443
端口,是由于它的实现方式是,在网站根目录中生成一个临时子目录 .well-known/acme-challenge
,而后从 certbot 的服务器向这个路径发送请求,若是请求成功,那么验证经过。
这篇文章简单介绍了几个 webroot 模式可能会遇到的问题。
基本命令为:
sudo letsencrypt certonly --standalone
复制代码
须要使用 80
或 443
端口,能够经过 --preferred-challenges
指定要使用的端口,只要其中一个端口空闲就能够。
可是说实话,难道存在其中一个端口是空闲的状况吗
使用 80
端口:
--preferred-challenges http
复制代码
使用 443
端口:
--preferred-challenges tls-sni
复制代码
使用 -d
指定要生成证书的域名。能够添加多个域名,使用逗号分隔,或者使用多个 -d
。两种方法效果相同。
多个域名时,同一个主域名下的多个子域名是能够的,没有试过多个不一样的主域名是什么效果。
执行生成命令:
sudo letsencrypt certonly --standalone -d example.com,www.example.com
复制代码
第一次使用时,会询问邮箱、是否赞成服务条款、是否接受推送内容。
推荐如实填写邮箱,由于证书即将过时时会经过邮件通知。
推送内容不接受就行了。
证书即将过时的通知邮件:
感受这个通知不太准,不少次收到邮件,可是登陆查看了一下并无。
也(很)多是由于当时没用
--staging
致使生成了不少证书...
2019年4月26日更新:
妈的今天面试演示项目的时候证书过时。。
回来一看有过邮件通知,可是没在乎。。这是一个狼来了的故事
若是看到如下信息说明证书生成成功:
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2018-09-25. 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 复制代码
可使用 certbot certificates
命令查看当前证书的信息:
Found the following certs:
Certificate Name: example.com
Domains: example.com, www.example.com
Expiry Date: 2018-09-26 00:42:47+00:00 (VALID: 89 days)
Certificate Path: /etc/letsencrypt/live/example.com/fullchain.pem
Private Key Path: /etc/letsencrypt/live/example.com/privkey.pem
复制代码
能够看到,生成了1个证书,-d
后面的第一个域名为证书名和所在目录的目录名,该证书包含了两个域名 example.com
和 www.example.com
。
这些域名均可以使用该证书。
共生成了四个文件:
/etc/letsencrypt/archive/example.com
目录保存原件。
/etc/letsencrypt/live/example.com
目录保存文件的软连接,能够理解为快捷方式。
live/
中的文件貌似不仅是原件的软连接那么简单,可是没有仔细研究。
privkey.pem 为私钥,务必妥善保管。
这种方式须要给出网站根目录做为参数。从这名字 webroot
应该也能看出来。
基本命令:
certbot certonly --webroot -w /path/to/webroot -d www.example.com -d example.com
复制代码
其中,-w
是网站的根目录,-d
是要生成证书的域名。
注意,-d
列出的域名的根路径必须所有与 -w
的值相同。
若是要为根路径不一样的多个域名生成证书,那么只须要在后面再添加 -w
及其对应的 -d
便可:
certbot certonly --webroot -w /path/to/webroot1 -d www.example.com -d example.com -w /path/to/webroot2 -d other.example.net -d another.other.example.net
复制代码
其余内容再也不赘述,参考前面 standalone
部分。
从 0.22.0
版本开始,Let's Encrypt 支持生成泛域名证书。
TODO
以 Apache 和 Nginx 为例。这里只介绍一下最基本的配置。
首先,这里是官方文档给出的上面生成的4个文件与服务器配置项的对应关系。
# apache < 2.4.8
SSLCertificateKeyFile privkey.pem
SSLCertificateFile cert.pem
SSLCertificateChainFile chain.pem
复制代码
# apache >= 2.4.8
SSLCertificateKeyFile privkey.pem
SSLCertificateFile fullchain.pem
复制代码
官方文档是这么写的,可是如今最新版本才2.4.3,哪来的2.4.8?
启用 SSL 模块:
a2enmod ssl
复制代码
修改配置文件:
<VirtualHost *:443>
ServerName example.com
SSLCertificateKeyFile privkey.pem
# apache < 2.4.8
SSLCertificateFile cert.pem
SSLCertificateChainFile chain.pem
# apache >= 2.4.8
SSLCertificateFile fullchain.pem
...
</VirtualHost>
复制代码
重启 Apache:
service apache2 restart
复制代码
ssl_certificate_key privkey.pem;
ssl_certificate fullchain.pem;
复制代码
修改配置文件:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate fullchain.pem;
ssl_certificate_key privkey.pem;
...
}
复制代码
重启服务器:
service nginx restart
复制代码
更进一步的配置:《分享一个 HTTPS A+ 的 nginx 配置》
上面使用 standalone
方法已经生成了域名 example.com
和 www.example.com
的证书,证书名为 example.com
。
若是如今又要为 a.example.com
生成证书,那么能够生成一张新的证书,也能够选择添加到现有的证书 example.com
里。
certbot 官方文档提供了简单的示例:
certbot --expand -d existing.com -d example.com -d newdomain.com
复制代码
虽然示例使用了 --expand
选项,可是后面推荐使用的是 --cert-name
,由于 --expand
只能用来添加域名,而 --cert-name
更灵活,既能够添加也能够删除。
官方文档没有提供 --cert-name
的示例,下面介绍一下。
命令的大体结构是:
certbot certonly --cert-name certname -d a.domain.com,b.domain.com
复制代码
--cert-name
选项的参数是现有证书的证书名,-d
选项的参数是证书包含的域名。
注意,多个域名用逗号分隔,而且中间不能有空格。
即,不能是:
-d a.domain.com, b.domain.com
,而应该是-d a.domain.com,b.domain.com
。
如今开始为证书名为 example.com
、包含 example.com
和 www.example.com
两个域名的证书添加一个新的域名 a.example.com
。
certbot certonly --cert-name example.com -d example.com,www.example.com,a.example.com
复制代码
会出现提示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
How would you like to authenticate with the ACME CA?
-------------------------------------------------------------------------------
1: Spin up a temporary webserver (standalone)
2: Place files in webroot directory (webroot)
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
复制代码
选择 1
。或者在命令中添加 --standalone
选项,能够跳过这一步。
Plugins selected: Authenticator standalone, Installer None
-------------------------------------------------------------------------------
You are updating certificate example.com to include new domain(s):
+ a.example.com
You are also removing previously included domain(s):
(None)
Did you intend to make this change?
-------------------------------------------------------------------------------
(U)pdate cert/(C)ancel:
复制代码
选择 u
,执行更新便可。
如今证书中包含了三个域名,若是要从中删除域名www.example.com
,执行的命令为:
certbot certonly --cert-name example.com -d example.com,a.example.com
复制代码
想要删除的域名,不在 -d
后列出来便可。
能够发现,上面在修改由 standalone
方式生成的证书时,仍然能够选择 standalone
和 webroot
。
那么看来证书的生成方式是能够改变的。
可是没有试过,感兴趣的同窗能够试一下。
从证书信息中能够看到,证书有效期为90天。在证书到期前须要进行更新。
能够手动进行更新:
certbot renew
复制代码
根据 certbot 官方文档,该命令只会更新30天内即将过时的证书。对其余的证书没有影响。
证书更新可使用 crontab 等设置计划任务实现自动更新,好比天天执行一次 renew
命令。
可是注意,由 standalone
方法生成的证书,更新时也须要使用 80
或 443
端口,所以可能须要(不如说必定须要)在执行 renew
前先关闭服务器,结束后再从新开启。
使用 revoke
选项:
sudo certbot revoke --cert-path /etc/letsencrypt/live/CERTNAME/cert.pem
复制代码
前面说过,archive
目录保存了证书原件,live
目录保存了证书的软连接。
这里的路径只能是 live
,不能是 archive
,不然会报错。
撤销成功后,会询问是否删除证书文件和证书所在目录:
-------------------------------------------------------------------------------
Would you like to delete the cert(s) you just revoked?
-------------------------------------------------------------------------------
(Y)es (recommended)/(N)o:
复制代码
推荐选择 Yes
自动删除。若是选择 No
,以后须要执行 certbot delete
来删除文件。
根据 certbot 官方文档,不管自动仍是手动,最后都须要把相关文件删掉。
若是没有删除,那么 archive
和 live
两个目录及其中的文件就仍然存在。执行 certbot certificates
仍然能显示该证书的信息,不事后面会注明已经失效 INVALID
。可是,在执行证书更新的时候,已经撤销的证书还会被更新。
Staging Environment - Let's Encrypt 官方文档
Let's Encrypt 为证书申请的频率作了限制,每一个主域名每周不超过20次。
若是达到这个 rate limits,不可以再继续生成了,会报错。
因此若是只是为了学习和测试,最好使用 Let's Encrypt 提供的 Staging Environment(模拟环境),没有频率限制。
只要在执行命令时加上 --staging
选项就能够了。
等会用了再玩真的。
百度 LAVAS 项目中关于 HTTPS 的介绍:使用 HTTPS,写得挺好的。
个人其余文章: