在当今互联网环境中,网站安全至关重要。使用SSL/TLS证书启用HTTPS不仅能保护用户数据,还能提升搜索引擎排名。本文将详细讲解如何在CentOS系统上为常见的Web服务器(如Apache和Nginx)配置SSL证书,即使你是Linux新手也能轻松上手。
在开始之前,请确保你已满足以下条件:
Let's Encrypt 是一个免费、自动化、开放的证书颁发机构。我们使用 Certbot 工具来申请证书。
对于 CentOS 7/8/9,执行以下命令:
# CentOS 7sudo yum install -y epel-releasesudo yum install -y certbot# CentOS 8/9sudo dnf install -y epel-releasesudo dnf install -y certbot 假设你的域名为 example.com,且Web服务已停止(或使用webroot方式):
# 临时停止Web服务(以Apache为例)sudo systemctl stop httpd# 获取证书sudo certbot certonly --standalone -d example.com -d www.example.com 成功后,证书将保存在 /etc/letsencrypt/live/example.com/ 目录下。
如果你使用的是 Apache,请按以下步骤操作。
sudo yum install -y mod_ssl # CentOS 7sudo dnf install -y mod_ssl # CentOS 8/9 编辑 SSL 配置文件(通常位于 /etc/httpd/conf.d/ssl.conf),或创建新的虚拟主机配置:
<VirtualHost *:443> ServerName example.com DocumentRoot /var/www/html SSLEngine on SSLCertificateFile "/etc/letsencrypt/live/example.com/fullchain.pem" SSLCertificateKeyFile "/etc/letsencrypt/live/example.com/privkey.pem" <Directory "/var/www/html"> AllowOverride All Require all granted </Directory></VirtualHost> sudo systemctl restart httpd 如果你使用的是 Nginx,请参考以下步骤。
通常配置文件位于 /etc/nginx/conf.d/default.conf 或 /etc/nginx/sites-available/:
server { listen 443 ssl; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; root /usr/share/nginx/html; index index.html; location / { try_files $uri $uri/ =404; }} sudo nginx -t # 测试配置是否正确sudo systemctl reload nginx # 重载配置 Let's Encrypt 证书有效期为90天,建议设置自动续期。Certbot 已自带定时任务:
# 测试续期命令sudo certbot renew --dry-run# 添加到 crontab(每天凌晨2点检查)echo "0 2 * * * /usr/bin/certbot renew --quiet" | sudo crontab - 通过本教程,你应该已经成功在 CentOS 服务器上完成了 SSL证书配置,无论是 Apache 还是 Nginx。启用 HTTPS 不仅能提升网站安全性,也是现代 Web 开发的基本要求。记住定期检查证书状态,并确保自动续期机制正常工作。
关键词回顾:
本文由主机测评网于2025-12-25发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/20251212486.html