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天,务必配置自动续期以避免证书过期。

相关推荐
未济1 天前
linux 配置环境变量
linux
傲世仙尊1 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
_艾伦 耶格尔.1 天前
进程间通信
linux
Liuqy-051 天前
Linux IO编程——静态库、动态库
linux
彧azz1 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅1 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK1 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid
琥珀色糖1 天前
SimlpeHttp
linux·服务器
qeen871 天前
【Linux】操作系统之进程介绍(二)
linux·笔记·学习·进程
Zenova EdgeOS1 天前
Linux ss 工业边缘网络分析实战
linux·python·边缘计算·工业网关