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 version of PHP used when running command line (CLI) commands through Secure Shell (SSH).
If you want to change the PHP version of your website instead, please read this guide .
Change the version of PHP used in CLI
To change the version of PHP used from the command line (CLI) via SSH:
- log in using SSH ( help )
- create a .profile file if it doesn't exist:touch ~/.profile
- edit .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 php version:php-v
Interactive mode
The .profile file mentioned above is only loaded when the user interface is used. In SSH (login mode), the .bash_profile file is sourced. And in interactive mode (non-login), it is the .bashrc file which is sourced.
Some server deployment scripts open sessions in interactive mode (non-login) and environment variables are not loaded in the PATH even if the SSH session is correctly configured.
In this case, you must load the php version in the .bashrc file (export PATH="/opt/php7.4/bin:$PATH"), and source in the .bash_profile and .profile files the .bashrc file (. ~ /.bashrc).
After following these steps, the version of PHP you specified should be loaded into your shell environment each time you open a new session. This will allow you to run scripts using this version of PHP from the command line (CLI).
Mention of the PHP version
If you only use the mention "php", the release preceding the one that was already in place will be installed (latest version -1): if the last version installed is 8.1, 8.0 will be used with "php"). It is preferable to use the mention "php-7.0", "php-7.1", etc. so that the version does not change as updates are made.