You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu24.04下Nginx+PHP8.3+Laravel11出现502错误求助

问题:Laravel 11.2项目部署后Nginx返回502 Bad Gateway错误

环境信息

  • Ubuntu 24.04 LTS
  • Nginx 1.24.0
  • PHP 8.3.6
  • Laravel 11.2项目,虚拟主机域名telegrambot.test,监听82端口

访问http://telegrambot.test:82时返回502 Bad Gateway错误,已完成以下排查:

  1. 修正Nginx配置中的fastcgi_pass,从PHP 7.4 FPM套接字改为PHP 8.3的unix:/var/run/php/php8.3-fpm.sock
  2. 确认Nginx和PHP-FPM服务均正常运行
  3. 单独部署的phpinfo()脚本可正常执行,说明PHP-FPM与Nginx的基础通信无问题

相关配置及日志

Nginx虚拟主机配置

server {
    listen 82;
    server_name telegrambot.test;
    root /var/www/telegrambot/public;

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

/etc/hosts配置

127.0.0.1   telegrambot.test

Nginx错误日志片段

2024/xx/xx xx:xx:xx [error] 1234#1234: *5 connect() to unix:/var/run/php/php8.3-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: telegrambot.test, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.3-fpm.sock:", host: "telegrambot.test:82"

排查建议

  • 检查PHP-FPM套接字权限
    查看套接字的所有者与权限:

    ls -l /var/run/php/php8.3-fpm.sock
    

    该套接字需对Nginx运行用户(通常为www-data)开放权限,可修改PHP-FPM配置文件/etc/php/8.3/fpm/pool.d/www.conf:

    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0660
    

    修改后重启PHP-FPM:

    sudo systemctl restart php8.3-fpm
    
  • 修复Laravel目录权限
    Laravel的storage和bootstrap/cache目录需要可写权限,执行以下命令:

    sudo chown -R www-data:www-data /var/www/telegrambot/storage /var/www/telegrambot/bootstrap/cache
    sudo chmod -R 775 /var/www/telegrambot/storage /var/www/telegrambot/bootstrap/cache
    
  • 验证Nginx配置并重启
    检查配置文件语法:

    sudo nginx -t
    

    无错误则重启Nginx:

    sudo systemctl restart nginx
    
  • 查看PHP-FPM日志
    检查PHP-FPM错误日志(路径通常为/var/log/php8.3-fpm.log),排查执行层面的问题:

    tail -f /var/log/php8.3-fpm.log
    
  • 重置Laravel缓存
    确保.env文件中APP_URL设置为http://telegrambot.test:82,然后清除缓存:

    cd /var/www/telegrambot
    php artisan config:clear
    php artisan cache:clear
    

内容的提问来源于stack exchange,提问作者Amir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 01:03:23