在VPS上生成并管理免费SSL证书的关键步骤与配置技巧
一、准备工作与环境配置
部署SSL证书前需完成以下准备:
- 选择免费证书方案:推荐使用Let’s Encrypt的DV证书,支持单域名与通配符
- 安装Certbot工具:通过
sudo apt install certbot python3-certbot-nginx
安装对应版本 - 开放防火墙端口:确保80(HTTP)和443(HTTPS)端口已放行
- 验证域名解析:确认A记录正确指向VPS的公网IP
二、生成SSL证书的核心流程
通过Certbot生成证书的两种方式:
- Standalone模式:
sudo certbot certonly --standalone -d example.com
- Web服务器集成模式:
sudo certbot --nginx
自动修改Nginx配置
生成过程中需选择是否强制HTTPS重定向,推荐选择选项2启用全站加密
三、Web服务器配置实践
以Nginx为例的配置要点:
server { listen 443 ssl; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # 其他安全配置... }
Apache用户需启用ssl模块,并在虚拟主机配置中添加证书路径
四、证书自动续期管理
Let’s Encrypt证书有效期为90天,建议设置自动续期:
- 测试续期命令:
sudo certbot renew --dry-run
- 添加crontab任务:每月执行
certbot renew
- 配置后需重启Web服务:
systemctl reload nginx
五、安全验证与优化建议
部署完成后需执行:
- 通过
https://example.com
检查浏览器安全锁标识 - 使用SSL Labs测试工具检测配置评分(A+为最佳)
- 启用HSTS预加载,添加
add_header Strict-Transport-Security
指令