You are not logged in.
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
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
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
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
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
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
@progandy so php will be found by the order of appearance in $PATH? That's actually really cool, thanks.
Offline
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