PHP项目怎样配置HTTPS证书?

wen PHP项目 11

本文目录导读:

PHP项目怎样配置HTTPS证书?

  1. 第一步:获取SSL证书
  2. 第二步:将证书部署到Web服务器
  3. 第三步:高级功能与验证

为PHP项目配置HTTPS证书,核心步骤分为获取证书在Web服务器(如Nginx、Apache)中配置两部分。

以下是详细的操作流程,涵盖了从证书获取到最终配置的全过程。

第一步:获取SSL证书

你需要一个证书文件(.crt或.pem)和一个私钥文件(.key)。

  1. 购买商业证书:从CA机构(如Symantec、GlobalSign、Let's Encrypt等)购买,适用于企业级应用。
  2. 使用免费证书
    • Let's Encrypt (推荐):免费、自动续期,使用工具Certbot即可快速获取。
    • 阿里云/腾讯云等云服务商:提供免费的一年期DV证书(单域名)。
    • 自签名证书:仅用于本地开发环境,生产环境浏览器会提示不安全。

第二步:将证书部署到Web服务器

你的PHP项目是通过Web服务器(Nginx或Apache)运行的,所以需要在这两个软件里配置证书。

使用 Nginx (最常见)

  1. 上传证书文件: 将your_domain.pem(或.crt)和your_domain.key文件上传到服务器,例如放到/etc/nginx/ssl/目录下。

  2. 修改Nginx配置: 编辑你的站点配置文件(通常在 /etc/nginx/sites-available/your_domain/usr/local/nginx/conf/nginx.conf)。

    server {
        # 强制HTTP跳转到HTTPS (可选但强烈建议)
        listen 80;
        server_name your_domain.com www.your_domain.com;
        return 301 https://$server_name$request_uri;
    }
    server {
        # HTTPS 监听端口
        listen 443 ssl http2;  # 开启HTTP/2可以提升性能
        server_name your_domain.com www.your_domain.com;
        # 证书文件路径 (指向你上传的文件)
        ssl_certificate /etc/nginx/ssl/your_domain.pem;  # 或 .crt
        ssl_certificate_key /etc/nginx/ssl/your_domain.key;
        # 安全配置 (可选但推荐)
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_ciphers HIGH:!aNULL:!MD5;
        # 你的PHP项目根目录
        root /var/www/your_project/public;  # 指向你的入口目录
        index index.php index.html;
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
        # PHP-FPM 解析配置(确保支持PHP)
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;  # 根据实际PHP版本调整
        }
        # 其他location配置...
    }
  3. 重启Nginx

    nginx -t  # 测试配置文件语法是否正确
    systemctl restart nginx  # 或 service nginx restart

使用 Apache

  1. 上传证书文件: 同样将.crt.key文件上传到服务器,例如/etc/apache2/ssl/

  2. 启用SSL模块

    a2enmod ssl
    systemctl restart apache2
  3. 修改Apache配置 (编辑 /etc/apache2/sites-available/your_domain.conf):

    <VirtualHost *:80>
        ServerName your_domain.com
        # 强制跳转HTTPS
        Redirect permanent / https://your_domain.com/
    </VirtualHost>
    <VirtualHost *:443>
        ServerAdmin webmaster@your_domain.com
        ServerName your_domain.com
        DocumentRoot /var/www/your_project/public
        # SSL配置
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/your_domain.crt
        SSLCertificateKeyFile /etc/apache2/ssl/your_domain.key
        # 安全协议
        SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
        SSLCipherSuite HIGH:!aNULL:!MD5
        # PHP目录权限等...
        <Directory /var/www/your_project/public>
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
  4. 重启Apache

    a2ensite your_domain.conf  # 启用站点
    systemctl restart apache2

第三步:高级功能与验证

自动续期 (Let's Encrypt)

如果使用Let's Encrypt证书,务必配置自动续期(证书有效期90天)。

# 使用Certbot工具
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
# 测试续期是否正常
sudo certbot renew --dry-run

检查HTTP链接是否被强制跳转

PHP代码中所有内部链接(如header('Location: ...')或页面中的资源引用)应该使用相对路径或https://

推荐做法: 在PHP项目的根目录.env或配置文件中定义APP_URL

APP_URL=https://your_domain.com

然后在代码中始终使用APP_URL来拼接完整URL,而不是硬编码http://

配置HSTS (HTTP Strict Transport Security)

在Nginx或Apache配置中添加响应头,告诉浏览器始终使用HTTPS访问。

  • Nginx
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
  • Apache
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"

最终验证

  • 浏览器测试:访问 https://your_domain.com,查看地址栏是否有锁标志。
  • 在线工具:使用 SSL Labs 测试证书配置等级(A评级为最佳)。
  • PHP代码检测
    // 在入口文件或中间件中添加检查
    if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') {
        // 如果不是HTTPS,且请求来自用户浏览器,可以强制跳转
        if (PHP_SAPI !== 'cli') {
            header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
            exit;
        }
    }
  1. 获取证书:免费证书用Certbot(Let's Encrypt),商业证书无所谓,流程一样。
  2. 放入服务器:文件位置随意,但路径要有正确的读取权限。
  3. 配置Nginx/Apache:监听443端口,指定ssl_certificatessl_certificate_key文件路径。
  4. 强制HTTPS:在80端口的监听块中设置301重定向到443。
  5. 重启服务nginx -t && systemctl restart nginxapachectl configtest && systemctl restart apache2
  6. 代码适配:确保PHP中所有硬编码的http://链接改为https://或使用相对路径。

完成以上步骤,你的PHP项目就能安全地通过HTTPS访问了。

抱歉,评论功能暂时关闭!