Apache虚拟主机SSL配置核心是自动申请+正确加载,需确保SSL模块启用、443端口开放、DNS解析正常;推荐Certbot自动部署并强制HTTPS跳转,手动配置则需严格设置证书路径与安全协议。

为 Apache 虚拟主机申请并配置 SSL 证书,核心是「自动申请 + 正确加载」,不是单纯放几个文件就完事。关键在于 Certbot 能识别你的虚拟主机,且 Apache 已准备好接受 HTTPS 流量。
确认 Apache 已就绪并开放 443 端口
这是 HTTPS 能通的前提,很多失败都卡在这步:
- 运行 sudo a2enmod ssl(Ubuntu/Debian)或检查 /etc/httpd/conf.modules.d/00-ssl.conf 是否启用(CentOS/RHEL)
- 执行 apache2ctl -M | grep ssl 或 httpd -M | grep ssl,确保输出含 ssl_module (shared)
- 检查防火墙:Ubuntu 用 sudo ufw allow 443,CentOS 用 sudo firewall-cmd --permanent --add-port=443/tcp 并重载
- 确认 DNS 已生效,且域名能正常通过 HTTP(
http://yourdomain.com)访问到该虚拟主机
用 Certbot 自动申请并部署(推荐)
这是最可靠、最省心的方式,Certbot 会自动处理证书获取、配置修改和重定向:
- 安装 Certbot 及 Apache 插件:sudo apt install certbot python3-certbot-apache -y(Ubuntu)
- 运行命令指定域名:sudo certbot --apache -d example.com -d www.example.com
- 过程中选择「2」启用 HTTP → HTTPS 强制跳转,Certbot 会自动在
:80虚拟主机中添加Redirect permanent / https://example.com/ - 成功后证书默认存放在 /etc/letsencrypt/live/example.com/,Certbot 已写入
<VirtualHost *:443>配置并启用 SSL
手动配置已有证书(如商业证书)
适用于从 DigiCert、腾讯云等平台购买的证书,需自行组织文件路径与指令:
- 把三个文件放进安全目录,例如:
/etc/ssl/certs/example.com.crt(域名证书)
/etc/ssl/private/example.com.key(私钥,权限设为600)
/etc/ssl/certs/chain.crt(中间证书链,部分厂商提供1_root_bundle.crt) - 编辑对应站点的虚拟主机配置(如
/etc/apache2/sites-enabled/example.com.conf),新增一个<VirtualHost *:443>块: - 必须包含以下三行(路径严格对应):
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/private/example.com.key
SSLCertificateChainFile /etc/ssl/certs/chain.crt - 加上 SSLEngine on 和 SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1(禁用老旧协议)
验证与维护要点
配置完不等于万事大吉,还需验证是否真正生效:
- 测试配置语法:sudo apache2ctl configtest(Ubuntu)或 sudo httpd -t(CentOS),无报错再重启
- 重启服务:sudo systemctl restart apache2 或 sudo systemctl restart httpd
- 浏览器访问
https://yourdomain.com,看地址栏是否显示锁图标,点击可查看证书详情 - 测试自动续期:sudo certbot renew --dry-run,成功说明定时任务已就位;Let’s Encrypt 证书 90 天过期,Certbot 默认每天尝试续期


















