bg-tutorials

如何在CentOS上安装SSL证书

在本指南中,您将学习如何在 CentOS 及其 RHEL 系列后继版本(AlmaLinux、Rocky Linux 和 CentOS Stream)上使用 Apache httpd 网络服务器安装 SSL 证书。

我们还录制了一个视频,带您完整了解整个流程。如果您更喜欢文字版本,请继续阅读下文。


生成 CSR 代码

在申请 SSL 证书时,您首先需要生成一个 CSR(证书签名请求),并将其发送给证书颁发机构。CSR 是一段包含您的联系方式和域名信息的文本,它与同时生成的私钥配对。您有两个选择:

在结账时将 CSR 提交给证书颁发机构。一旦 CA 验证通过并颁发您的证书,请继续下面的安装步骤。

在 CentOS / RHEL(Apache)上安装 SSL 证书

版本提示:CentOS Linux 8 已于 2021 年 12 月 31 日终止支持,CentOS Linux 7 已于 2024 年 6 月 30 日终止支持。生产环境中,请改用受支持的 RHEL 系列发行版:RHEL 8/9/10、AlmaLinux、Rocky Linux 或 CentOS Stream。它们都拥有相同的 Apache httpd 布局,因此以下步骤适用于其中任何一个。

开始之前

验证通过后,证书颁发机构会通过电子邮件向您发送证书文件。请确保您已准备好以下三个独立文件:

  • yourdomain.crt,您的主 SSL 证书。
  • yourdomain.ca-bundle,中间证书(CA 捆绑包)。
  • yourdomain.key,您在生成 CSR 时创建的私钥。

重要提示:切勿将私钥打包进证书链中。私钥应保存在单独的文件中,且仅 root 用户可读取。

步骤 1:安装 TLS 支持(mod_ssl)

安装 mod_ssl,它会为 Apache 添加 TLS 支持,并在 /etc/httpd/conf.d/ssl.conf 生成默认的 TLS 配置:

sudo dnf install -y mod_ssl

在仍使用 yum 包管理器的旧系统上,请改用 sudo yum install -y mod_ssl。

步骤 2:放置您的 SSL 文件

将私钥复制到 /etc/pki/tls/private/,并锁定其权限,使其只能被 root 读取:

sudo cp yourdomain.key /etc/pki/tls/private/
sudo chown root:root /etc/pki/tls/private/yourdomain.key
sudo chmod 600 /etc/pki/tls/private/yourdomain.key

接下来,通过合并您的证书和 CA 捆绑包来构建完整链文件,并将其放置在 /etc/pki/tls/certs/ 中:

cat yourdomain.crt yourdomain.ca-bundle 
  | sudo tee /etc/pki/tls/certs/yourdomain-fullchain.crt > /dev/null

完整链文件只能包含公钥证书和中间链,绝不能包含私钥。

步骤 3:配置 Apache TLS 虚拟主机

您可以编辑 mod_ssl 创建的默认 /etc/httpd/conf.d/ssl.conf,或者(更简洁的做法)为您的站点创建专用文件,例如 /etc/httpd/conf.d/yourdomain.conf。用文本编辑器打开它:

sudo nano /etc/httpd/conf.d/yourdomain.conf

添加一个 80 端口的虚拟主机,将所有流量重定向到 HTTPS,随后添加包含 SSL 指令的安全端口 443 虚拟主机:

<VirtualHost *:80>
  ServerName yourdomain.com
  ServerAlias www.yourdomain.com
  Redirect permanent / https://yourdomain.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName yourdomain.com
  ServerAlias www.yourdomain.com
  DocumentRoot /var/www/yourdomain.com

  SSLEngine on
  SSLCertificateFile    /etc/pki/tls/certs/yourdomain-fullchain.crt
  SSLCertificateKeyFile /etc/pki/tls/private/yourdomain.key
</VirtualHost>
  • SSLCertificateFile,您的完整链文件(证书 + 中间证书)。在 Apache 2.4.8 及更高版本中,这一条指令替代了已弃用的 SSLCertificateChainFile。
  • SSLCertificateKeyFile,您在生成 CSR 时创建的私钥。

关于默认主机的说明:mod_ssl 的 /etc/httpd/conf.d/ssl.conf 自带一个使用自签名证书的 <VirtualHost _default_:443>。当您像上面那样添加针对特定站点的 *:443 主机时,匹配您 ServerName 的请求将使用您的证书,但未匹配的 HTTPS 请求(例如通过 IP 地址访问服务器)仍会回退到该默认自签名主机。如果您不希望出现这种回退情况,请注释掉 ssl.conf 中的 <VirtualHost _default_:443> 块,或将其指向您自己的证书。

关于 TLS 强化的说明:RHEL 8 及更高版本上的 Apache 遵循系统级加密策略,该策略已默认禁用过时协议。要为整个服务器设置策略,请运行 sudo update-crypto-policies --set DEFAULT(或使用 FUTURE 以获得更严格的设置)。如果您希望针对每个虚拟主机单独固定协议,请在 443 端口块内添加 SSLProtocol -all +TLSv1.2 +TLSv1.3。

步骤 4:测试您的 Apache 配置

在重新加载之前请务必检查语法,因为一个小小的拼写错误就可能导致网站离线:

sudo apachectl configtest

您应该会看到 Syntax OK。(命令 sudo httpd -t 效果相同。)继续之前请修复任何报告的错误。

步骤 5:重启 Apache

通过重新加载 httpd 服务应用更改(重新加载可在不中断现有连接的情况下激活新配置):

sudo systemctl reload httpd

如果 httpd 尚未运行,请使用 sudo systemctl enable --now httpd 启动它。此时您的网站应该已经通过 HTTPS 上线。要验证结果并获得即时状态报告,请使用我们的SSL Checker。

旧版 CentOS(7 及更早版本)注意事项

CentOS Linux 7、6 和 8 均已终止支持,不再接收安全更新,因此请尽快迁移到受支持的 RHEL 系列发行版。如果您必须在旧版服务器上操作,流程与上述相同,只有两点不同:

  • 包管理器:CentOS 7 和 6 使用 yum 而非 dnf。使用 rpm -qa | grep mod_ssl 检查 mod_ssl 是否已安装,如果没有,请使用 sudo yum install -y mod_ssl 进行安装。
  • Apache 版本低于 2.4.8:只有非常旧的版本才早于 2.4.8。在这种情况下,请将证书和证书链保存在单独的文件中,并添加第三条指令 SSLCertificateChainFile,指向CA 捆绑包(而非您的证书):
SSLCertificateFile      /etc/pki/tls/certs/yourdomain.crt
SSLCertificateKeyFile   /etc/pki/tls/private/yourdomain.key
SSLCertificateChainFile /etc/pki/tls/certs/yourdomain.ca-bundle

在 Apache 2.4.8 及更高版本(涵盖所有当前受支持的版本)上,请改用步骤 3 中展示的更简单的单一完整链 SSLCertificateFile。

在哪里购买适用于 CentOS 的 SSL 证书?

购买适用于您 CentOS 服务器的 SSL 证书的最佳地点是像 SSL Dragon 这样值得信赖的 SSL 经销商。我们的价格是市场上最低的之一,我们在整个 SSL 产品系列中提供定期折扣和优惠活动,并配有出色的客户支持。我们所有的证书均与 CentOS 及其 RHEL 系列后继版本兼容。

SSL Dragon 会为您保护敏感数据的安全,让您的网站或业务能够在网络上蓬勃发展。

常见问题

如何知道 CentOS 上是否已安装 SSL 证书?

使用 OpenSSL 连接到您的站点并读取其提供的证书:
echo | openssl s_client -connect yourdomain.com:443 -servername yourdomain.com 2>/dev/null | openssl x509 -noout -issuer -dates
如果已安装证书,该命令将输出其颁发者和有效期。若要检查磁盘上的证书文件,请运行 openssl x509 -text -noout -in certificate.crt。您也可以使用 sudo apachectl -S 列出 Apache 的活动虚拟主机,或在浏览器中打开您的网站并查看挂锁图标。

CentOS 中 SSL 证书存储在哪里?

按照惯例,证书存放在 /etc/pki/tls/certs/,私钥存放在 /etc/pki/tls/private/。这些是 RHEL 系列的默认位置;只要您的虚拟主机指向正确的路径,您也可以使用其他位置。

CentOS 上 Apache 的 SSL 配置位于何处?

TLS 模块会在 /etc/httpd/conf.d/ssl.conf 安装其默认配置,主 Apache 配置文件为 /etc/httpd/conf/httpd.conf。您可以将虚拟主机添加到 ssl.conf 中,但创建一个单独的站点专用文件(例如 /etc/httpd/conf.d/yourdomain.conf)会更清晰。/etc/httpd/conf.d/ 中的任何 .conf 文件都会被自动加载。Apache 日志位于 /var/log/httpd/。

在 CentOS 上,我需要合并证书和 CA 捆绑包吗?

在 Apache 2.4.8 及更高版本(所有当前受支持的 RHEL 系列版本)上,是的:将您的证书和 CA 捆绑包合并为一个完整链文件,并将 SSLCertificateFile 指向该文件。单独的 SSLCertificateChainFile 指令已被弃用,仅在早于 2.4.8 的 Apache 版本中才需要使用。

如何在 CentOS 上安装 OpenSSL?

OpenSSL 在 CentOS 及其他所有主流 Linux 发行版中都是预装的,因此通常无需另行安装。如果缺失,可使用 sudo dnf install -y openssl(在 CentOS 7 及更早版本上使用 sudo yum install -y openssl)进行安装。了解更多关于OpenSSL 及其命令行的信息。

总结

在 CentOS 或 RHEL 上使用 Apache 安装 SSL 证书,归根结底就是:安装 mod_ssl,将您的私钥和完整链证书放置在 /etc/pki/tls/ 下,在 /etc/httpd/conf.d/ 中配置 <VirtualHost *:443> 块,使用 sudo apachectl configtest 进行测试,并使用 sudo systemctl reload httpd 重新加载。还没有证书?浏览我们的 SSL 证书。

立即订购 SSL 证书, 可节省 10% 的费用!

快速发行, 强大加密, 99.99% 的浏览器信任度, 专业支持和 25 天退款保证. 优惠券代码 SAVE10

龙飞行的详细图像
撰写人

I've been building and managing websites for over 20 years, with a heavy focus on the technical side of the cybersecurity, VPN, and SaaS industries. I know how sites are built from the ground up, which means I know how to secure them. Here at SSL Dragon, I write about web architecture, encryption, and keeping your infrastructure safe.

Avatar of Sergiu Rosca
Technical Review by Sergiu Rosca

Sergiu Rosca is the core web developer behind SSL Dragon. He manages the technical infrastructure, platform performance, and backend integrations that keep the site running smoothly and securely. At SSL Dragon, Sergiu shares practical insights on web development, site optimization, and technical troubleshooting.

All SSL Dragon installation guides are tested on live server environments and undergo a strict peer-review process to ensure your infrastructure remains secure. Read our full Editorial Policy.