bg-tutorials

如何在 Linux 中安装 SSL 证书

在本教程中,您将学习如何在运行 Apache(httpd)Web 服务器的 Linux 上安装 SSL 证书。

示例使用红帽企业版 Linux(RHEL)及其近亲系统(Rocky Linux、AlmaLinux、CentOS、Fedora),在这些系统中,Apache 对应的软件包是 httpd,配置文件位于 /etc/httpd/ 目录下。最后一节还提供了适用于 Red Hat Linux 服务器的 SSL 购买建议。

在 Linux 上生成 CSR

如果您已经生成了 CSR 并收到了证书,可直接跳转到安装部分。

CSR(证书签名请求)是一段包含您域名信息的编码文本块,对于商业证书,还包含您组织的信息。生成 CSR 是 SSL 订购流程中的必需步骤;每个商业证书颁发机构在签发证书之前都会要求提供 CSR。您有两种选择:

在结账过程中将 CSR 提交给证书颁发机构。CA 验证通过并签发您的 SSL 证书后,继续进行下面的安装步骤。

在 Linux(Red Hat / Apache)上安装 SSL 证书

验证通过后,证书颁发机构会向您发送包含安装文件的 ZIP 压缩包。下载并解压后,您应该会得到以下文件:

  • yourdomain.crt,您的主 SSL 证书(叶证书)。
  • yourdomain.ca-bundle,中间证书,也称为CA 证书包。
  • yourdomain.key,与 CSR 一起生成的私钥。

如果您的 CA 是以文本形式而非文件形式发送证书的,请打开主证书,复制包括 —–BEGIN CERTIFICATE—– 和 —–END CERTIFICATE—– 行的全部内容,并将其保存为 yourdomain.crt。对 CA 证书包执行相同的操作。按照以下步骤完成安装。

第 1 步:安装 SSL 模块(mod_ssl)

在基于 Red Hat 的系统上,HTTPS 支持来自 mod_ssl 软件包。安装该软件包(在 RHEL 8/9 和 Fedora 上使用 dnf;在较旧版本上使用 yum):

sudo dnf install mod_ssl

安装该软件包还会在 /etc/httpd/conf.d/ssl.conf 处创建默认的 SSL 配置文件,您将在这里添加证书相关的指令。

第 2 步:将证书文件上传到服务器

将文件复制到标准的 Red Hat 证书存放位置。证书存放在 /etc/pki/tls/certs/ 中,私钥存放在 /etc/pki/tls/private/ 中:

sudo cp yourdomain.crt /etc/pki/tls/certs/
sudo cp yourdomain.ca-bundle /etc/pki/tls/certs/
sudo cp yourdomain.key /etc/pki/tls/private/

锁定私钥,使其只能由 root 用户读取:

sudo chmod 600 /etc/pki/tls/private/yourdomain.key

在 Apache 2.4.8 及更新版本上(涵盖所有受支持的 RHEL 版本),将证书和 CA 证书包合并为一个完整链文件(叶证书在前,中间证书在后)。这一个文件就足以满足 SSLCertificateFile 指令的需求:

cat yourdomain.crt yourdomain.ca-bundle > yourdomain_fullchain.crt
sudo cp yourdomain_fullchain.crt /etc/pki/tls/certs/

第 3 步:配置 HTTPS VirtualHost

使用文本编辑器打开 SSL 配置文件:

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

在端口 443 的 VirtualHost 块中,设置如下这样一份完整的现代配置。请根据您的文件调整路径和域名:

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

    SSLEngine on

    # Apache 2.4.8+ : certificate + CA bundle in one full-chain file
    SSLCertificateFile    /etc/pki/tls/certs/yourdomain_fullchain.crt
    SSLCertificateKeyFile /etc/pki/tls/private/yourdomain.key

    # Recommended TLS hardening (2026)
    SSLProtocol         -all +TLSv1.2 +TLSv1.3
    SSLHonorCipherOrder off

    # Enable HSTS only after you confirm HTTPS works
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</VirtualHost>
  • SSLCertificateFile:您的完整链文件(证书后接中间证书)。在 Apache 2.4.8+ 中,这一个指令取代了已废弃的 SSLCertificateChainFile。
  • SSLCertificateKeyFile:您在生成 CSR 时创建的私钥。

旧版 Apache(早于 2.4.8):如果您使用的是非常老旧的服务器,请将证书和证书链分开保存,将 SSLCertificateFile 指向 yourdomain.crt,并添加 SSLCertificateChainFile /etc/pki/tls/certs/yourdomain.ca-bundle,该指令指向的是 CA 证书包,而不是您自己的证书。

第 4 步:将 HTTP 重定向到 HTTPS

为确保访问者始终访问网站的安全版本,请添加一个端口 80 的 VirtualHost(在主站点配置文件中,例如 /etc/httpd/conf.d/yourdomain.conf),将其重定向到 HTTPS:

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

第 5 步:测试配置

重新加载前务必先测试语法;配置文件中的一个拼写错误就可能导致 Apache 无法启动:

sudo apachectl configtest

您应该会看到 Syntax OK。如果出现错误,请返回检查之前的步骤后再继续。

第 6 步:重新加载 Apache

通过重新加载 httpd 服务来应用更改。重新加载会激活新配置,同时不会中断现有连接:

sudo systemctl reload httpd
sudo systemctl start httpd
sudo systemctl enable httpd

还有一点:确保防火墙允许端口 443 上的 HTTPS 流量:

sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

恭喜!您的 SSL 证书现已成功安装在 Red Hat Linux Apache 服务器上。

为确认一切正常运作并获取即时状态报告,请使用我们的SSL Checker对您的域名进行检测。该扫描工具能标记出可能影响浏览器信任您证书的任何证书链或配置错误。

立即订购 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.