Akemi

通过Let's Encrypt生成免费SSL证书

2025/03/11

本文将介绍如何将Let’s Encrypt生成免费SSL证书,并且进行自动续期

我之前还傻呆呆的在阿里云花68买了个人证书来着,md


Let’s Encrypt的优势

  • 支持ACME协议,实现了证书申请、安装和续期的自动化
  • 提供的SSL/TLS证书完全免费

安装工具与生成SSL证书

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
# 环境说明,使用的是CentOS7.9 2C2G 阿里云公有云实例 

# 安装Let's Encrypt提供的工具certbot与nginx插件
(1)使用certbot的旧python2插件
yum install epel-release
yum install certbot
yum install python2-certbot-nginx -y

(2)使用snapd安装(如果本来用的是snapd就建议这么搞)
yum install snapd -y
systemctl enable --now snapd.socket
ln -s /var/lib/snapd/snap /snap
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot

# 为域名生成一个针对于nginx的证书,我的域名就是akemi.zj.cn
(1)手动配置、自定义 SSL 参数
certbot certonly --nginx -d akemi.zj.cn

证书存放位置/etc/letsencrypt/live/<domain>
ls /etc/letsencrypt/live/akemi.zj.cn
cert.pem chain.pem fullchain.pem privkey.pem README

(2)快速部署、自动配置(建议备份配置文件)
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
certbot --nginx -d akemi.zj.cn
...
server {
listen 443 ssl http2;
server_name akemi.zj.cn;
ssl_certificate /etc/letsencrypt/live/akemi.zj.cn/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/akemi.zj.cn/privkey.pem; # managed by Certbot
root /home/git/web/blog;
...

# 也可以certbot certonly后执行certbot --nginx,但会覆盖配置

验证:网站SSL证书生效

1
nginx -s reload

证书续期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(1)自动续期
如果使用的是certbot --nginx -d akemi.zj.cn,那就会自动续期
可以通过下面两条命令检查
cat /etc/cron.d/certbot

systemctl list-timers | grep certbot
# Tue 2025-03-11 20:03:00 CST 8h left Tue 2025-03-11 05:13:00 CST 6h ago snap.certbot.renew.timer snap.certbot.renew.service

(2)手动续期
certbot renew # 当证书距离到期不足30天时会生效

(3)续期注意事项
续期过程中所有文件均会被更新
续期后重启服务
新证书(cert.pem)会包含更新的有效期 90天
续期后旧文件会被替换,若需备份需手动操作

CATALOG
  1. 1. 安装工具与生成SSL证书
  2. 2. 验证:网站SSL证书生效
  3. 3. 证书续期