如何在Linux系统上安装Let's Encrypt证书 - 完整指南
Let's Encrypt作为目前最受欢迎的免费SSL证书颁发机构,为网站提供安全加密服务。本文将详细介绍在Linux环境下安装Let's Encrypt证书的完整流程。
准备工作
- 拥有服务器root权限
- 已注册并解析的域名
- 服务器已开放80和443端口
- Linux系统(本文以Ubuntu为例)
安装Certbot客户端
Certbot是Let's Encrypt官方推荐的客户端工具。
# Ubuntu/Debian系统
sudo apt update
sudo apt install certbot python3-certbot-nginx
# CentOS/RHEL系统
sudo yum install epel-release
sudo yum install certbot python3-certbot-nginx
获取SSL证书
根据您的Web服务器选择相应方式:
Nginx服务器
sudo certbot --nginx -d example.com -d www.example.com
Apache服务器
sudo certbot --apache -d example.com -d www.example.com
独立验证方式
sudo certbot certonly --standalone -d example.com -d www.example.com
证书自动续订
Let's Encrypt证书有效期为90天,设置自动续订:
sudo certbot renew --dry-run
# 添加定时任务
sudo crontab -e
# 添加以下内容(每天凌晨2点检查续订)
0 2 * * * /usr/bin/certbot renew --quiet
常见问题解决
- 端口占用问题:确保80和443端口未被其他程序占用
- 验证失败:检查域名解析是否正确
- 证书续订失败:检查日志/var/log/letsencrypt/
进阶配置
1. 通配符证书申请:
sudo certbot certonly --manual --preferred-challenges=dns -d *.example.com
2. 强制HTTPS跳转(Nginx配置示例):
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
通过以上步骤,您已成功在Linux服务器上安装Let's Encrypt SSL证书。定期检查证书状态并确保自动续订正常工作,可以保证网站持续提供安全的HTTPS服务。
