The problem “I want a new version of% software% on my
old ... stable Debian / CentOS ... ”as old as * the nix-world. Ways to achieve the desired enough. There is a mass of solutions how to drag several versions of the same software into the system. But then you want not only to have another version, but also to control which version is available in the system by default, for specific applications or users.
What if I want to change the system version of PHP to one of the custom builds? Let's start from the fact that you have already installed several versions of PHP on your server and you want the php command to have a specific version in the console that is different from the version that came with the system. In this article I will tell you how to properly configure it so that there are no problems with future batch updates.
As an example, take the 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 on the server is installed our Plesk with a couple of its own 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 (globally switching PHP from version 5.4 to 7 is somehow ss ... scary - something in the system can poploch from this). The PHP 5.6 binary 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 you can see from the PATH
, /usr/local/bin
is listed before /usr/bin
. So, if we put the link to the alternative version of PHP “earlier” in /usr/local/bin
, then it will be used when calling the php
command instead of /usr/bin/php
. We can create this link with our hands (and everything will even work), but it is more correct to use the update-alternatives
utility specially created for this purpose (on CentOS these are just alternatives
, but there is a update-alternatives
, so we’ll continue to use this command as a universal for Debian / Ubuntu / CentOS / etc).
Now, let's register all available PHP versions 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 has not selected a specific version. The largest number is the default choice.
Check that php
now 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 update-alternatives
has done 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
Apparently, she created a chain of symlinks and now, on demand, simply changes the intermediate symlink to the binary 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 system PHP is automatically selected by default. 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 has occurred:
# 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 fine. Now the system uses the version of PHP we need and I am not afraid that this setting will fly off with the next batch updates.
With the help of update-alternatives
you can choose not only the version of PHP, but many other things, such as different versions of phpunit
or 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 didn’t have a quest “Why does it work that way?” For your colleagues. Customize your system correctly.
PS I invite to phpenv
about phpenv
in the comments :)
Source: https://habr.com/ru/post/306682/
All Articles