CentOs下Apache服务器配置SSL证书
封面图
•
2025-10-20
•
Linux
307
1
1
0
在 CentOS 系统下配置 Apache 服务器使用 SSL 证书(如 Let's Encrypt 证书),可以按照以下步骤进行操作。这里以 **Let's Encrypt** 的免费 SSL 证书为例,使用 **Certbot** 工具自动获取和安装证书。 --- ## ✅ 前提条件 1. **CentOS 7 或更高版本**(推荐使用 CentOS 7) 2. **Apache 已安装并运行** 3. **域名已解析到服务器 IP** 4. **防火墙允许 HTTPS(443)端口** --- ## 📌 步骤一:安装 EPEL 和 Certbot ```bash sudo yum install -y epel-release sudo yum install -y certbot python2-certbot-apache ``` > 如果你使用的是 CentOS 8 或更高版本,可能需要使用 `dnf` 替代 `yum`,并且 Certbot 可能已经包含在默认仓库中。 --- ## 📌 步骤二:获取 SSL 证书 ```bash sudo certbot --apache ``` 运行后,Certbot 会提示你: - 输入你的邮箱(用于通知) - 同意服务条款 - 选择是否要强制重定向 HTTP 到 HTTPS(推荐选 Yes) 然后它会自动检测 Apache 配置,并为你生成证书。 --- ## 📌 步骤三:验证证书 证书生成后,Certbot 会修改 Apache 的虚拟主机配置文件,通常位于: ``` /etc/httpd/conf.d/your-site.conf ``` 或者: ``` /etc/apache2/sites-available/your-site.conf ``` (根据你的 Apache 安装方式而定) 检查是否有如下内容: ```apache <IfModule mod_ssl.c> <VirtualHost *:443> ServerName yourdomain.com SSLEngine on SSLCertificateFile "/etc/letsencrypt/live/yourdomain.com/fullchain.pem" SSLCertificateKeyFile "/etc/letsencrypt/live/yourdomain.com/privkey.pem" ... </VirtualHost> </IfModule> ``` --- ## 📌 步骤四:重启 Apache 服务 ```bash sudo systemctl restart httpd ``` 或: ```bash sudo systemctl restart apache2 ``` --- ## 📌 步骤五:测试 HTTPS 访问 打开浏览器访问: ``` https://yourdomain.com ``` 你应该能看到绿色的 HTTPS 标志。 --- ## 📌 步骤六:设置自动续期(可选但推荐) Let's Encrypt 证书有效期为 90 天,Certbot 提供了自动续期功能: ```bash sudo certbot renew --dry-run ``` 如果测试成功,你可以设置定时任务: ```bash sudo crontab -e ``` 添加以下行: ```cron 0 0,12 * * * /usr/bin/certbot renew --quiet ``` 这会在每天的 0 点和 12 点尝试自动更新证书。 --- ## 📌 注意事项 - 如果你使用的是 Nginx 而不是 Apache,请使用 `certbot --nginx`。 - 如果你在配置过程中遇到问题,可以查看日志: ```bash tail -f /var/log/letsencrypt/letsencrypt.log ``` --- ## ✅ 总结 | 步骤 | 内容 | |------|------| | 1 | 安装 Certbot | | 2 | 获取 SSL 证书 | | 3 | 配置 Apache | | 4 | 重启 Apache | | 5 | 测试 HTTPS | | 6 | 设置自动续期 | 如果你有其他类型的 SSL 证书(如购买的商业证书),也可以手动配置,步骤类似,只需将证书路径替换为你的证书文件路径即可。 --- 如有具体问题(如证书路径不对、无法访问等),欢迎继续提问!
上一篇:
linux history命令详解,如何在history命令时查看历史命令执行时间?
下一篇:
4款简单常见的纯CSS表格(table)样式
标题录入,一次不能超过6条
留言
评论