ssl-guides 2026-12-02 ⏱ 1 min read

如何在 Apache 上安装免费 SSL

如何在 Apache 上安装免费 SSL

Apache 加 certbot 是目前最顺畅的免费 SSL 方案之一。Certbot 的 Apache 插件会从 Let's Encrypt 获取证书、重写你的 VirtualHost 配置并设置自动续期。在已经运行的站点上,整个过程大约五分钟。

前提条件

  • 一个域名的 DNS A 记录指向你服务器的公网 IP。
  • Apache 已在 80 端口提供该域名,因为 certbot 默认通过 HTTP 验证。
  • 防火墙和任何云安全组中开放 80 和 443 端口。
  • 服务器上的 sudo 权限。

第 1 步:安装 certbot

在 Ubuntu 或 Debian 上:

sudo apt update
sudo apt install certbot python3-certbot-apache

在 AlmaLinux、Rocky 等基于 RHEL 的系统上,先启用 EPEL 仓库,然后用 dnf 安装相同的软件包。

Apache 免费 SSL,第 2 步:获取并安装证书

sudo certbot --apache -d example.com -d www.example.com

Certbot 会要求填写邮箱(Let's Encrypt 已于 2025 年 6 月 4 日停止发送到期提醒,所以不要指望它),让你接受服务条款,然后获取证书并自动更新 Apache 配置。询问是否把 HTTP 重定向到 HTTPS 时,选择重定向,让访客总是落到安全版本。如有提示,重新加载 Apache:

sudo systemctl reload apache2

Apache 免费 SSL,第 3 步:确认自动续期

Certbot 会安装一个 systemd 定时器,在证书到期前续期,通常每天检查两次,并续期所有到期的证书。自 Certbot 4.0.0 起,这意味着剩余有效期不足三分之一,即 90 天证书为 30 天(Certbot 文档)。在不真正续期的情况下测试续期路径:

sudo certbot renew --dry-run

出现成功提示意味着续期将无人值守运行。如需 deploy hook 等更深入的自动化选项,请参阅 用 certbot 自动化 SSL 续期.

在 Apache 上手动配置免费 SSL:自己编写 VirtualHost

如果你从其他服务商或其他机器上取得证书,就手动配置 Apache。启用 SSL 模块和你的 SSL 站点,然后重新加载:

sudo a2enmod ssl
sudo a2ensite example-ssl
sudo systemctl reload apache2

一个最小的 SSL VirtualHost 如下所示:

<VirtualHost *:443>
  ServerName example.com
  DocumentRoot /var/www/example
  SSLEngine on
  SSLCertificateFile /path/to/cert.pem
  SSLCertificateKeyFile /path/to/privkey.pem
  SSLCertificateChainFile /path/to/chain.pem
</VirtualHost>

把这三个路径改成证书文件实际所在的位置;由 certbot 管理的证书位于 /etc/letsencrypt/live/example.com/ 下。如果手动安装后浏览器报证书链或信任错误,ChainFile 那一行是首先要检查的,我们的 修复 SSL 证书链错误 指南涵盖了常见原因。

测试结果

打开 https://yourdomain.com,确认锁标志没有警告,然后加载纯 HTTP 地址,确认它会跳转。最后用外部扫描工具检查完整证书链和到期日期,不要只信自己浏览器的缓存。当 Apache 在 VPS 上与 nginx 同时运行时,同样的 certbot 流程也适用;这种部署方式请参阅 在 nginx 和 Apache VPS 服务器上安装免费 SSL 。

当 certbot 无法修改你的配置时

高度定制的 Apache 设置,例如一个文件里有许多 vhost,或由面板生成的配置,有时会让插件困惑。这种情况下,用 certonly 子命令配合 webroot 或 standalone 验证器运行 certbot,然后按上面的手动方法把证书写进你的 VirtualHost。你既保留了 certbot 的续期自动化,又完全掌控配置。

检查你的 SSL 评级

一切正常后,用外部 SSL 测试扫描站点,查看协议和加密套件的强度。certbot 写入的默认值很稳妥,但较旧的 Apache 构建有时仍允许 TLS 1.0 或 1.1;禁用它们只需改两行,在任何公开站点上都值得做。

长期保持配置整洁

Certbot 会写自己的 SSL vhost 文件,文件名通常带 le-ssl。不要动它的证书行,因为续期更新的是底层文件,不是配置。如果以后你重新整理 Apache 配置,让三个证书路径继续指向 /etc/letsencrypt/live/,续期才能继续正常工作。切断这个联系,是最常见的让一个原本健康的 Apache 站点在几个月后突然提供过期证书的原因。

延伸阅读: 在 Nginx 上安装免费 SSL 和 在 Ubuntu 上安装免费 SSL.

常见问题

如果 Certbot 无法修改我的 Apache 配置怎么办?

运行 certbot certonly 并配合 webroot 或 standalone 验证器,然后手动把证书路径加入你的 VirtualHost。你仍保留 Certbot 的续期自动化。

续期后需要重新加载 Apache 吗?

需要。续期后的证书在 Apache 重新加载之前不会生效。一个执行 systemctl reload apache2 的 deploy hook 就能解决。

如何确认续期会生效?

运行 sudo certbot renew --dry-run。自 Certbot 4.0.0 起,剩余有效期不足三分之一时就会续期(Certbot 文档).

🔒 Test Your Website SSL Certificate Now
Run Free SSL Check →

✅ Post-Installation Verification & SEO Checklist

After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:

  • Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
  • Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
  • Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
  • Confirm both root and www versions resolve cleanly to one canonical HTTPS URL.
  • Verify your HTTPS property and sitemap.xml inside Google Search Console and Bing Webmaster Tools.
Technically Reviewed by TryFreeSSL Security Team
Last updated: 2026-12-05
About Us →

免费 SSL 证书签发、在线 SSL 检测与 CSR 生成器

无需复杂服务器配置,60 秒快速获取 Let's Encrypt 与 Buypass 信任的 HTTPS 证书。

Related Articles

🤖 TryFreeSSL AI Assistant