ubuntu

Ubuntu PHP配置SSL证书怎么办

小樊
43
2025-09-02 19:05:10
栏目: 云计算

在Ubuntu上为PHP配置SSL证书,主要步骤如下:

  1. 安装Certbot获取证书

    • 以Nginx为例,安装Certbot工具:
      sudo apt update && sudo apt install certbot python3-certbot-nginx
    • 运行Certbot获取证书(替换为你的域名):
      sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
      证书会自动配置到Nginx。
  2. 配置PHP启用SSL

    • 编辑PHP配置文件(如/etc/php/7.4/fpm/php.ini):
      确保extension=openssl已取消注释,启用OpenSSL扩展。
    • 若需强制HTTPS,可在代码中添加重定向逻辑(可选):
      if ($_SERVER['HTTPS'] != "on") {  
          header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);  
          exit;  
      }  
      
  3. 重启服务生效

    • 重启Nginx和PHP-FPM:
      sudo systemctl restart nginx && sudo systemctl restart php7.4-fpm
  4. 验证配置

    • 浏览器访问https://yourdomain.com,查看是否显示安全锁图标。
    • 使用命令检查证书状态:
      sudo certbot renew --dry-run(测试自动续期)。

说明

0
看了该问题的人还看了