Let’s Encrypt 简介

Let’s Encrypt 是国外一个公共的免费SSL项目,由 Linux 基金会托管,它的来头不小,由 Mozilla、思科、Akamai、IdenTrust 和 EFF 等组织发起,目的就是向网站自动签发和管理免费证书,以便加速互联网由 HTTP 过渡到 HTTPS,目前 Facebook 等大公司开始加入赞助行列。

Let’s Encrypt 已经得了 IdenTrust 的交叉签名,这意味着其证书现在已经可以被 Mozilla、Google、Microsoft 和 Apple 等主流的浏览器所信任,你只需要在 Web 服务器证书链中配置交叉签名,浏览器客户端会自动处理好其它的一切,Let’s Encrypt 安装简单,使用非常方便。

Certbot 简介

Certbot 为 Let’s Encrypt 项目发布了一个官方的客户端 Certbot ,利用它可以完全自动化的获取、部署和更新安全证书,并且 Certbot 是支持所有 Unix 内核的操作系统。

安装 Certbot 客户端

1
2
$ yum install certbot # centos
$ # apt install certbot # ubuntu

Certbot 的两种使用方式

  1. webroot 方式: certbot 会利用既有的 web server,在其 web root 目录下创建隐藏文件,Let’s Encrypt 服务端会通过域名来访问这些隐藏文件,以确认你的确拥有对应域名的控制权。
  2. standalone 方式: Certbot 会自己运行一个 web server 来进行验证。如果我们自己的服务器上已经有 web server 正在运行 (比如 Nginx 或 Apache ),用 standalone 方式的话需要先关掉它,以免冲突。

获取证书

webroot 模式

使用这种模式会在 web root 中创建 .well-known 文件夹,这个文件夹里面包含了一些验证文件,Certbot 会通过访问 example.com/.well-known/acme-challenge 来验证你的域名是否绑定的这个服务器,所以需要编辑 nginx 配置文件确保可以访问刚刚创建的 .well-known 文件夹及里边存放的验证文件,以便在生成证书时进行验证:

使用以下命令查看 nginx 配置文件地址:

1
2
3
$ sudo nginx -t
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful

编辑 /usr/local/nginx/nginx.conf 配置

1
2
3
4
5
6
7
8
9
10
server {
...

location /.well-known/acme-challenge/ {
default_type "text/plain";
root /var/www/example;
}

...
}

重启 nginx 服务

1
$ nginx -s reload

获取证书,--email 为申请者邮箱,--webroot 为 webroot 方式,-w 为站点目录,-d 为要加 https 的域名,以下命令会为 example.com 和 www.example.com 这两个域名生成一个证书:

1
$ certbot certonly --email admin@example.com --webroot -w /var/www/example -d example.com -d www.example.com

standalone 模式获取证书

但是有些时候我们的一些服务并没有根目录,例如一些微服务,这时候使用 webroot 模式就走不通了。这时可以使用模式 standalone 模式,这种模式不需要指定网站根目录,他会自动启用服务器的443端口,来验证域名的归属。我们有其他服务(例如nginx)占用了443端口,就必须先停止这些服务,在证书生成完毕后,再启用。

1
$ certbot certonly --email admin@example.com --standalone -d example.com -d www.example.com

nginx 开启 https

证书生成完成后可以到 /etc/letsencrypt/live/ 目录下查看对应域名的证书文件。编辑 nginx 配置文件监听 443 端口,启用 SSL,并配置 SSL 的公钥、私钥证书路径:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {

listen 443;
server_name example.com;
root /var/www/example;
index index.html index.htm;

ssl on;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

charset utf-8;

...

}

添加 HTTP 跳转到 HTTPS:

1
2
3
4
5
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}

重启 nginx 服务,访问 https://example.com 查看是否配置成功

自动续期

Let’s Encrypt 提供的证书只有90天的有效期,所以我们要在在证书到期之前重新获取这些证书,Certbot 提供了一个方便的命令 certbot renew,我们可以先使用 --dry-run 测试是否可用:

1
$ certbot renew --dry-run

linux 系统上有 cron 可以来搞定这件事情,使用以下命令新建任务:

1
$ crontab -e

写入以下任务内容。这段内容的意思就是 每隔 两个月的 凌晨 2:15 执行 更新操作

1
15 2 * */2 * certbot renew --quiet --renew-hook "service nginx restart"
参数表述
–quiet执行时屏蔽错误以外的所有输出,也可以使用 -q
–pre-hook执行更新操作之前要做的事情
–pre-hook执行更新操作之前要做的事情
–post-hook执行更新操作完成后要做的事情

取消证书

可以使用以下命令取消刚刚生成的密匙,也就是以上的反操作:

1
2
$ certbot revoke --cert-path /etc/letsencrypt/live/example.com/cert.pem
$ certbot delete --cert-name example.com

至此,整个网站升级到 HTTPS 就完成了。

小结

  1. 如果加上 HTTPS 之后😊锁不够绿的话,检查下站点加载的资源(比如 js、css、照片等)是不是有 HTTP 的,有的话就会导致小锁变为灰色。
  2. 在我们使用 HTTP 的时候,打开网页总会遇到第三方偷偷加的一些脚本广告,很是烦人,升级 HTTPS 后他们就无从下手了,欧耶。