利用Let`s Encrypt为你的网站加把小锁

本文最后更新于:1 天前

爱丽丝和鲍勃以及伊娃哪些不得不说的事

前言

https是以安全为目标的http通道,在http的基础上加上了SSL层用于数据加密。有了这个功能你和网站的联系就可以不会被外人窃听了。这么好的功能那必须得用上啊,所以就上网找了下。还行,挺简单的。

那么就开始吧。

配置的原理

原理很简单,就是利用Certbot提供的工具,便捷快速地取得Let`s Encrypt的证书。然后在你的nginx里添加上它给你申请下来的证书。

我的环境是debian10,已安装git,nginx。没有这两个的就先安装好吧。

申请证书

申请特定域名的证书

1
2
3
4
5
6
#先从官方仓库下载好
git clone https://github.com/certbot/certbot
#转到对应的目录
cd certbot
#这里我为域名为yika.cf的子域名q.yika.cf申请一个证书
./letsencrypt-auto certonly -d q.yika.cf -d yika.cf

然后它会提示你下面这样的信息

1
2
3
4
5
6
7
8
How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Nginx Web Server plugin (nginx)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-3] then [enter] (press 'c' to cancel):

这句话就是说让你选一个办法去让ACME这个签发证书的人确定你就是这个域名的持有者。
现在我们就选第二个standalone吧 ,输入 2
然后就会跳出一堆信息,不用管它们。如果出现下面这种字样就说明成功了

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/q.yika.cf/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/q.yika.cf/privkey.pem
Your cert will expire on 2020-06-02. To obtain a new or tweaked
version of this certificate in the future, simply run
letsencrypt-auto again. To non-interactively renew *all* of your
certificates, run "letsencrypt-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

就像它提示的那样,取得的公钥和私钥都存放在 /etc/letsencrypt/live/q.yika.cf/这个目录里面。

申请域名通配符的证书

这个步骤就是调用的命令有点不同,现在是假设你在你的 /certbot 的目录下,输入以下命令

1
./certbot-auto certonly  -d "*.yikakia.com" --manual --preferred-challenges dns-01  --server https://acme-v02.api.letsencrypt.org/directory

输入这个命令以后会提示下面的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Cert is due for renewal, auto-renewing...
Renewing an existing certificate
Performing the following challenges:
dns-01 challenge for yikakia.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:

简单地说就是把这个证书和你的机器绑定,你有固定的公网IP的话同意就是了。输入y 然后回车。
然后会显示下面这样的字样

1
2
3
4
5
6
7
8
9
10
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.你的域名 with the following value:

fAap48jPpKKyS8ntHv9lIoAhGleXpdifuZdz0hhUcCS # 这是一串用于验证所有者的字符。为了隐私和安全我就随便乱打一串相同长度的表示吧

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

这里就是让你在 DNS 界面新加一个TXT记录,域名是_acme-challenge.你的域名,记录值为那一串字符。
下面我用 CloudFlare 的 DNS 界面做演示。

这样写好了之后,等一两分钟,然后回到客户端敲回车确定要验证你是这个网址的所有者。

之后就会显示下面这样的字样。

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

Cleaning up challenges

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/yikakia.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/yikakia.com/privkey.pem
Your cert will expire on 2020-09-02. 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


现在你的网站的域名通配符的证书就搞到手啦,以后不管是什么前缀的网址都可以用这同一个证书,非常方便。

添加监听以及开启强制https

配置nginx

1
2
#输入下面的语句配置你的博客
nano /etc/nginx/conf.d/hexo.conf

这里的配置就按你自己的配置地方配吧。
打开以后,这么写配置

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
server {
#这个配置是监听80端口来的q.yika.cf的请求
#我们收到了后就把它转发到对应的https的链接上起到强制https访问的作用
listen 80;
server_name q.yika.cf;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
root /var/www/hexo;
server_name q.yika.cf;
#下面这两行就是写你的公钥和私钥的存放地址
#fullchain.pem是公钥,privkey.pem是私钥,不要搞混了
ssl_certificate /etc/letsencrypt/live/www.yika.cf/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.yika.cf/privkey.pem;
access_log /var/log/nginx/hexo_access.log;
error_log /var/log/nginx/hexo_error.log;
location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
root /var/www/hexo;
access_log off;
expires 1d;
}
location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
root /var/www/hexo;
access_log off;
expires 10m;
}
location / {
root /var/www/hexo;
if (-f $request_filename) {
rewrite ^/(.*)$ /$1 break;
}
}
}

好了现在Ctrl+x保存它吧。然后输入

1
service nginx restart

来重启nginx的服务。
这时候你再去看看你的博客,网址的左边是不是加了把小锁?
现在进行访问的时候我们都是强制走你的https协议了,不会被有心人劫持掉链接。挺好的不是么?

配置自动续期

输入

1
2
3
4
5
crontab -e
#选择用你熟悉的编辑器编辑
#然后在后面添加
0 3 1 * * /root/certbot/certbot-auto renew --renew-hook "sudo nginx -s reload"
#现在保存了就可以了

这样你就可以高枕无忧了,自动化程序可以帮你搞定自动续期的问题。

这是让你的机器每个月1号的凌晨3点自动更新所有的证书。

尾巴

什么时候写下HTTPS和RSA的算法,来总结一下吧。爱丽丝和鲍勃以及伊娃的爱恨情仇~

参考文章

为你的Blog快速开启https支持
Hexo启用https加密连接
Certbot 自动化生成 https 证书
certbot-auto申请https证书,自动续期


利用Let`s Encrypt为你的网站加把小锁
https://www.yikakia.com/利用Let`s Encrypt为你的网站加把小锁/
作者
Yika
发布于
2020年3月4日
许可协议