背景:

最近本站的ssl证书又过期了需要更新,于是趁此机会研究下Certbot自动续期,实现ssl证书自动更新;

环境:

操作系统:ubuntu 22.04 LTS

步骤记录:

1.安装certbot(使用apt安装)

添加Certbot仓库

1
2
3
4
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt update

2.插件选择

目前certbot常用的自动续期方式有两种,一种是使用certbot-nginx插件的方式,一种是使用dns服务商插件的方式;第一种方式不支持泛域名,如果需要申请支持泛域名的ssl证书的话,只能选择第二种方式;

3.nginx插件方式(不支持泛域名)

3.1 安装nginx插件

1
sudo apt install certbot python3-certbot-nginx

打开nginx.conf,在你的站点配置中添加ACME挑战路径处理模块

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
server {
listen 80;
server_name demo.cyanfish.site;

# 添加 ACME 挑战路径处理
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /var/www/letsencrypt;
}

# 其他请求重定向到 HTTPS
location / {
return 301 https://$host$request_uri;
}
}

# HTTPS 配置
server {
listen 443 ssl http2;
server_name demo.cyanfish.site;
ssl_certificate /etc/letsencrypt/live/cyanfish.site/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/cyanfish.site/privkey.pem;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:10m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/errors;
}

add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header Cache-Control "public, max-age=3600";

# 代理到本地 8000 端口
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

access_log /var/log/nginx/cyanfish_access.log;
error_log /var/log/nginx/cyanfish_error.log;
}

server {
listen 8000;
server_name localhost;
charset utf-8;

root /var/www/demoSite;
index index.html;

# 支持history模式前端路由
location / {
try_files $uri $uri/ /index.html;
}
}

3.2 运行命令申请ssl证书

1
certbot --nginx -d your.site -d your.another.site

成功的响应:

4.dns插件申请(支持泛域名)