为网站部署数字证书

一, 数字证书申请

1. 安装Certbot(以Ubuntu为例)

1
2
sudo apt update
sudo apt install certbot python3-certbot-nginx

2. 申请证书(Nginx为例)

1
sudo certbot --nginx -d yourdomain.com -d 你的域名

3. 自动续期测试

1
sudo certbot renew --dry-run
1
2
3
4
5
6
7
8
通常 Certbot 生成的证书路径如下:

/etc/letsencrypt/live/yourdomain.com/
├── fullchain.pem # 证书链(包含域名证书+中间证书)
├── privkey.pem # 私钥文件
├── cert.pem # 域名证书(不含中间证书)
└── chain.pem # 中间证书
关键文件:fullchain.pem + privkey.pem

4. Nginx 基础 HTTPS 配置

编辑 Nginx 配置文件(通常位于 /etc/nginx/conf.d/):

1
2
cd /etc/nginx/conf.d/ 
ls -al # 查看nginx配置文件

打开配置文件(没有的话直接vim创建),调整内容粘贴仔细查看括号中的内容

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
# HTTPS 配置块
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.wzl.com wzl.com; #(这里按照你的域名修改)

# SSL 证书路径 (这里按照你的证书地址修改)
ssl_certificate /etc/letsencrypt/live/wzl.homes/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/wzl.homes/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

# 静态文件路径(这里按照你的情况修改)
root $path; # $path指的是你映射的目录
index index.html;# 你加载的静态文件

location / {
try_files $uri $uri/ =404;
}
}

# HTTP 跳转 HTTPS 配置块 (这里按照你的域名修改)
server {
listen 80;
listen [::]:80;
server_name www.wzl.com wzl.com;# (这里按照你的域名修改)

# 强制跳转到 HTTPS
return 301 https://$host$request_uri;
}

5. 重启nginx服务

1
sudo nginx -t && sudo systemctl restart nginx

6. 验证证书状态

浏览器访问:www.wzl.com# 指你的域名

命令行检测:

1
curl -I https://wzl.com

逾期输出:

1
2
3
4
5
6
7
8
9
HTTP/1.1 200 OK # 表明证书配置好了
Server: nginx/1.20.1
Date: Sun, 20 Apr 2025 06:02:46 GMT
Content-Type: text/html
Content-Length: 78995
Last-Modified: Sun, 20 Apr 2025 05:15:26 GMT
Connection: keep-alive
ETag: "680482ee-13493"
Accept-Ranges: bytes