Selecting the right PHP version on the Linux shell

Published 2021-02-01 by Jochen Lillich

One of the recurring support issues our customers have been raising in recent years was how to make sure a shell command or automation script on freistilbox uses the correct PHP version. We’ve finally laid this issue to rest.

freistilbox offers a selection of PHP versions, both on the web boxes running your Drupal or WordPress application, and on the shell box that you can use for maintenance and job automation. You assign a PHP version to a specific website on the freistilbox dashboard, and the hosting platform will automatically configure your web boxes accordingly.

With the shell box, things are a bit more complex because it’s not set up specifically for a single website and therefore needs to offer all PHP versions currently supported by freistilbox. There can, however, only be one php command.

How did we solve this “1:n” problem? We replaced the original php command with a script that introduces an environment variable for controlling the version of the PHP interpreter it will execute. It looks like this:


exec php${PHP_VERSION:-7.3} "$@"

(Yes, it’s a one-liner. Sometimes, the best solution is so simple that it takes you ages to find it.)

By assigning the environment variable PHP_VERSION the value 7.2, this script will execute php7.2. If the variable isn’t set at all, the script above will default to php7.3.

To summarise: Our definitive answer to the question “How do I run the PHP command line interpreter in the correct PHP version?” is “You assign it to the related environment variable”, like this:

export PHP_VERSION=7.3

You can find more details on this in the freistilbox documentation.

Spoiler: We’re already working on a follow-up change that will automatically configure a specific website user’s shell environment for the PHP version selected on the freistilbox dashboard!