Certbot申请免费SSL证书

Certbot 是 Let’s Encrypt 官方推荐的获取证书的客户端,可以帮我们获取免费的 Let’s Encrypt 证书

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 安装 certbot
[root@ckx conf.d]# yum install certbot python2-certbot-nginx

# 输入命令
[root@ckx conf.d]# certbot --nginx

# 提示输入接收紧急更新和安全提示的邮箱
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): chenkaixin12121@163.com
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org

# 是否同意该协议
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
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org

# 可以输入数字选择要安装的域名的证书,或者回车全部安装
Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: ckx.ink
2: blog.ckx.ink
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):
Obtaining a new certificate
Deploying Certificate to VirtualHost /etc/nginx/conf.d/ckx.ink.conf
Deploying Certificate to VirtualHost /etc/nginx/conf.d/blog.ckx.conf

# 是否重定向到 https
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/ckx.ink.conf
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/blog.ckx.conf

# 安装成功
Congratulations! You have successfully enabled https://ckx.ink and


You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=ckx.ink
https://www.ssllabs.com/ssltest/analyze.html?d=blog.ckx.ink
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/ckx.ink/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/ckx.ink/privkey.pem
Your cert will expire on 2020-08-14. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. 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

测试

1
2
3
4
5
6
# 检测 nginx 是否配置正确
[root@ckx conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 重新加载 nginx
[root@ckx conf.d]# systemctl reload nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 先检查一波,查看命令是否可以执行成功
[root@ckx conf.d]# /usr/bin/certbot renew --disable-hook-validation --renew-hook "/usr/sbin/nginx -s reload"
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/ckx.ink.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
OCSP check failed for /etc/letsencrypt/archive/ckx.ink/cert1.pem (are we offline?)
# 提示证书尚未到期
Cert not yet due for renewal

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

The following certs are not due for renewal yet:
/etc/letsencrypt/live/ckx.ink/fullchain.pem expires on 2020-06-14 (skipped)
No renewals were attempted.
No hooks were run.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

定时刷新

Let’s Encrypt 免费 SSL 证书只有三个月有效期,但是可以重新申请,我们可以使用它提供的续订命令配合定时任务,实现自动续订

1
2
3
4
5
# 设置定时任务
[root@ckx conf.d]# crontab -e
0 3 * * * /usr/bin/certbot renew --disable-hook-validation --renew-hook "/usr/sbin/nginx -s reload"
# 检查定时任务
[root@ckx conf.d]# crontab -l