1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Change the PHP version in CLI
This guide explains how to change the PHP version directly from the command line when you run PHP scripts using PHP CLI (Command Line Interface) on an Web Hosting Infomaniak.
Preamble
- This can be useful when you need to temporarily modify certain parameters for a specific script or for a PHP session.
- If you use only the mention
php
below, it is the release preceding the one already in place that will be installed (last version -1): if the last installed version is 8.1, it is 8.0 that will be used with "php
". It is preferable to use the mentionphp-7.0
,php-7.1
, etc. so that the version does not change over time with updates. - Refer to this other guide if you are looking for information about configuring the PHP version set on the Infomaniak Manager.
Change the PHP version used in CLI
It is possible to easily modify the PHP version used in the command line (CLI) via SSH:
- Log in to the server via SSH (need help?).
Create a
.profile
file if it does not exist:touch ~/.profile
Edit the
.profile
file:nano ~/.profile
Add this code inside the file to select the desired PHP version:
export PATH=/opt/php7.4/bin:$PATH
Load the
.profile
file:source ~/.profile
Check the PHP version:
php -v
Interactive mode
The .profile
file mentioned above is only loaded when the user interface is used. In SSH (login mode), it is the .bash_profile
file that is sourced. And in interactive mode (non-login), it is the .bashrc
file that is sourced.
Some server deployment scripts open sessions in interactive (non-login) mode and the environment variables are not loaded into the PATH even if the SSH session is correctly configured.
In this case, you need to load the php version in the .bashrc
file (export PATH="/opt/php7.4/bin:$PATH"
), and source the .bashrc
file in the .bash_profile
and .profile
files (. ~/.bashrc
).
After following these steps, the version of PHP you specified should be loaded into your shell environment every time you open a new session. This will allow you to run scripts using this version of PHP from the command line (CLI).