网上有很多教程,但是对于纯小白来说还是有蛮多坑的(比如我),所以还是有必要记录一下我踩过的坑吧?
什么是 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 安装简单,使用非常方便。
申请通配符证书
Let’s Encrypt 上的证书申请是通过 ACME 协议来完成的。ACME 协议规范化了证书申请、更新、撤销等流程,实现了 Let’s Encrypt CA 自动化操作。解决了传统的 CA 机构是人工手动处理证书申请、证书更新、证书撤销的效率和成本问题。
ACME v2 是 ACME 协议的更新版本,通配符证书只能通过 ACME v2 获得。要使用 ACME v2 协议申请通配符证书,只需一个支持该协议的客户端就可以了,官方推荐的客户端是 Certbot。
开工
- 安装Let's Encrypt
$ git clone https://github.com/letsencrypt/letsencrypt
2. 生成证书(网上很多教程都只生成二级域名证书,反而顶级域名没有配置,如163.com),把lihx.top替换成你需要申请证书的域名。
Certbot包装在EPEL(企业Linux的额外包)中。要使用Certbot,必须首先 启用EPEL存储库。在RHEL或Oracle Linux上,还必须启用可选通道。
$ cd letsencrypt
$ ./certbot-auto certonly -d *.lihx.top -d lihx.top --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
参数 | 说明 |
---|---|
certonly | 表示安装模式,Certbot 有安装模式和验证模式两种类型的插件。 |
--manual | 表示手动安装插件,Certbot 有很多插件,不同的插件都可以申请证书,用户可以根据需要自行选择 |
-d | 为那些主机申请证书,如果是通配符,输入 *.you.cn(可以替换为你自己的一级域名) |
--preferred-challenges dns | 使用 DNS 方式校验域名所有权 |
--server | Let's Encrypt ACME v2 版本使用的服务器不同于 v1 版本,需要显示指定。 |
接着可以看到下面界面结果:
Dependency Installed:
dwz.x86_64 0:0.11-3.el7 perl-srpm-macros.noarch 0:1-8.el7 tcl.x86_64 1:8.5.13-8.el7 tix.x86_64 1:8.4.3-12.el7 tk.x86_64 1:8.5.13-6.el7 tkinter.x86_64 0:2.7.5-69.el7_5
Complete!
Creating virtual environment...
Installing Python packages...
Installation succeeded.
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): email@163.com
接下来需要输入些指令:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for kuaichuangkeji.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: Y
这里有几个需交互的提示:
- 是否同意 Let's Encrypt 协议要求=>需要同意
- 是否分享你的邮箱
- 询问是否对域名和机器(IP)进行绑定=>需要同意
接下来不要着急按回车不要按回车不要按回车!!!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.lihx.top with the following value:
IQ-mPEUQxqBi0aYd_UazXTeJFCAF-0FFV3nCFzk8P9
Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
Waiting for verification...
Cleaning up challenges
需要在域名DNS TXT 记录,从而校验域名所有权,也就是判断证书申请者是否有域名的所有权。
上面输出要求给 _acme-challenge.lihx.top 配置一条 TXT 记录,在没有确认 TXT 记录生效之前不要回车执行。就在IQ-mPEUQxqBi0aYd_UazXTeJFCAF-0FFV3nCFzk8P9这一行的位置,复制一下
我的是dynadot注册的域名,别的如万网注册的基本也差不多,小白踩了多次坑才明白,是要配一项子域名解析(圈起来的地方),子域名为_acme-challenge,记录类型为txt,看图就明白了。接下来点击确定,但不一定能即时生效(DNS缓存???)。
假设域名为lihx.top,打开电脑“开始”——“运行”,输入cmd后按回车键,输入: nslookup -qt=txt _acme-challenge.lihx.top
后按回车键,若出现TXT记录值与自己输入的相同,则代表已生效,否则代表未解析成功。

确认生效后,回车执行,输出如下
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/lihx.top/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/lihx.top/privkey.pem
Your cert will expire on 2019-09-03. 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
证书续签
注:证书在到期前30天才会续签成功,但为了确保证书在运行过程中不过期,官方建议每天自动执行续签两次;使用crontab自动续期
$ crontab -e // 编辑定时任务
$ 0 */12 * * * certbot renew --quiet --renew-hook "/etc/init.d/nginx reload"
需要注意"/etc/init.d/nginx reload
"为nginx重启命令,需要根据自己服务器的重启命令重启即可;这里建议使用reload,不推荐使用restart,因为这样万一配错了,也不会影响服务器其他项目的运行
证书保存的路径[配置nginx需要用到的]
/etc/letsencrypt/live/lihx.top/fullchain.pem;
/etc/letsencrypt/live/lihx.top/privkey.pem;
/etc/letsencrypt/live/lihx.top/chain.pem;
配置nginx使ssl生效
server {
listen 443 ssl;
server_name lihx.top;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
ssl on;
ssl_certificate /etc/letsencrypt/live/lihx.top/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/lihx.top/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/lihx.top/chain.pem;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
添加 HTTP 自动跳转到 HTTPS:
server {
listen 80;
server_name lihx.top;
location / {
# 重定向为https
rewrite ^(.*)$ https://$host$1 permanent;
}
}
配置好后重启nginx
取消证书
可以使用一下命令取消刚刚生成的密匙,也就是以上的反操作:
certbot revoke --cert-path /etc/letsencrypt/live/lihx.top/cert.pem
certbot delete --cert-name lihx.top
参考文章