You are not logged in.

#1 2021-02-25 15:41:19

andrej
Member
Registered: 2012-03-31
Posts: 21

[SOLVED] PHP 7/8; how to mask /usr/bin/php to switch between versions

I'd like to run PHP 7 and 8 alongside on a VM used for development and have a simple way to switch between the versions. The problem I can't think of a solution for is how to make PHP CLI scripts that use the `php` executable to use `php7` instead.

Example:

#!/usr/bin/env php
<?php
echo phpversion();

Without changing this script I need to be able to use both 8 and 7.

So `php` and `php7` both exists in `$PATH` in `/usr/bin/`. How to configure the environment to make `php` temporarily point to `/usr/bin/php7` ?

Last edited by andrej (2021-02-26 13:40:04)

Offline

#2 2021-02-25 16:05:39

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] PHP 7/8; how to mask /usr/bin/php to switch between versions

Do you really need to be able to use the script exactly as it currently is, or do you just need to be able to switch between the two without changing the script at that time?  If the latter, make a symlink, like /bin/php-current and call that from the script.  Then you can reassign the symlink as needed.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2021-02-26 00:59:21

amish
Member
Registered: 2014-05-10
Posts: 470

Re: [SOLVED] PHP 7/8; how to mask /usr/bin/php to switch between versions

Method 1:
mv php php8
Create new php such that it execs php7 or php8 based on whatever your criteria is.
Criteria can be checking environment variable (say USE_PHP7) or existence of some file say /etc/use_php7

Then you can simply make CLI call like these (for first criteria)
USE_PHP7=1 php myscript.php
USE_PHP7=1 ./myscript.php

Method 2:
mv php php8
ln -s php7 realphp
OR
ln -s php8 realphp

Create new php such that it execs realphp

Offline

#4 2021-02-26 04:08:00

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] PHP 7/8; how to mask /usr/bin/php to switch between versions

moving and creating a new /bin/php will be rather fragile as you'd need to restore it all after every update of the php package.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2021-02-26 07:26:12

andrej
Member
Registered: 2012-03-31
Posts: 21

Re: [SOLVED] PHP 7/8; how to mask /usr/bin/php to switch between versions

Thanks for the feedback. Unfortunately the script is from a 3rd party so it's out of my control by what name it calls php.

I was hoping there was some magical linux thing to mask the path to php as something else. Symlink it is then. And a pacman hook that will put things back in order after update.

Last edited by andrej (2021-02-26 07:38:10)

Offline

#6 2021-02-26 07:38:52

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: [SOLVED] PHP 7/8; how to mask /usr/bin/php to switch between versions

You can do something like this:

mkdir /tmp/php7bin
ln -s /usr/bin/php7 /tmp/php7bin/php
export PATH="/tmp/php7bin:$PATH"
# now /bin/env php will find php7 in this shell

Or use a container / mount namespace where php is overridden with bind mounts.

Last edited by progandy (2021-02-26 07:39:33)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#7 2021-02-26 07:46:34

andrej
Member
Registered: 2012-03-31
Posts: 21

Re: [SOLVED] PHP 7/8; how to mask /usr/bin/php to switch between versions

@progandy so php will be found by the order of appearance in $PATH? That's actually really cool, thanks.

Offline

#8 2021-02-26 13:38:56

andrej
Member
Registered: 2012-03-31
Posts: 21

Re: [SOLVED] PHP 7/8; how to mask /usr/bin/php to switch between versions

So I ended up with this if it helps someone:

Have a directory for the user for shell scripts:

mkdir ~/bin

Have this directory in the shell's PATH. Edit

~/.bash_profile

and add:

[ -d ~/bin ] && { PATH="${HOME}/bin:${PATH}"; }
export PATH

Then manually symlink the correct php into ~/bin/php. Or you can put the following script into

~/bin/usephp
#!/bin/sh
[ "$#" -lt "1" ] && {
  echo "Configure current shell to use a specific major PHP version.

    Usage: usephp 7"
  exit 0
}
binary="php${1}"
wanted="/usr/bin/${binary}"
[ ! -f "${wanted}" ] && {
  echo "'${binary}' not found. Using 'php'."
  wanted="/usr/bin/php"
}
ln -sf "${wanted}" "${HOME}/bin/php"
inUse=$(php -nr "echo phpversion();")
echo "Shell configured to use PHP ${inUse}."

Make it executable

chmod u+x ~/bin/usephp

and use it like so:

usephp 8
php -v
PHP 8.0.2 (cli) (built: Feb  2 2021 18:26:02) ( NTS )

usephp 7
php -v
PHP 7.4.15 (cli) (built: Feb  2 2021 18:30:22) ( NTS )

Offline

Board footer

Powered by FluxBB