在阿里云CentOS服务器上使用Certbot为Nginx配置SSL证书

本文记录如何为阿里云CentOS服务器上的网站配置免费的Let's Encrypt SSL证书,解决浏览器"不安全"警告。

环境准备

  • 操作系统:CentOS 7/8

  • Web服务器:Nginx

  • 域名:已解析到服务器IP

安装步骤

1. 安装Certbot及Nginx插件

复制代码
# 安装EPEL仓库
yum install -y epel-release

# 安装Certbot和Nginx插件
yum install -y certbot python3-certbot-nginx

2. 运行Certbot配置SSL

执行以下命令开始配置:

复制代码
certbot --nginx

3. 交互式配置过程

按照提示完成以下步骤:

第1步:输入邮箱地址

text

复制代码
Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel):

输入你的邮箱地址后回车,用于接收证书续期提醒和安全通知。

第2步:同意服务条款

复制代码
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.5-February-24-2025.pdf. You must
agree in order to register with the ACME server. Do you agree?
(Y)es/(N)o:

输入 Y 回车同意服务条款。

第3步:邮箱共享选择(可选)

复制代码
Would you be willing to share your email address with the Electronic Frontier Foundation?
(Y)es/(N)o:

输入 YN 根据个人意愿选择,不影响证书申请。

第4步:选择要启用HTTPS的域名

复制代码
Which names would you like to activate HTTPS for?
1: 
2: 
3: 

Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):

直接按回车选择所有列出的域名。

第5步:设置HTTP重定向(关键步骤)

复制代码
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
1: No redirect - Make no changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access.

输入 2 回车,启用HTTP到HTTPS的强制重定向。

配置完成

看到以下信息表示配置成功:

复制代码
Congratulations! You have successfully enabled https://www.xxx.com.cn

证书文件位置:

  • 证书文件:/etc/letsencrypt/live/你的域名/fullchain.pem

  • 私钥文件:/etc/letsencrypt/live/你的域名/privkey.pem

证书自动续期

Let's Encrypt证书有效期为90天,但Certbot会自动处理续期。测试续期功能:

复制代码
certbot renew --dry-run

为新域名添加SSL

后续添加新的二级域名时:

  1. 先在Nginx中配置好新域名

  2. 运行:certbot --nginx

  3. 在域名选择步骤中选中新域名即可

常见问题

1. Nginx插件未安装

错误信息:The requested nginx plugin does not appear to be installed

解决方案:重新安装插件

复制代码
yum install -y python3-certbot-nginx

2. 证书覆盖多个域名

Certbot会自动扫描Nginx配置中的所有域名,可以一次性为多个域名配置SSL。

验证效果

配置完成后,访问你的网站:

  • http://你的域名 会自动跳转到 https://你的域名

  • 浏览器地址栏显示安全锁图标

相关推荐
Dawn-bit18 小时前
Linux日志处理三剑客之基础篇:(基础正则+扩展正则)
linux·运维·服务器·正则表达式·云计算·运维开发
深圳市爱派派智能科技有限公司18 小时前
“进迭时空 RISC-V 集群服务器 CSB1-N10SPK3:10 节点 K3,600 TOPS 绿色算力“
运维·服务器·risc-v·k3·进迭时空·集群服务器
☆凡尘清心☆18 小时前
Linux运维故障排查速查命令清单
linux·运维·服务器
QWEDDRFTG18 小时前
机房供电安全
服务器
qeen8718 小时前
【Linux】make/Makefile 自动化工具的介绍
linux·运维·服务器·自动化
艾莉丝努力练剑19 小时前
【Linux:动静态库】Linux 动静态库与可执行文件
linux·运维·服务器·学习·面试·文件系统·动静态库
殷忆枫19 小时前
Linux 4G模块驱动适配实战:从手动绑定到自动识别
linux·运维·服务器
独隅19 小时前
CLion 在 Linux 上的完整安装与配置使用指南
linux·运维·服务器·c语言·c++·ide
☆凡尘清心☆19 小时前
CentOS Stream 9 编译安装 Redis 7.2.7 完整版详细步骤
linux·redis·centos
大黄说说19 小时前
EF Core 避坑指南:查询慢、循环查询、并发更新问题如何解决
java·服务器·数据库