Skip to content

Apache

适用环境

  • Apache 服务器
  • Phpstudy
  • Wamp
  • Lamp

准备工作

在安装 SSL 证书之前,请确保:

  1. 已经获取了 SSL 证书文件
  2. Apache 已正确安装并可以正常访问
  3. 已安装 mod_ssl 模块
  4. 确保 Apache 版本兼容性

配置示例

apache
<VirtualHost *:443>
    ServerName yourdomain.com
    DocumentRoot /var/www/html
    SSLEngine on
    SSLCertificateFile /path/to/yourdomain.com.crt
    SSLCertificateKeyFile /path/to/yourdomain.com.key
    SSLCertificateChainFile /path/to/yourdomain.com-ca-bundle.crt
</VirtualHost>

安装步骤

1. 准备证书文件

Apache 格式的证书解压后应包含三个文件:

txt
domain.com.crt    (域名证书,也可能是pem格式)
domain.com.key    (私钥文件)
domain.com-ca-bundle.crt (根证书链,也可能是其他文件名,一般带有 ca 或者 chain)

2. 放置证书文件

将证书文件放在 apache 的 conf/ssl 目录下,如果 conf 目录里没有 ssl 目录,可以新建一个。 放在 conf/ssl 可以用相对路径,其他位置需要用绝对路径。

3. 配置虚拟主机

找到站点的 80 端口配置,一般在 conf/vhosts.conf 文件里(有的是在 conf/extra/httpd-vhosts.conf),例如:

apache
<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /www/wwwroot
    #其他的配置信息···
</VirtualHost>

4. 配置 HTTPS 虚拟主机

在 vhosts.conf 同级目录里,新建一个 vhosts-https.conf 文件,复制 80 端口配置过来进行修改,添加 ssl 配置:

apache
<VirtualHost *:443>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /www/wwwroot
    #不同证书文件名和后缀可能不同  修改文件名为apache的3个实际文件名即可
    SSLEngine on
    SSLCertificateFile      conf/ssl/yourdomain.com.crt
    SSLCertificateKeyFile   conf/ssl/yourdomain.com.key
    SSLCertificateChainFile conf/ssl/yourdomain.com-ca-bundle.crt
    #其他的配置信息···
</VirtualHost>

5. 修改主配置文件

打开 conf 目录里的 httpd.conf 文件,找到 Include conf/vhosts.conf 在下面添加:

apache
#这一行一般在文件后面,从后面更容易找到
Include conf/vhosts.conf
#下面是添加的
Include conf/vhosts-https.conf
Listen 443
SSLCipherSuite ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE
SSLProtocol All -SSLv2 -SSLv3
SSLHonorCipherOrder On

6. 检查 SSL 模块

Linux 系统检查方法

使用以下命令检查 mod_ssl.so 模块:

bash
find / -name mod_ssl.so | grep modules/mod_ssl.so

如果没有找到,可以安装:

  • CentOS/Redhat:
bash
yum install mod_ssl
  • Debian/Ubuntu:
bash
sudo a2enmod ssl
sudo service apache2 restart

检查 httpd.conf 文件是否加载 ssl 模块:

apache
LoadModule ssl_module modules/mod_ssl.so  #如果前面有#号要去掉

7. 重启 Apache

配置完成后重启 Apache 使其生效:

bash
# Linux
service httpd restart
# 或
apachectl restart

# Windows
net stop apache
net start apache

8. 测试配置

测试 https 是否可以访问

常见问题

  1. Apache 无法启动

    • 检查是否加载了 mod_ssl.so 模块
    • 检查证书文件路径是否正确
    • 检查证书文件权限是否正确
  2. HTTPS 访问失败

    • 检查 443 端口是否开放
    • 检查证书是否过期
    • 检查证书域名是否匹配
  3. 证书链不完整

    • 确保 SSLCertificateChainFile 指向正确的证书链文件
    • 确保证书链文件包含完整的证书链

SSL 证书帮助中心