Back to Home

Juggling PHP Versions in a System / Plesk Blog

php · update-alternatives · alternatives · plesk · linux

We juggle versions of PHP in the system

  • Tutorial

Problem “I want a new version of% software% on my old... stable Debian / CentOS ... ”is as old as the * nix world. There are enough ways to achieve the desired. There are lots of decisions on how to drag several versions of the same software into the system. But then I want to not only have another version, but also to control which version is available in the system by default, for specific applications or users.


What should I do if I want to change the system version of PHP to one of the custom assemblies? Let's start from the fact that you already have several versions of PHP installed on the server and you want the php command in the console to have a specific version, different from the one that came with the system. In this article I will tell you how to configure it correctly so that there are no problems with future batch updates.



As an example, take a server on CentOS 7 where native PHP is installed:


# php -v
PHP 5.4.16 (cli) (built: May 12 2016 13:45:17)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v5.0.12, Copyright (c) 2002-2015, by ionCube Ltd.

Also, our Plesk is installed on the server with a couple of its PHP builds:


# rpm -qa | grep plesk-php.*-release
plesk-php56-release-5.6.22-centos7.16052711.x86_64
plesk-php70-release-7.0.7-centos7.16052710.x86_64

Suppose we want to switch the system to use PHP 5.6 by default (switching globally PHP from version 5.4 to 7 is somehow ss ... scary - something in the system may stink from this). The PHP 5.6 binar is here:


# /opt/plesk/php/5.6/bin/php -v
PHP 5.6.22 (cli) (built: May 27 2016 11:45:28)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v5.0.18, Copyright (c) 2002-2015, by ionCube Ltd.
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

How to make the system use this version of PHP we need?


First, look at the system variable PATH


# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

It lists the directories in which programs are searched by name. The main nuance is that the search in directories occurs sequentially and the first result found is used. We can see the current path to the current PHP binary using the command:


# which php
/usr/bin/php

As can be seen from PATH, /usr/local/binis in the list earlier than /usr/bin. So, if we put the link to the alternative version of PHP “earlier” /usr/local/bin, then it will be used when the command is called phpinstead /usr/bin/php. We can create this link by hand (and everything will even work), but it’s more correct to use a utility specially created for these purposes update-alternatives(in CentOS it’s simple alternatives, but there is a symlink update-alternatives, so we will continue to operate with this command as universal for Debian / Ubuntu / CentOS / etc.).


Now, let's register all available versions of PHP with this command:


# update-alternatives --install /usr/local/bin/php php /opt/plesk/php/5.6/bin/php 10
# update-alternatives --install /usr/local/bin/php php /opt/plesk/php/7.0/bin/php 20
# update-alternatives --install /usr/local/bin/php php /usr/bin/php 30

The numbers 10, 20 and 30 are a priority. It works for automatic selection if the administrator himself did not select a specific version. The largest number determines the default selection.


Check that phpnow it points to the symlink created by the command:


# update-alternatives --list | grep php
php        auto        /usr/bin/php
# update-alternatives --display php
php - status is auto.
 link currently points to /usr/bin/php
/opt/plesk/php/5.6/bin/php - priority 10
/opt/plesk/php/7.0/bin/php - priority 20
/usr/bin/php - priority 30
Current `best' version is /usr/bin/php.

Let's see what she update-alternativesdid for us:


# which php
/usr/local/bin/php
# ls -l /usr/local/bin/php
lrwxrwxrwx. 1 root root 21 Jul  2 10:03 /usr/local/bin/php -> /etc/alternatives/php
# ls -l /etc/alternatives/php
lrwxrwxrwx. 1 root root 26 Jul  2 10:03 /etc/alternatives/php -> /usr/bin/php

As you can see, she created a chain of symlinks and now, on demand, she simply changes the intermediate symlink to the binar we need.


# php -v
PHP 5.4.16 (cli) (built: May 12 2016 13:45:17)
...

That is, we have successfully configured the PHP group in update-alternatives, where, by default, system PHP is automatically selected. Now we have the opportunity to switch the PHP command to any other version ..


Let's switch to PHP version 5.6, which comes with Plesk:


# update-alternatives --config php
There are 3 programs which provide 'php'.
  Selection    Command
-----------------------------------------------
   1           /opt/plesk/php/5.6/bin/php
   2           /opt/plesk/php/7.0/bin/php
*+ 3           /usr/bin/php
Enter to keep the current selection[+], or type selection number: 1

Check that the switch happened:


# php -v
PHP 5.6.22 (cli) (built: May 27 2016 11:45:28)
…
# update-alternatives --display php
php - status is manual.
 link currently points to /opt/plesk/php/5.6/bin/php
… 

Everything works great. Now the system uses the version of PHP we need and I am not afraid that this setting will crash during the next batch updates.


Using update-alternativesit, you can choose not only the version of PHP, but also many other things, for example, different versions phpunitor the default editor in the system. This approach is universal for various systems. Without reinventing your bike, using existing tools, you can be sure that you did not arrange the quest “Well, why does it work like that ?!” for your colleagues. Set up your system correctly.


PS I invite you to flame pro phpenvin the comments :)

Read Next