CentOS Certbot 自动化配置指南

简介
CentOS 是一款广泛使用的开源操作系统,而 Certbot 则是一款用于自动化获取和安装 SSL/TLS 证书的工具,本文将介绍如何在 CentOS 系统上使用 Certbot 自动化配置 SSL/TLS 证书。
安装 Certbot
使用 yum 安装 Certbot 和 Nginx 插件:
sudo yum install certbot python2-certbot-nginx
启动 Nginx 服务:
sudo systemctl start nginx
使 Nginx 服务开机自启:
sudo systemctl enable nginx
配置 Certbot
创建 Nginx 配置文件:

sudo nano /etc/nginx/sites-available/example.com
编辑配置文件,添加以下内容:
server {
listen 80;
server_name example.com www.example.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# Redirect all HTTP requests to HTTPS
return 301 https://$server_name$request_uri;
} 创建一个符号链接,将配置文件链接到 Nginx 的 sites-enabled 目录:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
重启 Nginx 服务以应用更改:
sudo systemctl restart nginx
获取 SSL/TLS 证书
运行 Certbot 获取证书:
sudo certbot --nginx -d example.com -d www.example.com
根据提示完成证书的获取过程。
验证证书
查看证书文件:

sudo ls -l /etc/letsencrypt/live/example.com/
使用 openssl 命令验证证书的有效性:
sudo openssl x509 -noout -text -in /etc/letsencrypt/live/example.com/fullchain.pem
FAQs
问题:如何查看 Certbot 的日志?
解答: Certbot 的日志通常存储在
/var/log/目录下,可以使用以下命令查看日志:sudo less /var/log/letsencrypt.log
问题:如何更新 SSL/TLS 证书?
解答: 更新 SSL/TLS 证书非常简单,只需再次运行 Certbot 的命令即可:
sudo certbot renew --nginx -d example.com -d www.example.com

