申请 Let's Encrypt 的免费通配符证书

原文:https://juejin.im/entry/5aa8f855f265da23945f2f2b

在今天,Let’s Encrypt 宣布他们已经支持了通配符证书(Wildcard Certificates),通配符证书是一个可以被多个子域使用的公钥证书,在多子域名的情况下非常方便。市面上的通配符证书大多数比较昂贵,不适合个人使用,而 Let’s Encrypt 则主推免费证书。在去年的时候宣布今年支持通配符证书,在预定发布的 2.27 为了保证更好的测试推迟了一段时间,今天终于发布了。

这篇博客就介绍一下怎么申请 Let’s Encrypt 的免费通配符证书。

下载最新的 Certbot

通配符证书需要客户端支持 ACME v2,支持 ACME v2 的客户端列表:letsencrypt.org/docs/client…

我们使用 certbot,需要 0.22.0 版本以上的 certbot,

安装 Certbot 客户端

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

如果版本不到 0.22.0,可以利用 pip 更新或者从 GitHub 下载:

git clone https://github.com/certbot/certbot
cd certbot

安装使用 Screen

因为申请过程中涉及到 DNS TXT 记录的更新,更新需要的时间未知,所以使用 Screen 避免等待时间过长服务器自动断开连接:

yum install -y screen
screen -S ssl

这样,如果服务器因为长时间无操作断开连接,我们只需要重新登陆服务器,然后执行:

screen -r ssl

即可恢复会话。

申请 ssl

sudo ./certbot-auto certonly --manual -d *.example.com -d example.com --agree-tos --manual-public-ip-logging-ok --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory

注意:

  1. -d 选项后是你要申请通配符证书的域名
  2. 通配符证书的申请必须使用 DNS TXT 的方式验证域名所有权

根据提示进行几个选择后会出现:

-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:
qqiR_lsa2AjMfoVR16mH4UDbOxy_E02l0K1CNyz1RdI
Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue

此时我们需要在自己的域名解析下添加一条 TXT 记录:

_acme-challenge.example.com

值(随机生成)是:

qqiR_lsa2AjMfoVR16mH4UDbOxy_E02l0K1CNyz1RdI

添加完成后最好等待差不多十几分钟,按下 Enter,certbot 就会验证 DNS TXT 记录是否是随机生成的 qqiR_lsa2AjMfoVR16mH4UDbOxy_E02l0K1CNyz1RdI,如果验证成功:

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-06-11. 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

如果验证失败,可能 DNS TXT 记录没有及时更新,重试一次再多等一会儿 :)

更新 Nginx 设置

更新 Nginx 中所有子域名的配置文件,其实只需要更新其中关于 ssl 位置的两行:

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

将原来的地址更新为申请 ssl 成功时提示的地址就行了:

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

更新后:

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

Apache 同理。

重启 Nginx,访问网站,浏览器查看 ssl 证书




注意事项:

1、因为默认LNMP的虚拟主机里是禁止 . 开头的隐藏文件及目录的,所以访问 http://a.com/.well-known/acme-challenge/**** 这个链接的话返回403错误,所以必须要将对应虚拟主机配置文件里的

location ~ /\.
{
    deny all;
}

这段配置删掉或注释掉或在这段配置前面加上

location ~ /.well-known {
    allow all;
}

以上配置代码,然后重启nginx。

=================================================================

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 配置文件地址:

$ 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 配置

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

重启 nginx 服务

$ nginx -s reload

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

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

standalone 模式获取证书

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

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


nginx 开启 https

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

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:

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 测试是否可用:

$ certbot renew --dry-run

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

$ crontab -e

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

15 5 * */2 * certbot renew --quiet --renew-hook "service nginx restart"

参数表述
–quiet执行时屏蔽错误以外的所有输出,也可以使用 -q
–pre-hook执行更新操作之前要做的事情
–pre-hook执行更新操作之前要做的事情
–post-hook执行更新操作完成后要做的事情

取消证书

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

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

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值