一, 数字证书申请
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
| server { listen 443 ssl http2 listen [::]:443 ssl http2 server_name www.wzl.com wzl.com
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 index index.html
location / { try_files $uri $uri/ =404 } }
server { listen 80 listen [::]:80 server_name www.wzl.com wzl.com
return 301 https://$host$request_uri }
|
5. 重启nginx
服务
1
| sudo nginx -t && sudo systemctl restart nginx
|
6. 验证证书状态
浏览器访问:www.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
|