企业证书 和 免费证书,本篇都是使用 免费证书
使用 Let’s Encrypt
手动申请SSL证书
- 购买一个域名
阿里云、腾讯云、Azure 都可以
你有一个域名 domain
xxx.com
- DNS解析到公网服务器
A XXX.com ---> 服务器IP
验证:
ping xxx.com
能通即可。
- 申请证书
向 CA 机构申请证书
手动的使用 acme.sh
- 安装
# 安装
curl https://get.acme.sh | sh
# 加载环境变量
source ~/.bashrc
# 检查
acme.sh --version
- 准备一个目录给CA做认证
sudo mkdir -p /var/www/html
- 申请证书
acme.sh --issue \
-d xxx.domain.com \
--webroot /var/www/html
如果成功就能看到
Your cert is in: ~/.acme.sh/ragflow.akabane.top/fullchain.cer Your key is in: ~/.acme.sh/ragflow.akabane.top/ragflow.akabane.top.key
证书就在:
~/.acme.sh/xxx.doamin.com/fullchain.cer
~/.acme.sh/xxx.doamin.com/xxx.domain.com.key
- 反代
nginx来做反代配置
# 强制 HTTP → HTTPS
server {
listen 80;
server_name xxx.domain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name xxx.domain.com;
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:1080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300s;
}
}
自动续期SSL证书
Let’s Encrypt 证书有效期:90 天
工程实践都是:
- 自动续约
| 工具 | 是否推荐 |
|---|---|
| certbot | ⭐⭐⭐⭐ |
| acme.sh | ⭐⭐⭐⭐⭐ |
| Caddy内置 | ⭐⭐⭐⭐⭐⭐ |
最方便的方法是使用 Caddy 反代并自动申请证书,自动支持 https
两个方案:
- caddy
- Certbot + Nginx
反向代理玩法
caddy
caddy 官方文档:
比较推荐使用,直接就能实现反向代理和证书的自动认证和续约
Caddyflie:
app.domain.com {
reverse_proxy 127.0.0.1:1080
}
将文件替换 /etc/caddy/Caddyfile
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
sudo systemctl status caddy --no-pager
日志排错
sudo journalctl -u caddy -e --no-pager
Caddy 内部自动实现了,在云原生场景很顶
定时检测证书剩余时间
↓
少于30天自动续期
↓
无感知替换证书
ngnix + certbot
- 安装 ngnix + cerbot
sudo apt update
sudo apt install -y nginx certbot python3-certbot-nginx
- 写基础反向代理(http)
sudo vi /etc/nginx/sites-available/app
写如下内容:
server {
listen 80;
server_name ragflow.akabane.top;
location / {
proxy_pass http://127.0.0.1:1080;
}
}
启动:
sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
启动SSL并自动申请证书
sudo certbot --nginx -d xxx.com
自动续期
测试:
sudo cerbot renew --dry-run
系统默认已经加了定时任务:
systemctl list-timers | grep certbot
证书位置
/etc/letsencrypt/live/xxx.com/
├── fullchain.pem
├── privkey.pem