1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Modify the PHP version used in CLI via SSH
This guide provides instructions for changing the PHP version used when running Command Line Interface (CLI) commands via Secure Shell (SSH).
Changing the PHP Version Used in CLI
To switch the PHP version used in Command Line Interface (CLI) via SSH:
- Connect via SSH (help)
- Create a .profile file if it doesn't exist: touch ~/.profile
- Edit the .profile file: nano ~/.profile
- Add this code inside the file to choose 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's the .bash_profile file that is sourced. And in interactive mode (non-login), it's the .bashrc file that is sourced.
Some server deployment scripts open sessions in interactive mode (non-login), and 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 PHP version you specified should be loaded into your shell environment every time you open a new session. This allows you to run scripts using this PHP version in Command Line Interface (CLI).
Mention of PHP Version
If you only use the "php" mention, it will install the release preceding the one already in place (latest version -1): if the last installed version is 8.1, then 8.0 will be used with "php"). Preferably, use the mention "php-7.0", "php-7.1", etc., so the version doesn't change with updates.