申请 Let’s Encrypt 通配符 HTTPS 证书

发布 : 2018-05-21 分类 : Notes 浏览 :

Let’s Encrypt 是 ISRG (Internet Security Research Group) 提供的一个免费、开放、自动化的证书签发服务。

下载 Certbot

  1. 使用 wget 命令下载 certbot 客户端

由于 Let’s Encrypt 的有效期是 90 天,到期后我们需要对证书进行续签,因此每隔一段时间就需要使用到这个工具,所有建议将这个工具下载到 /sbin 目录。

1
wget https://dl.eff.org/certbot-auto -O /sbin/certbot-auto

  1. 设为可执行权限
    1
    chmod a+x /sbin/certbot-auto

申请证书

  1. 使用 Certbot 申请通配符证书
1
2
# 注xxx.com请根据自己的域名自行更改
/sbin/certbot-auto certonly -d *.newyingyong.cn --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
  • certonly,表示安装模式,Certbot 有安装模式和验证模式两种类型的插件。
  • –manual 表示手动安装插件,Certbot 有很多插件,不同的插件都可以申请证书,用户可以根据需要自行选择
  • -d 为那些主机申请证书,如果是通配符,输入 *.newyingyong.cn(可以替换为你自己的域名)
  • –preferred-challenges dns,使用 DNS 方式校验域名所有权
  • –server,Let’s Encrypt ACME v2 版本使用的服务器不同于 v1 版本,需要显示指定。
  1. 提示输入您的邮箱地址
1
2
3
4
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):
  1. 输入 A 同意 Let’s Encrypt 协议
1
2
3
4
5
6
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel:
  1. 输入 Y 对域名和机器(IP)进行绑定
1
2
3
4
5
6
7
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
-------------------------------------------------------------------------------
(Y)es/(N)o:
  1. 要求配置 DNS TEXT 记录,从而校验域名所有权
1
2
3
4
5
6
7
8
9
-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.whai.me with the following value:

2_8KBE_jXH8nYZ2unEViIbW52LhIqxkg6i9mcwsRvhQ

Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue

添加完解析后,稍等1分钟,按回车执行。如果证书申请成功,提示如下信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/whai.me/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/whai.me/privkey.pem
Your cert will expire on 2018-06-12. 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

设置自动更新

Let’s Encrypt 的单次授权有效时间为 90 天。
所以我们需要设置自动更新授权,在其没过期之前更新一遍。
命令行运行以下命令:

1
certbot-auto renew

会输出类似:

1
2
3
4
5
6
7
8
9
10
11
$ certbot-auto renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/tiicle.com.conf
-------------------------------------------------------------------------------
Cert not yet due for renewal

The following certs are not due for renewal yet:
/etc/letsencrypt/live/tiicle.com/fullchain.pem (skipped)
No renewals were attempted.

certbot-auto renew 命令会读取 所有 /etc/letsencrypt/renewal 目录下的配置信息,遍历检查每一个授权域名是否离过期时间只有 30 天,如果是的话,执行更新。因为我们才刚刚授权过,所以如以上显示的,skipped 跳过了。

renew 命令自带了判断机制,我们可以放心地把它放到 cron 里去每日执行。

1
sudo crontab -e

增加以下内容:

1
2
30 2 * * * /usr/local/sbin/certbot-auto renew >> /var/log/le-renew.log
35 2 * * * /etc/init.d/nginx reload

这样就能每日检查,并保证 HTTPS 授权不过期。

本文作者 : 王海
原文链接 : https://blog.whai.me/2018/05/21/letsencrypt-https/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
留下足迹