Laravel 9 Composer 检测到 PHP 版本问题

问题描述 投票:0回答:1

我看到其他人问过这个问题,但似乎没有一个解决方案对我和其他人有用

规格

  • 适用于 Laravel 8 的 PHP 7.4
  • 适用于 Laravel 9 的 PHP 8.2
  • Ubuntu 服务器 18.4
  • Nginx 版本 1.14.0 (Ubuntu)
  • 作曲家 2.5.4

我的
composer.json
档案

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": "^8.0.2",
        "ext-imagick": "*",
        "ext-json": "*",
        "ext-openssl": "*",
        "defuse/php-encryption": "^2.2",
        "doctrine/dbal": "^2.8",
        "fideloper/proxy": "^4.4",
        "guzzlehttp/guzzle": "^7.2",
        "laravel/framework": "^9.19",
        "laravel/helpers": "*",
        "laravel/installer": "^4.4",
        "laravel/tinker": "^2.7",
        "laravel/ui": "^3.0",
        "nategood/httpful": "^0.2.20",
        "laravel/sanctum": "^3.0"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "laravel/pint": "^1.0",
        "laravel/sail": "^1.0.1",
        "fakerphp/faker": "^1.9.1",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^6.1",
        "phpunit/phpunit": "^9.5.10",
        "spatie/laravel-ignition": "^1.0",
        "pusher/pusher-php-server": "^5.0"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

从8升级到9没问题,但是在我的网站上,出现如下错误:

Composer 在您的平台中检测到问题:您的 Composer 依赖项需要 PHP 版本“>= 8.1.0”。

最后,我希望我的项目使用 PHP 8.2 在 Laravel 9 上运行

我试过的

加入

composer.json

{
    "config": {
        "platform-check": false
    }
}

{
    "config": {
        "platform": {
            "php": "7.4.33"
        }
    }
}

我也尝试用

sudo update-alternatives --config php
切换PHP版本。

我也改了

/etc/nginx/sites-available/YOURSITENAME
文件:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

fastcgi_cache_path /var/nginx/cache levels=1:2 keys_zone=MYAPP:10m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

server {
    server_tokens off;
    listen 443 ssl  http2;
    listen [::]:443 ssl  http2;
    ssl_certificate /etc/letsencrypt/live/CENSORED/fullchain.pem; # managed by Certbot 
    ssl_trusted_certificate /etc/letsencrypt/live/CENSORED/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/CENSORED/privkey.pem; # managed by Certbot 

    server_name CENSORED;

    root /var/www/kfz/public/;
    index index.php;

    error_page 403 404 /404.html;

    location = /404.html {
        internal; #return 404
    }

    index index.php index.html index.htm;

    location ~ /\. {
        access_log off;
        log_not_found off;
        deny all;
        return 404;
    
    }

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

    location ~ \.php$ {
        # regex to split $uri to $fastcgi_script_name and $fastcgi_path
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        # Check that the PHP script exists before passing it
        try_files $uri =404;

        # Bypass the fact that try_files resets $fastcgi_path_info
        # see: http://trac.nginx.org/nginx/ticket/321
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;

        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_cache MYAPP;
        fastcgi_cache_valid 200 30s;
    }
}

我只换了线

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

所有的解决方案都不适合我,所以我检查了 composers usage 中的 PHP 版本:

composer -vvv about 2>&1 | grep "PHP"
.

输出:

在 Linux / 4.15.0-206-generic 上使用 PHP 8.2.3 运行 2.5.4 (2023-02-15 13:10:06)

Composer - PHP 依赖管理器 - 版本 2.5.4

然后我决定使用以下命令删除 PHP 7.4:

sudo apt-get purge 'php7.4*'
.

然后报错:

Composer 在您的平台中检测到问题:您的 Composer 依赖项需要 PHP 版本“>= 8.1.0”。

不见了,但后来我有了

500 SERVER ERROR
.

所以对我来说,错误的解决方案是删除旧的 PHP 版本,无论作曲家拥有什么 PHP 版本。

因此请确保您没有安装或启用比 8.1 更早的 PHP 版本。

我会尽快发布错误 500 的解决方案。或者也许有人有想法。

php laravel nginx composer-php ubuntu-18.04
1个回答
1
投票

问题解决了。

/var/log/nginx
是来自nginx的日志。没有问题。

但是在我的 Laravel 项目中

/storage/app/logs
是一个有以下错误的日志:

未定义常量 Illuminate\Http\Request::HEADER_X_FORWARDED_ALL

快速搜索,这是一个Trusted Proxies错误.

所以这是我所有的改变:

app/Http/Middleware/TrustProxies.php
更新
use Fideloper\Proxy\TrustProxies as Middleware
use Illuminate\Http\Middleware\TrustProxies as Middleware
.

接下来我更新了

$headers
属性定义:

来自

//before
protected $headers = Request::HEADER_X_FORWARDED_ALL;

//after
protected $headers =
    Request::HEADER_X_FORWARDED_FOR |
    Request::HEADER_X_FORWARDED_HOST |
    Request::HEADER_X_FORWARDED_PORT |
    Request::HEADER_X_FORWARDED_PROTO |
    Request::HEADER_X_FORWARDED_AWS_ELB;

然后你必须从你的应用程序中删除

fideloper/proxy
Composer 依赖:

composer remove fideloper/proxy

效果很好!

也可以在 Laravel upgrade 8 to 9 文档中阅读。

文档链接:https://laravel.com/docs/9.x/upgrade?ref=hackernoon.com

我希望我能帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.