How to change main ( native ) php ( used by Magento 2 for Cli commands in ssh ) version in linux OS ( Debian, Centos, RedHat etc ... ) for only one Magento 2 installation on your server?
Magento 2 using for operations in ssh mode cli ( Command Line Interface ) commands, so magento call php using one word: php ( as you can see in /bin/magento file )
In some cases you wont use main php version and want using alternate php version ( other than native ) for only one your Magento 2 installation and keep all other OS settings as is.
You can do easy by changing only first line of one file ( <magento root>/bin/magento ) and your Magento 2 cli commands will be sent to other php version!
Here is original version of /bin/magento file ( Magento 2.3.5 ):
#!/usr/bin/env php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
if (PHP_SAPI !== 'cli') {
echo 'bin/magento must be run as a CLI application';
exit(1);
}
try {
require __DIR__ . '/../app/bootstrap.php';
} catch (\Exception $e) {
echo 'Autoload error: ' . $e->getMessage();
exit(1);
}
try {
$handler = new \Magento\Framework\App\ErrorHandler();
set_error_handler([$handler, 'handler']);
$application = new Magento\Framework\Console\Cli('Magento CLI');
$application->run();
} catch (\Exception $e) {
while ($e) {
echo $e->getMessage();
echo $e->getTraceAsString();
echo "\n\n";
$e = $e->getPrevious();
}
exit(Magento\Framework\Console\Cli::RETURN_FAILURE);
}
Just Replace like this ( only first line ):
#!/usr/bin/env /path/to/your/alternate/php/version/binary/
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
if (PHP_SAPI !== 'cli') {
echo 'bin/magento must be run as a CLI application';
exit(1);
}
try {
require __DIR__ . '/../app/bootstrap.php';
} catch (\Exception $e) {
echo 'Autoload error: ' . $e->getMessage();
exit(1);
}
try {
$handler = new \Magento\Framework\App\ErrorHandler();
set_error_handler([$handler, 'handler']);
$application = new Magento\Framework\Console\Cli('Magento CLI');
$application->run();
} catch (\Exception $e) {
while ($e) {
echo $e->getMessage();
echo $e->getTraceAsString();
echo "\n\n";
$e = $e->getPrevious();
}
exit(Magento\Framework\Console\Cli::RETURN_FAILURE);
}
Example of the tested first line for Debian ( of the /bin/magento file ):
#!/usr/bin/env /opt/php73/bin/php
As you can see there is full path to the binary file of alternate php version ( main php version of OS in example php7.4 ) changed. So every Cli command of Magento 2 will be sent to the other php version than native and even than VirtualHost settings says.
Want to say thanks:
https://goaskle.com/en/donation/