Changing default PHP CLI version

MageStack comes complete with all versions of PHP installed and usable as standard - and the version of PHP may vary depending on the scope that you are working within, be it the access server or web server.

The power of domain-groups means that you can have completely different websites, running completely different versions of PHP, all simultaneously - whilst using another version on command line. Understanding how these are selected can help you when running commands via CLI or web.

Access server

When SSH'ed into the access server, any version of PHP is available - where the default may differ based on your selection (see "Selecting access server default").

Eg. To run a script using the default version of PHP,

php foo.php

Eg. To execute a script using a specific version of PHP,

php-5.4 foo.php  # Executes as PHP 5.4
php-5.5 foo.php  # Executes as PHP 5.5
php-5.6 foo.php  # Executes as PHP 5.6
php-7.0 foo.php  # Executes as PHP 7.0
php-7.1 foo.php  # Executes as PHP 7.1
php-7.2 foo.php  # Executes as PHP 7.2

Domain group

The PHP version for a domain group is set via the Stacks page on https://my.sonassi.com, you can review which version is in use by either,

  • Creating a PHP script with <?php phpinfo(); in it and viewing it in a web browser
  • Checking the version by command line,

    /microcloud/domains/example/usr/bin/php -v

! Please note you will need the Stacks role in order to manage domain-groups and PHP Versions.

Displaying access server default PHP

You can view the default version by using the update-alternatives command,

sudo update-alternatives --display php

Where you can see the current selected version,

php - manual mode
  link currently points to /opt/php/php-5.4/bin/php
/opt/php/php-5.4/bin/php - priority 100
/opt/php/php-5.5/bin/php - priority 100
/opt/php/php-5.6/bin/php - priority 100
/opt/php/php-7.0/bin/php - priority 100
/opt/php/php-7.1/bin/php - priority 100
/opt/php/php-7.2/bin/php - priority 100
Current 'best' version is '/opt/php/php-5.4/bin/php'.

The final line shows which version is currently set as the default, in this case, it is PHP 5.4

  link currently points to /opt/php/php-5.4/bin/php

Selecting access server default PHP

You can change the default PHP version by running,

sudo update-alternatives --config php

From the dialog that appears, you can then go on to select the version of PHP that you would like to use by default,

There are 5 choices for the alternative php (providing /usr/bin/php).

  Selection    Path                      Priority   Status
------------------------------------------------------------
  0            /opt/php/php-5.4/bin/php   100       auto mode
* 1            /opt/php/php-5.4/bin/php   100       manual mode
  2            /opt/php/php-5.5/bin/php   100       manual mode
  3            /opt/php/php-5.6/bin/php   100       manual mode
  4            /opt/php/php-7.0/bin/php   100       manual mode
  5            /opt/php/php-7.1/bin/php   100       manual mode
  6            /opt/php/php-7.2/bin/php   100       manual mode

You can see from the example above that PHP 5.4 is currently selected. To change to another version, just type the selection ID (left column) and press enter.

Eg. To change to PHP 5.5

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /opt/php/php-5.5/bin/php to provide /usr/bin/php (php) in manual mode

You can now verify that the default PHP version is as selected,

php -v
PHP 5.5.38 (cli) (built: Mar  7 2018 16:18:27)

! Please note that this will only change the version of PHP used when using the php command, if you need to change a domain-group's PHP version then you can do this via the Stacks page on https://my.sonassi.com

Auto selecting PHP version based on domain-group

We have an article on how to implement an alias to automatically point to a domain-groups specific PHP version available: Here