【问题标题】:Ubuntu 16.04 - Nginx Laravel 500 HTTP errorUbuntu 16.04 - Nginx Laravel 500 HTTP 错误
【发布时间】:2018-03-06 18:32:36
【问题描述】:

我正在尝试通过 nginx 启动一个 Laravel 项目,但我遇到了 HTTP 500 错误。我试过sudo chmod 777 -R storage 也试过755。已有用户php artisan cache:clear。这是我的配置/etc/nginx/sites-enabled/default

server {
    listen 80;
    listen [::]:80;

    server_name cbattles.com www.cbattles.com;

    root /home/enthys/workspace/CodeBattles/public;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

在我的/var/log/nginx/error.log 中出现错误

2018/03/06 20:33:16 [错误] 4090#4090: *10 FastCGI 在标准错误中发送: “PHP 消息:PHP 解析错误:语法错误,意外'?', 期望变量 (T_VARIABLE) 在 /home/enthys/workspace/CodeBattles/vendor/symfony/finder/Comparator/NumberComparator.php 在第 42 行 PHP 消息:PHP 致命错误:抛出的异常没有 第 0 行未知中的堆栈帧 PHP 消息:PHP 解析错误:语法 错误,意外的“?”,期望变量 (T_VARIABLE) 在 /home/enthys/workspace/CodeBattles/vendor/symfony/finder/Comparator/NumberComparator.php 在第 42 行 PHP 消息:PHP 致命错误:抛出的异常没有 在从第 0 行“未知”中的堆栈帧,同时从 上游,客户端:127.0.0.1,服务器:cbattles.com,请求:“GET / HTTP/1.1”,上游:“fastcgi://unix:/run/php/php7.0-fpm.sock:”,主机: “cbattles.com”

storage/logs/laravel.log 状态

[2018-03-06 18:33:16] local.ERROR: Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) at /home/enthys/workspace/CodeBattles/vendor/symfony/finder/Comparator/NumberComparator.php:42)
[stacktrace]
#0 /home/enthys/workspace/CodeBattles/vendor/composer/ClassLoader.php(301): Composer\\Autoload\\includeFile('/home/enthys/wo...')
#1 [internal function]: Composer\\Autoload\\ClassLoader->loadClass('Symfony\\\\Compone...')
#2 /home/enthys/workspace/CodeBattles/vendor/symfony/finder/Finder.php(120): spl_autoload_call('Symfony\\\\Compone...')
#3 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php(420): Symfony\\Component\\Finder\\Finder->depth(0)
#4 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(381): Illuminate\\Filesystem\\Filesystem->directories('/home/enthys/wo...')
#5 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Support/helpers.php(1038): Illuminate\\Foundation\\Exceptions\\Handler->Illuminate\\Foundation\\Exceptions\\{closure}(Object(Whoops\\Handler\\PrettyPageHandler))
#6 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(384): tap(Object(Whoops\\Handler\\PrettyPageHandler), Object(Closure))
#7 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(334): Illuminate\\Foundation\\Exceptions\\Handler->whoopsHandler()
#8 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Support/helpers.php(1038): Illuminate\\Foundation\\Exceptions\\Handler->Illuminate\\Foundation\\Exceptions\\{closure}(Object(Whoops\\Run))
#9 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(339): tap(Object(Whoops\\Run), Object(Closure))
#10 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(314): Illuminate\\Foundation\\Exceptions\\Handler->renderExceptionWithWhoops(Object(Symfony\\Component\\Debug\\Exception\\FatalThrowableError))
#11 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(287): Illuminate\\Foundation\\Exceptions\\Handler->convertExceptionToResponse(Object(Symfony\\Component\\Debug\\Exception\\FatalThrowableError))
#12 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php(188): Illuminate\\Foundation\\Exceptions\\Handler->prepareResponse(Object(Illuminate\\Http\\Request), Object(Symfony\\Component\\Debug\\Exception\\FatalThrowableError))
#13 /home/enthys/workspace/CodeBattles/app/Exceptions/Handler.php(51): Illuminate\\Foundation\\Exceptions\\Handler->render(Object(Illuminate\\Http\\Request), Object(Symfony\\Component\\Debug\\Exception\\FatalThrowableError))
#14 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(326): App\\Exceptions\\Handler->render(Object(Illuminate\\Http\\Request), Object(Symfony\\Component\\Debug\\Exception\\FatalThrowableError))
#15 /home/enthys/workspace/CodeBattles/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(124): Illuminate\\Foundation\\Http\\Kernel->renderException(Object(Illuminate\\Http\\Request), Object(Symfony\\Component\\Debug\\Exception\\FatalThrowableError))
#16 /home/enthys/workspace/CodeBattles/public/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#17 {main}
"} 
[2018-03-06 18:33:16] local.ERROR: Exception thrown without a stack frame {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 16): Exception thrown without a stack frame at Unknown:0)
[stacktrace]
#0 {main}
"} 

【问题讨论】:

  • 您的错误信息是什么? tail storage/logs/laravel.log -n 100
  • 看起来是 php 版本的问题。您正在运行的 laravel 版本是什么?可能需要 7.1 或更高版本。
  • 发现@Kamran 的问题是对的。问题出在我的 php 版本中。 Laravel 版本需要 7.1^ 而我使用的是 7.0

标签: php laravel nginx


【解决方案1】:

我遇到了同样的问题。我不确定所有细节,但您需要将 php 更新到版本 7.1+

我以这种方式在 Ubuntu 16.04 上将 php 更新为 7.2:

sudo add-apt-repository ppa:ondrej/php
sudo apt install php7.2 php7.2-common php7.2-cli php7.2-fpm

然后去掉旧版本的php

sudo apt purge php7.1*

然后通过设置更新/etc/php/7.2/cli/php.ini文件

cgi.fix_pathinfo=0

然后:

sudo systemctl restart php7.1-fpm.service

下一步是通过设置正确的 fpm sock 版本来更新 /etc/nginx/sites-available/example.com 文件:

fastcgi_pass unix:/run/php/php7.2-fpm.sock;

终于重启了nginx:

sudo systemctl restart nginx.service

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2014-08-27
    • 2017-03-27
    • 2017-03-18
    • 2018-03-21
    • 2018-02-28
    • 1970-01-01
    相关资源
    最近更新 更多