Certbot工具在CentOS 7.9上申请和配置SSL证书完整教程

1. 环境准备与依赖安装

1.1 系统更新

首先确保系统处于最新状态:

bash 复制代码
sudo yum update -y

1.2 安装必要依赖

安装Certbot所需的依赖包:

bash 复制代码
sudo yum install -y epel-release

2. Certbot工具安装

2.1 安装Certbot

根据您的Web服务器类型选择相应的安装命令:

使用Nginx服务器:

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

使用Apache服务器:

bash 复制代码
sudo yum install -y certbot python3-certbot-apache

2.2 验证安装

检查Certbot版本以确认安装成功:

bash 复制代码
certbot --version

3. SSL证书申请

3.1 基于Web服务器的自动配置

使用Nginx服务器:

bash 复制代码
sudo certbot --nginx

使用Apache服务器:

bash 复制代码
sudo certbot --apache

3.2 手动申请证书

3.2.1 standalone方式

使用内置服务器验证域名:

bash 复制代码
sudo certbot certonly --standalone -d example.com -d www.example.com -m your@email.com
3.2.2 webroot方式

使用Web服务器的根目录验证域名(不需要停止Web服务器):

bash 复制代码
sudo certbot certonly --webroot -w /path/to/webroot -d example.com -d www.example.com -m your@email.com

3.3 命令参数说明

  • certonly:仅获取证书,不自动配置
  • --standalone:使用内置服务器验证域名
  • --webroot:使用Web服务器根目录验证域名
  • -w:指定Web服务器根目录路径
  • -d:指定域名,可多次使用添加多个域名
  • -m:指定邮箱地址,用于证书到期通知

3.4 其他常用命令

检查证书:

bash 复制代码
sudo certbot certificates

删除证书:

bash 复制代码
sudo certbot delete --cert-name example.com

证书验证:

bash 复制代码
openssl s_client -connect example.com:443 -servername example.com < /dev/null

4. 证书自动续期配置

4.1 创建自动续期脚本

bash 复制代码
sudo crontab -e

添加以下内容(每隔10天的凌晨3点运行续期):

bash 复制代码
0 3 */10 * * /usr/bin/certbot renew --quiet

4.2 测试续期功能

bash 复制代码
# 测试续期(使用standalone方式时需要关闭Web服务器)
sudo certbot renew --dry-run

4.3 手动续期

bash 复制代码
# 手动续期(使用standalone方式时需要关闭Web服务器)
sudo certbot renew --quit

5. 常见问题排查与解决方案

5.1 端口80被占用

问题:申请证书时提示端口80被占用

解决方案

  • 临时停止占用端口80的服务
  • 使用--http-01-port参数指定其他端口

5.2 域名解析问题

问题:无法验证域名所有权

解决方案

  • 确保域名A记录正确指向服务器IP
  • 等待DNS解析生效(通常需要10-15分钟)

5.3 权限问题

问题:证书文件权限不足

解决方案

  • 确保Web服务器用户有读取证书目录的权限
  • 使用sudo执行Certbot命令

6. 证书配置示例

6.1 Nginx配置示例

nginx 复制代码
server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # 其他配置...
}

6.2 Apache配置示例

apache 复制代码
<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

    # 其他配置...
</VirtualHost>

7. 操作截图

7.1 安装Certbot截图

7.2 申请证书截图

8. 总结

本教程详细介绍了在CentOS 7.9服务器上使用Certbot工具申请和配置SSL证书的完整流程,包括环境准备、工具安装、证书申请、自动续期配置以及常见问题排查。按照本教程操作,您可以为您的网站轻松配置免费的SSL证书,提升网站安全性和用户信任度。

注意:Let's Encrypt证书有效期为90天,务必配置自动续期以避免证书过期。

相关推荐
zhangrelay2 小时前
三分钟云课实践速通--大学物理--python 版
linux·开发语言·python·学习·ubuntu·lubuntu
风翼靓崽3 小时前
linux命令杂记 - 杂乱无章
linux·运维·服务器
handler013 小时前
Linux 进程探索:从 PCB 管理到 fork() 的写时拷贝
linux·c语言·c++·笔记·学习
域中四大3 小时前
rk3568中修改波特率
linux·运维
风曦Kisaki3 小时前
# Linux Shell 编程入门 Day01:Shell 基础认知、脚本编写规范、变量四大类型、数值运算
linux·运维·chrome
pray~4 小时前
海外Linux Debian环境临时安装依赖包
linux·运维·debian
y_m_h4 小时前
程序调用追踪
linux
代码中介商5 小时前
Linux 进程间通信(IPC):管道与信号量完全指南
linux·运维·服务器
张青贤5 小时前
linux离线部署docker和docker-compose
linux·docker·docker-compose