Use different PHP version CLI executable for one command

醉酒当歌 提交于 2019-11-28 18:13:07

Maybe you can try to fix the environnement!

$ php -v
PHP 5.4.x (cli) ...
$ set PATH="/usr/lib64/php5.6/bin:$PATH"
$ php -v
PHP 5.6.x (cli) ...

Or, if you don't want to modify the PATH for your shell session, you can scope the change for the current command only:

$ php -v
PHP 5.4.x (cli) ...
$ env PATH="/usr/lib64/php5.6/bin:$PATH" php -v
PHP 5.6.x (cli) ...
$ php -v
PHP 5.4.x (cli) ...

Default PHP executable can be found using:

$ which php

In most cases it is link to particular PHP version:

lrwxrwxrwx 1 root root      21 aug 15  2016 /usr/bin/php -> /usr/bin/php7.1

To change it to different version just relink it to another

$ sudo rm /usr/bin/php

$ sudo ln -s /usr/bin/php5.6 /usr/bin/php

Before relink you have to make sure target PHP version is installed.

Alex Andrei

Identify where the current generic php command is and to which binary it points to with which php.

It will give you a path to a symlink like you mention in your question

/usr/bin/php -> /usr/lib64/php5.4/bin/php

Edit the symlink to point to which ever php version you want for now, see here https://unix.stackexchange.com/questions/88824/how-can-i-edit-symlinks

When you are done just reverse the process.

UPDATE: you can also add an alias for the current user by editing ~/.bashrc and adding the following

alias php='/usr/bin/php5.6'

see if this works out

Since PHP7 came around Debian Linux creates different executables for PHP versions 5 and 7 in /usr/bin by default (if you install both versions that is).

Calling those different versions from the command line is as simple as ever now:

kkarski@debian:~ $ php5 -v
PHP 5.6.26-0+deb8u1 (cli) (built: Sep 21 2016 12:37:50) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies


kkarski@debian:~ $ php -v
PHP 7.0.9-1~dotdeb+8.1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.9-1~dotdeb+8.1, Copyright (c) 1999-2016, by Zend Technologies

This is obviously only good for simple scripts. For anything larger (composer, artisan etc.) you'll have to change the PATH variable.

To change the version your Apache server is using all you have to do is:

root@debian:~# a2dismod php5 && a2enmod php7.0
Module php5 disabled.
To activate the new configuration, you need to run:
  service apache2 restart
Considering conflict php5 for php7.0:
Enabling module php7.0.
To activate the new configuration, you need to run:
  service apache2 restart

and vice versa if you want to use the lower PHP version.

Mentioning it in case someone has similar problems on Debian.

For anyone else who found no solution in the above, because they use composer update and somehow the wrong PHP version gets used. By using composer self-update I got some more info and eventually found out that in the composer.json you can specify a platform in the config section, which overrides what php version is used by composer. Simply changing this value or removing this config solved my issue.

composer.json "config": { "platform": { "php": "7.1" },

It's possible to do using alias, but keep in mind that aliases are not expanded by default.

You must also enable expanding of those.

shopt -s expand_aliases alias php="/usr/local/bin/php-5.6" ./some-script.sh unalias php # back to previous version

I find the easiest to achieve the same like just create a softlink like for example

ln -s /opt/php-7.0.32/bin/php /usr/bin/php7

ln -s /opt/php-7.1/bin/php /usr/bin/php71

ln -s /opt/php-5.6/bin/php /usr/bin/php56

then as you use your default version say it is php7.2 as just php for alternative version you can you php7 or php71 or php56

here ln -s /opt/php-7.1/bin/php /usr/bin/php71 is the source/orginal file and /usr/bin/php7 is the destination / link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!