简体   繁体   中英

Laravel Artisan PHP version error in 1and1 server

I have a server hosted on 1and1 and I am using Laravel. When I want to execute the Artisan command to schedule a tasks, I get this error:

$ php artisan schedule:run

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /htdocs/artisan on line 31

Parse error: syntax error, unexpected T_STRING in /htdocs/artisan on line 31

After a lot searches, nothing solved my issue (do an alias for PHP, call $ php5.5 instead $ php , etc.).

The main problem is that a call to php uses version 4.4.9 of PHP, instead 5.5 that Laravel needs.

$ php -v 
PHP 4.4.9 (cgi-fcgi) (built: Mar 31 2016 16:41:29)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

$ php5.5 -v 
PHP 5.5.35 (cgi-fcgi) (built: May  3 2016 07:09:03)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

I changed the call to php5.5 and altered the Artisan file, calling this on the first line:

#!/usr/local/bin/php5.5
<?php

But at the end I always get this from artisan module calls:

Running scheduled command: '/usr/local/bin/php' 'artisan' moneySaved:send >> './logs/log.log' 2>&1 &

So the problem must come from who generates those "Running scheduled command" lines.

The problem is that when running scheduled commands, Laravel uses Symfony's "PhpExecutableFinder" to identify and locate the path to the PHP binary to use to run the scheduled commands.

And depending on the context from which artisan schedule:run is called, PhpExecutableFinder will not return the path to the correct PHP binary.

However, in the current version of PhpExecutableFinder there is an if clause which checks whether PHP_PATH is defined in the environment. If so and set to an executable path, it is returned.

So I added export PHP_PATH=/usr/local/bin/php5.5; right before the call to artisan schedule:run in the crontab:

* * * * * export PHP_PATH=/usr/bin/php8.0; $PHP_PATH artisan schedule:run >> /dev/null 2>&1

and instead of the default (and wrong) /usr/bin/php , the correct /usr/bin/php8.0 was used to run the scheduled commands.

This fixed the problem I had with running FreeScout on IONOS hosted webspace (see also this FreeScout issue ).

After a research, the problem was in the way that Symfony internal scripts "locate" the php path to call it. Specifically those:

epoc/vendor/symfony/process/PhpExecutableFinder.php epoc/vendor/laravel/framework/src/Illuminate/Console/Scheduling/Schedule.php

The binary var holds the path to call PHP. In my case I forced it to use the 1and1 path for php5.5, and that's all.

public function command($command, array $parameters = [])    {
    //$binary = ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));
    $binary = "/usr/local/bin/php5.5";​
    if (defined('HHVM_VERSION')) {
        $binary .= ' --php';
    }​
    if (defined('ARTISAN_BINARY')) {
        $artisan = ProcessUtils::escapeArgument(ARTISAN_BINARY);
    } else {
        $artisan = 'artisan';
    }​
    return $this->exec("{$binary} {$artisan} {$command}", $parameters);
}

Now it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM