问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Windows系统使用phpstudy安装SSL证书

创作时间:
作者:
@小白创作中心

Windows系统使用phpstudy安装SSL证书

引用
CSDN
1.
https://m.blog.csdn.net/2409_89014517/article/details/144310572

在 Windows 系统中使用phpstudy安装 SSL 证书,可以使网站支持 HTTPS 协议,提升安全性。以下是详细的安装和配置步骤:

一、准备工作

  1. 安装 phpstudy
  • 确保已安装 phpstudy 并正确运行(可在其面板中启动 Apache 或 Nginx 环境)。
  1. 获取 SSL 证书文件
  • 如果您没有 SSL 证书,可以通过以下方式获取:
  • 免费证书:使用Let's Encrypt或ZeroSSL申请免费证书。
  • 付费证书:从正规证书提供商如 DigiCert、GlobalSign 或阿里云、腾讯云购买。
  • 通常,证书包含以下文件:
  • certificate.crt:证书文件。
  • private.key:私钥文件。
  • ca_bundle.crt:中间证书文件(部分证书提供)。
  1. 选择 Web 服务环境
  • phpstudy 支持 Apache 和 Nginx,两种环境的 SSL 配置稍有不同,请根据实际情况选择配置。

二、配置 SSL 证书(以 Apache 为例)

1. 确认证书文件存放路径

将证书文件上传到服务器并放置在一个固定目录中,例如:

D:\phpstudy_pro\Extensions\apache\conf\ssl\

将以下文件放入该目录:

  • certificate.crt(证书文件)
  • private.key(私钥文件)
  • ca_bundle.crt(中间证书,若有)

文件结构示例:

D:\phpstudy_pro\Extensions\apache\conf\ssl\certificate.crt
D:\phpstudy_pro\Extensions\apache\conf\ssl\private.key
D:\phpstudy_pro\Extensions\apache\conf\ssl\ca_bundle.crt

2. 修改 Apache 配置文件

  1. 打开 phpstudy,找到 Apache 的配置文件路径:
  • 默认路径:
    D:\phpstudy_pro\Extensions\apache\conf\httpd.conf
    
  1. 确保以下模块已启用(如果未启用,请取消注释):

    LoadModule ssl_module modules/mod_ssl.so
    LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
    
  2. 在 Apache 配置文件中启用 SSL 支持:

  • 找到并修改以下配置项:
    Include conf/extra/httpd-ssl.conf
    
  • 如果这一行被注释掉,请将注释(#)去掉。
  1. 编辑 httpd-ssl.conf 文件:
  • 默认路径:

    D:\phpstudy_pro\Extensions\apache\conf\extra\httpd-ssl.conf
    
  • 配置内容示例:

    <VirtualHost *:443>
        DocumentRoot "D:/phpstudy_pro/WWW/your-website"
        ServerName www.example.com
        SSLEngine on
        SSLCertificateFile "D:/phpstudy_pro/Extensions/apache/conf/ssl/certificate.crt"
        SSLCertificateKeyFile "D:/phpstudy_pro/Extensions/apache/conf/ssl/private.key"
        SSLCertificateChainFile "D:/phpstudy_pro/Extensions/apache/conf/ssl/ca_bundle.crt"
        <Directory "D:/phpstudy_pro/WWW/your-website">
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    

    配置说明:

  • DocumentRoot:网站的根目录。

  • ServerName:您的域名,如 www.example.com

  • SSLCertificateFile:证书文件路径。

  • SSLCertificateKeyFile:私钥文件路径。

  • SSLCertificateChainFile:中间证书路径(如果没有,可以忽略)。

3. 修改默认 HTTP 配置(可选)

为了强制将 HTTP 重定向到 HTTPS,可以在主配置文件中添加以下规则:

<VirtualHost *:80>
    DocumentRoot "D:/phpstudy_pro/WWW/your-website"
    ServerName www.example.com
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>

4. 重启 Apache 服务

  • 在 phpstudy 面板中,找到 Apache,点击重启,使配置生效。
  • 如果重启后 Apache 无法启动,请检查配置文件是否有语法错误。

三、配置 SSL 证书(以 Nginx 为例)

1. 确认证书文件存放路径

将证书文件和私钥文件放置在 Nginx 的适当目录中,例如:

D:\phpstudy_pro\Extensions\nginx\conf\ssl\

文件结构示例:

D:\phpstudy_pro\Extensions\nginx\conf\ssl\certificate.crt
D:\phpstudy_pro\Extensions\nginx\conf\ssl\private.key

2. 修改 Nginx 配置文件

  1. 打开 phpstudy,找到 Nginx 的配置文件路径:
  • 默认路径:
    D:\phpstudy_pro\Extensions\nginx\conf\nginx.conf
    
  1. 编辑配置文件,添加以下内容到您的站点配置中:

    server {
        listen 80;
        server_name www.example.com;
        # 重定向到 HTTPS
        return 301 https://$host$request_uri;
    }
    server {
        listen 443 ssl;
        server_name www.example.com;
        root D:/phpstudy_pro/WWW/your-website;
        ssl_certificate     D:/phpstudy_pro/Extensions/nginx/conf/ssl/certificate.crt;
        ssl_certificate_key D:/phpstudy_pro/Extensions/nginx/conf/ssl/private.key;
        ssl_session_cache   shared:SSL:10m;
        ssl_session_timeout 10m;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        location / {
            index index.php index.html;
            try_files $uri $uri/ /index.php?$query_string;
        }
        # PHP 处理
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
    

    配置说明:

  • listen 80:监听 HTTP 端口并重定向到 HTTPS。
  • listen 443 ssl:启用 SSL 并监听 HTTPS 端口。
  • ssl_certificatessl_certificate_key 分别指定证书和私钥文件路径。

3. 测试配置并重启 Nginx

  1. 测试配置是否正确:

    nginx -t
    

    如果配置正确,会显示 syntax is oktest is successful

  2. 在 phpstudy 面板中,重启 Nginx 服务使配置生效。

四、验证 SSL 证书安装是否成功

  1. 浏览器访问
  • 在浏览器中访问 https://your-domain.com,检查是否可以正常加载。
  • 确保地址栏中显示安全锁标志。
  1. 在线工具检查
  • 使用 SSL 检测工具验证证书是否正确安装:
  • SSL Labs
  • WhyNoPadlock
  1. 命令行验证
  • 使用命令检查证书:
    openssl s_client -connect your-domain.com:443
    

五、常见问题及解决办法

  1. Apache/Nginx 无法启动
  • 检查 SSL 配置文件路径是否正确。
  • 确保证书和私钥文件匹配。
  1. 证书无效或不信任
  • 确保中间证书已正确配置(Apache 的 SSLCertificateChainFile 或 Nginx 的 ssl_certificate)。
  • 如果使用自签名证书,请在客户端导入证书。
  1. HTTP 未自动跳转至 HTTPS
  • 检查是否正确配置了 HTTP 到 HTTPS 的重定向规则。

通过以上步骤,您可以在 Windows 系统中使用 phpstudy 成功安装和配置 SSL 证书,使网站支持 HTTPS 协议。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号