1. 安装 certbot 客户端
Deiban系列:
RedHat系列:
2. 自助签发证书
1
2
3
|
# webroot是签发域名的根目录,用于写入文件验证域名所有权
# -d 用于指定要签发的域名,可同时指定多个
certbot certonly --webroot -w /var/www/example -d example.com -d www.example.com
|
如果设置了反向代理或防火墙等,可使用nginx插件自动配置验证。
1
|
certbot certonly --nginx -d example.com
|
如果插件没安装,执行下面命令安装
1
2
3
4
5
6
|
# 1. 更新软件包列表
sudo apt update
# 2. 安装 Certbot + Nginx 插件
sudo apt install -y python3-certbot-nginx
# 3. 验证安装成功(查看插件是否存在)
certbot plugins
|

3. 在web服务器中配置证书
以 Nginx 为例,配置证书内容如下。
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
|
server {
listen 80;
server_name rpc.notnow.ink;
return 301 https://rpc.notnow.ink$request_uri;
}
server {
listen 443;
server_name rpc.notnow.ink;
root /var/www/html/rpc;
ssl_certificate /etc/letsencrypt/live/rpc.notnow.ink/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/rpc.notnow.ink/privkey.pem;
ssl_session_timeout 5m;
location / {
try_files $uri $uri/ /index.html;
if ($allowed_country = 0) {
return 403;
}
if ($allowed_city = 0) {
return 403;
}
client_max_body_size 1m;
}
location /api/ {
proxy_pass http://127.0.0.1:9000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 1m;
}
}
|

4. 访问网站测试
访问网站将显示 https 该网站是安全的。

5. 更新证书
- 使用 certbot 更新证书。
1
2
3
4
5
6
7
8
|
# 更新所有证书
certbot renew
# 更新指定域名
certbot renew --cert-name example.com
# 使用nginx插件自动配置验证更新
certbot renew --nginx
|
- 重启nginx以生效
1
|
systemctl restart nginx
|
- 配置自动更新
1
2
3
4
5
|
# 编辑 crontab(每天凌晨 3 点执行,静默更新所有证书)
crontab -e
# 添加以下内容(CentOS/Ubuntu 通用)
0 3 * * * certbot renew --quiet
|