Interactive switching mode
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
Manual Switching
From PHP 5.6 => PHP 7.1
Default PHP 5.6 is set on your system and you need to switch to PHP 7.1.
Apache:
$ sudo a2dismod php5.6
$ sudo a2enmod php7.1
$ sudo service apache2 restart
Command Line:
$ sudo update-alternatives --set php /usr/bin/php7.1
$ sudo update-alternatives --set phar /usr/bin/phar7.1
$ sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.1
From PHP 7.1 => PHP 5.6
Default PHP 7.1 is set on your system and you need to switch to PHP 5.6.
Apache:
$ sudo a2dismod php7.1
$ sudo a2enmod php5.6
$ sudo service apache2 restart
Command Line:
$ sudo update-alternatives --set php /usr/bin/php5.6
Source
Answer from Stevie G on Stack OverflowInteractive switching mode
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
Manual Switching
From PHP 5.6 => PHP 7.1
Default PHP 5.6 is set on your system and you need to switch to PHP 7.1.
Apache:
$ sudo a2dismod php5.6
$ sudo a2enmod php7.1
$ sudo service apache2 restart
Command Line:
$ sudo update-alternatives --set php /usr/bin/php7.1
$ sudo update-alternatives --set phar /usr/bin/phar7.1
$ sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.1
From PHP 7.1 => PHP 5.6
Default PHP 7.1 is set on your system and you need to switch to PHP 5.6.
Apache:
$ sudo a2dismod php7.1
$ sudo a2enmod php5.6
$ sudo service apache2 restart
Command Line:
$ sudo update-alternatives --set php /usr/bin/php5.6
Source
$ sudo update-alternatives --config php
should work for all ubuntu versions after 16.04 (18.04, 20.04 and 22.04)
This is what you should see as a response
There are 4 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php7.2 72 auto mode
1 /usr/bin/php5.6 56 manual mode
2 /usr/bin/php7.0 70 manual mode
3 /usr/bin/php7.1 71 manual mode
4 /usr/bin/php7.2 72 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Choose the appropriate version
You can do this with update-alternatives. If you would like to do this interactively, you can do this:
sudo update-alternatives --config php
If you like to specifically choose the PHP version (via an alias or whatnot), you can do this:
sudo update-alternatives --set php /usr/bin/php7.4
Of course, be sure to change php7.4 to the actual version you want to switch between.
Please use following command which will ask you to select a number against your required PHP version
sudo update-alternatives --config php
Then select your choice and press enter.
EDIT:
As pointed out by ᴍᴇʜᴏᴠ, Debian does support update-alternatives for the socket, but it does it a little differently, and there are two requirements:
- You must be running a PHP-FPM package from Feb 1 2020 or later (which hopefully you are)
- You must be running systemd versus sysvinit
The generic socket, located at /run/php/php-fpm.sock, is created by the systemd unit file by calling /usr/lib/php/php-fpm-socket-helper via the ExecStartPost and ExecStopPost directives. If you wish to use this socket, you still need to ensure your web server config is pointed to it versus the version-specific file.
The original answer is below if you don't meet those requirements.
It appears you are running a Debian/Ubuntu based distribution, which intentionally installs php-fpm in version specific locations (it's the whole reason you can use update-alternatives to switch the default version on the fly). However, there is no alternatives config for the socket files of php-fpm (your call to update-alternatives is only changing the CLI PHP version at /usr/bin/php).
The socket file to use is defined in /etc/php/8.3/fpm/pool.d/www.conf.
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php8.3-fpm.sock
If you really want to have a version agnostic socket file, you can change that setting to just be listen = /run/php/php-fpm.sock and update your nginx config accordingly. You'll need to do this for every version of PHP you have installed, and it also means you cannot have two different php-fpm versions running simultaneously.
You might be able to play around with creating your own update-alternative config for /run/php/php-fpm.sock that allows you to switch between versions, but I'm not sure how well that will work trying to manage files in /run that way.
This does not seem to be a problem on Debian 11, and I can use both /run/php/php-fpm.sock as well as /etc/alternatives/php-fpm.sock.
# ls -la /run/php
total 20
drwxr-xr-x 2 www-data www-data 260 Mar 29 19:24 .
drwxr-xr-x 20 root root 600 Mar 29 19:24 ..
-rw-r--r-- 1 root root 3 Mar 29 19:24 php5.6-fpm.pid
srw-rw---- 1 www-data www-data 0 Mar 29 19:24 php5.6-fpm.sock
-rw-r--r-- 1 root root 3 Mar 29 19:24 php7.3-fpm.pid
srw-rw---- 1 www-data www-data 0 Mar 29 19:24 php7.3-fpm.sock
-rw-r--r-- 1 root root 3 Mar 29 19:24 php7.4-fpm.pid
srw-rw---- 1 www-data www-data 0 Mar 29 19:24 php7.4-fpm.sock
-rw-r--r-- 1 root root 3 Mar 29 19:24 php8.2-fpm.pid
srw-rw---- 1 www-data www-data 0 Mar 29 19:24 php8.2-fpm.sock
-rw-r--r-- 1 root root 3 Mar 29 19:24 php8.3-fpm.pid
srw-rw---- 1 www-data www-data 0 Mar 29 19:24 php8.3-fpm.sock
lrwxrwxrwx 1 root root 30 Mar 29 19:24 php-fpm.sock -> /etc/alternatives/php-fpm.sock
See where /etc/alternatives/php-fpm.sock points to:
# stat /etc/alternatives/php-fpm.sock
File: /etc/alternatives/php-fpm.sock -> /run/php/php8.3-fpm.sock
Let's reconfigure PHP to 5.6 using the below command. It will present you with available versions and prompt for a number.
update-alternatives --config php-fpm.sock
Check again:
# stat /etc/alternatives/php-fpm.sock
File: /etc/alternatives/php-fpm.sock -> /run/php/php5.6-fpm.sock
you have to install alternative path first to display it in alternatives list,
see manual page
by using this command,
update-alternatives --install /usr/bin/php php /opt/plesk/php/7.2/bin/php 71
it will install your /opt/plesk/php/7.2/bin/php into alternatives list
I had to append an additional and mandatory option priority. The value has to be an integer.
This worked for me:
sudo update-alternatives --install /usr/bin/php php /opt/plesk/php/7.2/bin/php 100
Maybe you also want to add alternatives of these commands:
- phar
- phar.phar
- php-cgi
- php-cgi-bin
These commands will add alternatives for all five commands (also called link groups):
sudo update-alternatives --install /usr/bin/php php /opt/plesk/php/7.2/bin/php 100
sudo update-alternatives --install /usr/bin/phar phar /opt/plesk/php/7.2/bin/phar 100
sudo update-alternatives --install /usr/bin/phar.phar phar.phar /opt/plesk/php/7.2/bin/phar.phar 100
sudo update-alternatives --install /usr/bin/php-cgi php-cgi /opt/plesk/php/7.2/bin/php-cgi 100
sudo update-alternatives --install /usr/lib/cgi-bin/php php-cgi-bin /opt/plesk/php/7.2/bin/php-cgi 100
I think the best way to do this on Ubuntu is like that:
sudo update-alternatives --set php /usr/bin/php5.6
You may change the version according to your needs.
There are a million things you can do instead of changing your $PATH.
One is to define an alias in your /home/YOUR_NAME/.bashrc file,
alias myphp=' /path/to/my/favorite/php '
Another one is to rename the version of php that you do not want, and to transform /usr/bin/php in a symbolic link to the version you wish to use:
sudo mv /usr/bin/php /usr/bin/php_5.3.10
sudo ln -s /path/to/php/you/want /usr/bin/php
Or you may use the Debian alternatives system. First, you install a php alternative,
sudo update-alternatives --install "/usr/bin/php" "php" "/pathto/your/favorite/php" 1
then you control that everything's fine by means of
sudo update-alternatives --display php
And much, much more.