Laravel 9 Composer检测到PHP版本存在问题

kx5bkwkv  于 2023-03-19  发布在  PHP
关注(0)|答案(1)|浏览(153)

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

规格
  • 用于Laravel 8的PHP 7.4
  • 用于Laravel 9的PHP 8.2
  • Ubuntu服务器18.4
  • Nginx版本1.14.0(Ubuntu)
  • composer 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”。
最后,我希望我的项目能在Laravel 9和PHP 8.2上运行

我所尝试的

加入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
所有的解决方案并不适合我,所以我检查了PHP版本的 composer 使用:composer -vvv about 2>&1 | grep "PHP" .
输出:
在Linux / 4.15.0-206上运行2.5.4(2023年2月15日13:10:06)和PHP 8.2.3-通用
编写器-PHP依赖关系管理器-版本2.5.4
然后我决定使用以下命令删除PHP 7.4:sudo apt-get purge 'php7.4*' .
然后错误:
Composer在您的平台中检测到问题:您的Composer依赖项需要PHP版本“〉= 8.1.0”。
已经没有了,但后来我有了500 SERVER ERROR
所以对我来说,这个错误的解决方案是删除旧的PHP版本,无论 composer 有什么PHP版本。
因此,请确保您没有安装或启用比8.1更旧的PHP版本。
我会尽快发布错误500的解决方案。或者也许有人有主意。

46scxncf

46scxncf1#

问题已解决。

/var/log/nginx中的日志来自nginx。没有问题。
但在我的Laravel项目/storage/app/logs是一个日志与以下错误:
未定义的常量照明\Http\请求::HEADER_X_FORWARDED_ALL
快速搜索,这是可信代理错误
我的改动如下:
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升级8至9文档中阅读。
我希望我能帮助别人。

相关问题