申请SSL证书
证书申请或续期(使用certbot)
查看nginx状态
nginx -t
关闭nginx
nginx -s stop
查看https证书剩余时间及存放位置
certbot certificates
申请(必须先关闭nginx,释放8080端口,否则报错)
certbot certonly –standalone –email *@qq.com -d xxx.cszy.top
续期或新增(必须先关闭nginx)
certbot certonly -d xxx.cszy.top
配置nginx
进入 sites-available,新建xxx.cszy.top配置文件,详情见>>nginx入门<<
进入sites-enabled目录,执行命令
1 | |
启动nginx
nginx
或重启nginx
nginx -s reload
常用的命令
certbot certificates #可以查看证书的状态
certbot renow #证书到期30天前可以自动更新
certbot revoke #撤销证书
certbot delete #撤销后删除证书
官方教程(ubuntu)
SSH into the server
SSH into the server running your HTTP website as a user with sudo privileges.
Install snapd
You’ll need to install snapd and make sure you follow any instructions to enable classic snap support.
Follow these instructions on snapcraft’s site to install snapd.
Remove certbot-auto and any Certbot OS packages
If you have any Certbot packages installed using an OS package manager like
apt,dnf, oryum, you should remove them before installing the Certbot snap to ensure that when you run the commandcertbotthe snap is used rather than the installation from your OS package manager. The exact command to do this depends on your OS, but common examples aresudo apt-get remove certbot,sudo dnf remove certbot, orsudo yum remove certbot.Install Certbot
Run this command on the command line on the machine to install Certbot.
sudo snap install –classic certbot
Prepare the Certbot command
Execute the following instruction on the command line on the machine to ensure that the
certbotcommand can be run.sudo ln -s /snap/bin/certbot /usr/bin/certbot
Choose how you’d like to run Certbot
Either get and install your certificates…
Run this command to get a certificate and have Certbot edit your nginx configuration automatically to serve it, turning on HTTPS access in a single step.
sudo certbot –nginx
Or, just get a certificate
If you’re feeling more conservative and would like to make the changes to your nginx configuration by hand, run this command.
sudo certbot certonly –nginx
Test automatic renewal
The Certbot packages on your system come with a cron job or systemd timer that will renew your certificates automatically before they expire. You will not need to run Certbot again, unless you change your configuration. You can test automatic renewal for your certificates by running this command:
sudo certbot renew –dry-run
The command to renew certbot is installed in one of the following locations:
/etc/crontab//etc/cron.*/*systemctl list-timers
Confirm that Certbot worked
To confirm that your site is set up properly, visit
https://yourwebsite.com/in your browser and look for the lock icon in the URL bar.
成功后证书地址:
Certificate is saved at: /etc/letsencrypt/live/cszy.top/fullchain.pem
Key is saved at: /etc/letsencrypt/live/cszy.top/privkey.pem
安装工具(旧版,不推荐)
apt install certbot python3-certbot-nginx
然后就是生成证书并自动配置nginx,在这个步骤前建议先备份一下旧的nginx配置文件。
certbot –nginx -d domain.com
参考资料
Certbot Instructions | Certbot (eff.org)
https://zhuanlan.zhihu.com/p/354241539
https://zhuanlan.zhihu.com/p/442904647
CentOS 安装 Certbot 完整教程
Certbot 是 Let’s Encrypt 免费 SSL 证书的官方自动化管理工具,以下提供官方推荐的全版本通用 Snap 安装方式,以及适配不同 CentOS 版本的原生包安装方式,覆盖证书申请、自动续期全流程。
一、前置准备
- 拥有服务器
root或sudo权限 - 待申请证书的域名已正确解析到服务器公网 IP
- 服务器防火墙/安全组已开放 80(HTTP) 和 443(HTTPS) 端口
- 提前卸载系统中旧版本 Certbot,避免版本冲突
1
2
3
4
5
6
7# 卸载旧版本(yum/dnf 安装的)
sudo yum remove certbot -y
sudo dnf remove certbot -y
# 若曾使用 certbot-auto 脚本,执行删除
sudo rm -rf /usr/local/bin/certbot-auto
sudo rm -rf /opt/eff.org/certbot
二、官方推荐:Snap 安装方式(全版本通用)
该方式为 Certbot 官方首推,适配 CentOS 7/8/9/Stream 全系列,能保证获取最新稳定版,彻底解决依赖兼容问题。
步骤1:安装并启用 Snapd
1 | |
执行完上述命令后,建议重新登录 SSH 终端,确保 snap 环境变量生效。
步骤2:更新 Snap 核心组件
1 | |
步骤3:安装 Certbot
1 | |
步骤4:验证安装
1 | |
输出版本号(如 certbot 2.30.0)即代表安装成功。
三、备选方案:YUM/DNF 原生包安装
若不想使用 Snap,可通过系统原生包管理器安装,需按 CentOS 版本选择对应命令。
注意:原生包版本通常滞后于 Snap 版,部分旧系统可能存在依赖兼容问题。
CentOS 7
1 | |
CentOS 8 / Stream 8
1 | |
CentOS 9 / Stream 9
1 | |
四、证书申请与使用
安装完成后,可根据需求选择以下两种常用模式申请证书,默认使用 HTTP-01 验证方式,需保证80端口可正常访问。
模式1:一键自动配置(推荐新手)
该模式会自动识别 Web 服务配置,自动生成 SSL 配置并重载服务,一步完成 HTTPS 部署。
1 | |
执行后按提示完成:填写邮箱、同意服务协议、选择是否重定向 HTTP 到 HTTPS,完成后证书自动部署生效。
模式2:仅申请证书(手动配置)
适合需要自定义配置、不希望 Certbot 自动修改 Web 配置的场景,仅生成证书文件。
1 | |
证书默认存储路径
申请成功后,证书文件会保存在 /etc/letsencrypt/live/你的域名.com/ 目录下,核心文件:
- 证书链:
fullchain.pem - 私钥:
privkey.pem
可在 Nginx/Apache 配置中直接引用该路径,不要手动移动或修改文件权限。
五、自动续期配置
Let’s Encrypt 证书有效期为 90 天,Certbot 安装后会自动创建系统定时任务,无需手动配置,到期前会自动续期。
验证自动续期是否正常
1 | |
手动续期命令
1 | |
六、常见问题排查
- 80/443 端口占用导致验证失败:先停止占用端口的服务(如 Nginx、Apache),使用
--standalone模式申请,或更换--webroot模式。 - Snap 命令找不到:重新登录 SSH 终端,或执行
source /etc/profile刷新环境变量。 - 域名验证失败:确认域名已正确解析到当前服务器 IP,且国内服务器需完成域名备案,否则可能被拦截导致验证失败。
- 防火墙拦截:执行以下命令放行端口并重载防火墙:
1
2
3sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --add-port=443/tcp --permanent
sudo firewall-cmd --reload