How to use a custom PHP CLI version on Site5
April 05, 2021
Backstory
I needed to use PHP 7.1 with wp-cli on a shared Site5 server, but wp --info
reported using PHP 5.6.3. I tried using the WP_CLI_PHP
environment variable to
specify the php binary location to use, but that didn’t work. --info
would
report that it was using 7.1, but then the script would blow up, saying it
wasn’t.
I contacted Site5 support via chat, only to be told that not only could I not
use a custom PHP CLI version in my shell, I couldn’t use wp-cli
on their
shared servers. Since I use wp-cli
all the time on their shared servers, and
it is available at /usr/local/bin/wp
, I opted not to believe anything they
had said.
Solving the Problem
To create a binary of your own which will forward your commands to other PHP
versions, make a new file called php
which has the following contents
(this example uses PHP 7.1):
#!/usr/bin/env bashphp71 "$@"
After that, run chmod +x php
.
Testing the solution
Log out of your shell, then log back in. If you execute /usr/bin/env php -v
,
you should be greeted by something like this:
PHP 7.1.30 (cli) (built: Jul 24 2019 06:39:52) ( NTS )Copyright (c) 1997-2018 The PHP GroupZend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologieswith the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v10.2.0, Copyright (c) 2002-2018, by ionCube Ltd.
If everything is setup correctly, you should see the version you are hoping to use.
Future Versions
Since you don’t have read access to list files in /usr/local/bin
, you can’t
list the available PHP CLI versions. Because of that, you have to experiment to
figure out which PHP versions are available.
To do that, run commands such as php71 -v
and look at the output. If you see
an error saying the file or directory is not available, you have tried a
version which is not available. At the time of writing (April 5, 2021), the
latest binary available is php72
.
Other Use Cases
My original problem was actually that I needed to execute wp-cli
using a
custom version of PHP. In order to do that, I ended up using a modified version
of the script above:
#!/usr/bin/env bashphp71 /usr/local/bin/wp "$@"
I created this file in my path, and named it wp
. This caused the wp
shell
command to use my custom script, executing the system wp
in the custom
version of PHP and passing arguments on to it.
Thanks, Friend
I hope you found this useful and will come by again in the future.