How do I change the default PHP base-version on shell?

I have Centos 7 and Virtualmin installed, with the tipycal php-fpm 5.4, 7.0, 7.1 that you can choice between the versions you prefer on every virtualhost via Virtualmin control panel, and everything works well.

But when I access to the server via SSH and check php -v I get this:

PHP 5.4.16 (cli) (built: Oct 30 2018 19:30:51) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

How can I select another php default/base version for the shell system?

The issue you’re facing is that the PHP CLI (Command-Line Interface) version is set to the default system PHP version (5.4 in your case) installed with CentOS 7. When using Virtualmin, you can configure different PHP versions for virtual hosts, but it doesn’t change the default PHP version for the CLI globally.

Here’s how you can change the default PHP version for the shell (CLI) on CentOS 7:

1. Check Available PHP Versions

First, check which PHP versions are installed on your system:

sudo alternatives --display php

This will list all the installed PHP versions and their respective paths.

2. Set the Default PHP Version

To switch the default PHP version for the CLI:

sudo alternatives --config php

You will see a menu like this:

There are 3 programs which provide 'php'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/php-5.4
   2           /usr/bin/php-7.0
   3           /usr/bin/php-7.1

Enter to keep the current selection[+], or type selection number:

Choose the number corresponding to the desired PHP version (e.g., 2 for PHP 7.0). This will set the selected version as the default PHP for the CLI.

3. Verify the Change

After making the change, verify the default PHP version:

php -v

It should now show the version you selected.

4. Optional: Update Your Shell Profile

If you have specific needs for a different PHP version in your user shell environment, you can override the global setting by updating your shell profile file (e.g., ~/.bashrc or ~/.bash_profile).

Add the following line, replacing the path with the appropriate PHP binary:

alias php='/usr/bin/php-7.1'

Then reload your shell configuration:

source ~/.bashrc

5. Managing Multiple PHP Versions in Scripts

If you need to use a specific PHP version in scripts, you can directly call the version-specific binary, such as:

/usr/bin/php-7.0 -v
/usr/bin/php-7.1 -v

This ensures that the script runs with the intended PHP version.

6. Troubleshooting

  • If PHP versions are not listed in alternatives: You may need to add them manually. For example:
sudo alternatives --install /usr/bin/php php /usr/bin/php-7.0 1
sudo alternatives --install /usr/bin/php php /usr/bin/php-7.1 2
  • If Virtualmin uses a custom PHP directory:
    Ensure the paths to the PHP binaries align with Virtualmin’s setup.

By following these steps, you can set and manage the default PHP version for the CLI on your CentOS 7 system. Let me know if you encounter any issues!