网站如果需要从http升级到https,需要申请证书,然后将申请到的证书文件,部署到对应域名下的服务器中。证书有免费的或者收费的,免费的证书通常是3个月有效期,需要自己手动定时申请续期。收费的根据不同标准,有效期时间会长一点。
本次记录固定场景的实践过程,使用certbot工具一键申请并自动配置nginx。
certbot 官网
certbot Github地址
debian
一、必备环境条件
- Web 服务器已安装并运行(需通过 80 端口对外提供服务,Let's Encrypt 验证需用到):
-
- 如果你用 Nginx:systemctl status nginx 确认状态为 active (running)
-
- 如果你用 Apache:systemctl status apache2 确认状态为 active (running)
- 域名已解析到服务器 IP:确保 www.example.cn 解析到当前服务器的公网 IP(可通过 ping www.example.cn 验证)
- 80/443 端口已开放:防火墙需允许这两个端口(Debian 常用 ufw 或 firewalld 管理)
二、具体操作指令(可直接复制执行)
步骤 1:更新系统并安装 Certbot
# 更新系统包列表
sudo apt update
# 安装 Certbot 及对应 Web 服务器插件(二选一)
# 如果你用 Nginx:
sudo apt install certbot python3-certbot-nginx -y
# 如果你用 Apache:
sudo apt install certbot python3-certbot-apache -y
步骤 2:申请证书并自动配置 Web 服务器
# 根据你的 Web 服务器选择对应命令
# Nginx 用户:
sudo certbot --nginx -d www.example.cn
# Apache 用户:
sudo certbot --apache -d www.example.cn
步骤 3:按交互提示完成配置(无需复制,手动操作)
- 输入邮箱(用于接收证书到期提醒)→ 按回车
- 输入 A 同意服务条款 → 按回车
- 输入 N (不分享邮箱)→ 按回车
- 选择是否将 HTTP 重定向到 HTTPS → 推荐输入 2(强制重定向)→ 按回车
步骤 4:验证证书是否生效
# 查看已安装的证书信息
sudo certbot certificates
# 重启 Web 服务器确保配置生效
# Nginx:
sudo systemctl restart nginx
# Apache:
sudo systemctl restart apache2
步骤 5:配置证书自动续期(Let's Encrypt 证书有效期 90 天)
# 测试自动续期功能(模拟续期,不实际修改证书)
sudo certbot renew --dry-run
# Debian 系统默认已通过 systemd 定时器配置自动续期,可验证:
systemctl list-timers | grep certbot
三、常见问题解决
- 若提示 80 端口被占用:
# 查看占用 80 端口的进程
sudo lsof -i :80
# 停止对应进程(如 Apache/Nginx 以外的程序)
sudo systemctl stop 进程名
- 若域名解析正确但验证失败:
# 检查防火墙是否开放 80 端口(以 ufw 为例)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
完成后,访问 www.example.cn 应显示安全锁标志,表明证书生效。
centos
在 CentOS 7 系统上使用 Certbot 申请 HTTPS 证书,需要先满足基础环境条件,再按步骤执行操作。以下是详细指南:
一、必备环境条件
- Web 服务器运行正常(需通过 80 端口对外服务,用于 Let's Encrypt 域名验证):
-
- 若使用 Nginx:systemctl status nginx 需显示 active (running)
-
- 若使用 Apache:systemctl status httpd 需显示 active (running)
- 域名已解析到服务器公网 IP:
-
- 执行 ping 你的域名(如 ping www.example.cn),确保解析结果为服务器公网 IP
- 80/443 端口已开放:
-
- 防火墙需允许这两个端口(CentOS 7 默认用 firewalld 管理)
二、具体操作指令(可直接复制执行)
步骤 1:安装必要依赖和 Certbot
# 1. 安装 EPEL 仓库(Certbot 依赖源)
sudo yum install epel-release -y
# 2. 更新系统包
sudo yum update -y
# 3. 安装 Certbot 及对应 Web 服务器插件(二选一)
# 若用 Nginx:
sudo yum install certbot python2-certbot-nginx -y
# 若用 Apache:
sudo yum install certbot python2-certbot-apache -y
步骤 2:申请证书并自动配置 Web 服务器
# 根据 Web 服务器选择命令,替换为你的域名(如 www.example.cn)
# Nginx 用户:
sudo certbot --nginx -d 你的域名
# Apache 用户:
sudo certbot --apache -d 你的域名
步骤 3:按交互提示完成配置(手动操作)
- 输入邮箱(用于接收证书到期提醒)→ 回车
- 输入 A(同意服务条款)→ 回车
- 输入 N(不分享邮箱)→ 回车
- 选择是否将 HTTP 重定向到 HTTPS → 推荐输入 2(强制重定向)→ 回车
步骤 4:验证证书生效
# 查看证书信息(确认有效期和域名)
sudo certbot certificates
# 重启 Web 服务器生效配置
# Nginx:
sudo systemctl restart nginx
# Apache:
sudo systemctl restart httpd
步骤 5:配置证书自动续期(有效期 90 天)
# 测试续期功能(模拟操作,不实际更新证书)
sudo certbot renew --dry-run
# 添加定时任务(每月自动续期)
sudo crontab -e
# 在打开的编辑器中添加以下内容(保存后退出):
0 0 1 * * /usr/bin/certbot renew --quiet --renew-hook "systemctl reload nginx"
# (若用 Apache,将 nginx 替换为 httpd)
三、常见问题解决
- 80 端口被占用导致验证失败:
# 查看占用 80 端口的进程
sudo netstat -tulpn | grep :80
# 停止非 Web 服务器的进程(如临时程序)
sudo systemctl stop 进程名
- 防火墙阻止端口访问:
# 开放 80/443 端口(firewalld)
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload
- 依赖缺失报错:
若提示缺少某 Python 模块(如 pyparsing),用以下命令安装:
# 安装 pip2(若未安装)
sudo yum install python2-pip -y
# 安装缺失的模块(替换为实际缺失的包名)
sudo pip2 install 模块名
完成后,访问 https://你的域名 应显示安全锁标志,表明证书配置成功。
- nginx配置路径不是默认路径
建立软链接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
或者直接告诉certbot的地址
sudo certbot --nginx \
--nginx-server-root /usr/local/nginx/conf \
--nginx-ctl /usr/local/nginx/sbin/nginx \
-d xxx.xxx.cn
sudo certbot --nginx --nginx-server-root /usr/local/nginx/conf -d xxx.xxx.cn