Change the default php version for command line interface

Operating system:
CentOS
OS version:
7.6 x64

Currently, I have 3 PHP versions installed on the server:
5.4, 7.2, 7.3
When running the company php -v I’m getting PHP 5.4.16
I’m trying to understand how to assign a php version to CLI. I tried to follow what was suggested here:


But when running update-alternatives --list php I’m not getting a list of available versions.

Can anyone maybe help in guiding me on how exactly to do this?
Thanks in advance!

Is there anyone that can help with this?

Hopefully this will help…

In a multi PHP setup there’s default PHP and then all others. Each has a cli but the ‘others’ need to be pointed to their binaries. To change the default PHP the one that uses /etc/php.ini needs to be changed.

Hey @ramin, thanks for your reply.
Unfortunately, this did not help me, I’m not sure if I am missing something or if it’s just not working as it should.
I followed the steps to change the default server PHP version in the server template setting page.

Under the Webmin > Others > PHP Configuration, I see this:

But I still can’t understand how to change the default PHP version to be php73. When I enter php -v I still see it’s php 5.4

As a workaround that’s easy to reverse you could use a symlink:

ln -s /opt/rh/rh-php73/root/usr/bin/php /usr/bin/php

This would give you 7.3 output from the command php -v. But it doesn’t change how global/default PHP is set. Also consider how it affects 5.4.

Normally in a multi PHP setup, global PHP is the installation configured by /etc/php.ini no matter the version. To make 7.2 or 7.3 global, 5.4 would need to be replaced. You won’t be shifting things around in Virtualmin, you’ll need to upgrade or replace 5.4 with the newer version in a terminal using packages from SCL repository. I’m not much help with installing SCL PHP packages because I’m accustomed to Remi’s repo.

Let’s say you decide to take the long road and replace 5.4 with 7.3. First thing is to remove existing 7.3 because you don’t want it installed twice. Then, if you still need 5.4 you’ll have to reinstall it as an extra since it’s been replaced by global 7.3; 5.4’s install locations will be different than before and its .ini file would be /etc/opt/rh/rh-php54.ini instead of /etc/php.ini. The net benefits from juggling PHP installations this way won’t amount to much.

I bet the symlink solution is sounding pretty good at this point.

Thanks for the detailed information @ramin!
I tried applying the “symlink” solution but I get the following message:

ln: failed to create symbolic link ‘/usr/bin/php’: File exists

About upgrading the default php version, I saw in some threads that it’s not recommended to remove the php version that comes with the distro

That’s kinda what I meant about considering how 5.4 would be affected, but (my bad) I didn’t consider myself that the 5.4 binary is in the way of the symlink. A different name for the symlink might work but it’s still a cheap workaround. If you absolutely want a newer PHP version as global default, the longer road is the cleaner way to do it.

But do you really need to? The cli for your extra PHP versions should be available, they just require longer paths, like this or similar (rh packages throw me off):

/usr/bin/rh-php72 -v
/usr/bin/rh-php72 --ini

/usr/bin/rh-php73 -v
/usr/bin/rh-php73 --ini

If there aren’t any issues with scripts or Virtualmin then I suggest don’t try to fix what aint broken.

It’s less a matter of removing the distro’s package than it is a matter of removing the default. Best not to remove the default installation but it can be upgraded. Take a deep dive into CentOS 7 tutorials and you’ll find it’s not unusual for PHP 5.4 to get an upgrade.

Thanks again for the detailed reply @ramin!
Actually, the reason why I’d like to have CLI on a higher php version is that I use Drupal 8 for the website hosted on this server, and some cron jobs require a higher php version to run.
This is the only reason why I started investigating this in the first please :slight_smile:
I guess I will need to look more into updating the php 5.4 since this seems to be the “proper” way to go about this.

Ok, then in Virtualmin you just need to assign the newer PHP version to the virtualhost running Drupal. If it runs on PHP 7.3 cron job commands would look something like…

/usr/bin/rh-php73 /path/to/script1.php
/usr/bin/rh-php73 /path/to/script2.php

Even with default PHP cron sometimes requires the full path to PHP, or it never hurts…

/usr/bin/php /path/to/script.php