Debian set default php version

On your system, if you have installed multiple versions of PHP (eg PHP 8.1, 8.0, 7.4, 7.3, 7.2, 7.1 and 5.6). PHP 7.2 is running as default PHP for Apache and CLI. For any requirement, you need to use PHP 5.6. Then you don’t need to remove PHP 7.2. You can simply switch your PHP version to default used for Apache and command line.

We assume you have installed multiple PHP version on your Debian system. Now you need to switch the active PHP version for CLI and Apache2. This tutorial will help you to switch between multiple PHP versions for Apache server and CLI on Debian.

  • Read => How to Install PHP on Debian 11 via PPA
  • Read => How to Install PHP on Debian 10 via PPA

We have installed PHP 8.1, PHP 7.4 and PHP 5.6 on our Debian system. You can use the same for other PHP versions by changes commands accordingly.

Enable PHP 8.1 as Default Version

You need to set PHP 8.1 as your active PHP version for CLI and Apache2 both. You can do this by disabling Apache2 modules for all other installed PHP versions and configure CLI using the update-alternatives command.

  • For Apache:-
    Run the following command to disable Apache module for other PHP versions.
    sudo a2dismod php7.4 php5.6 
    

    Now, enable PHP 8.1 module in Apache server.

    sudo a2enmod php8.1 
    sudo service apache2 restart 
    
  • For Command Line Interface:-
    sudo update-alternatives --set php /usr/bin/php8.1 
    sudo update-alternatives --set phar /usr/bin/phar8.1 
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.1 
    sudo update-alternatives --set phpize /usr/bin/phpize8.1 
    sudo update-alternatives --set php-config /usr/bin/php-config8.1 
    

Note – The phpize8.1 and php-config8.1 command is available in php8.1-dev package. This is more useful for compiling PHP modules using pecl.

Enable PHP 7.4 as Default Version

You need to set PHP 7.4 as your active PHP version for CLI and Apache2 both. You can do this by disabling Apache2 modules for all other installed PHP versions and configure CLI using the update-alternatives command.

  • Apache:-
    Run the following commands to disable Apache module for other PHP versions
    sudo a2dismod php8.1 php5.6 
    

    Now, enable PHP 7.4 module in Apache server.

    sudo a2enmod php7.4 
    sudo service apache2 restart 
    
  • Command Line Interface:-
    sudo update-alternatives --set php /usr/bin/php7.4 
    sudo update-alternatives --set phar /usr/bin/phar7.4 
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4 
    sudo update-alternatives --set phpize /usr/bin/phpize7.4 
    sudo update-alternatives --set php-config /usr/bin/php-config7.4 
    

Note – The phpize7.4 and php-config7.4 command is available in php7.4-dev package. This is more useful for compiling PHP modules using pecl.

Enable PHP 5.6 as Default Version

The developers of PHP are no longer supporting PHP 5.6. It is strongly recommended to upgrade to higher PHP version like 7.4 or 8.1. But still you can install and Use php 5.6 on your Debian system if your application required PHP 5.6. Let’s set PHP 5.6 as your default version for CLI and Apache2 both.

  • Apache:-
    Run the following commands to disable other PHP versions like (php 8.1, php7.4 etc) for Apache and command line.
    sudo a2dismod php7.4 php8.1 php8.0     
    

    Now, enable PHP 5.6 module in Apache server.

    sudo a2enmod php5.6 
    sudo service apache2 restart 
    
  • Command Line:-
    sudo update-alternatives --set php /usr/bin/php5.6 
    sudo update-alternatives --set phar /usr/bin/phar5.6 
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6  
    sudo update-alternatives --set phpize /usr/bin/phpize5.6 
    sudo update-alternatives --set php-config /usr/bin/php-config5.6 
    

Note – The phpize5.6 and php-config5.6 command is available in php5.6-dev package. This is more useful for compiling PHP modules using pecl.

Conclusion

In this tutorial, you have learned about switching PHP versions on Debian system. You can change the default PHP version without uninstalling the other versions.

I have installed php 5.6 and and php 7.1 on my Ubuntu 16.04

I know with Apache as my web server, I can do

a2enmod php5.6 #to enable php5
a2enmod php7.1 #to enable php7

When I disable php7.1 in Apache modules and enable php 5.6, Apache recognizes the change and uses php 5.6 interpreter as expected.

But when I run internal php web server from the commandline:

php -S localhost:8888

php handles requests using php 7. So how do I switch between php 5.6 and php 7.1 in the command line ?

Debian set default php version

Akshay

5966 silver badges20 bronze badges

asked Mar 6, 2017 at 6:55

Debian set default php version

1

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

Debian set default php version

PiTheNumber

21.9k17 gold badges103 silver badges173 bronze badges

answered Mar 6, 2017 at 7:03

Debian set default php version

Stevie GStevie G

5,0581 gold badge9 silver badges15 bronze badges

7

$ sudo update-alternatives --config php

should work for all ubuntu versions after 16.04 (18.04 and 20.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  to keep the current choice[*], or type selection number:

Choose the appropriate version

answered Jan 19, 2018 at 8:28

GiorgosKGiorgosK

6,6872 gold badges29 silver badges24 bronze badges

6

To list all available versions and choose from them :

sudo update-alternatives --config php

Or do manually

sudo a2dismod php7.1 // disable
sudo a2enmod php5.6  // enable

answered May 30, 2018 at 11:25

Debian set default php version

Kamal KumarKamal Kumar

3,0421 gold badge18 silver badges15 bronze badges

1

in ubuntu 20.04 switching between PHP 8.0 and PHP 7.4 version:

DOWNGRADE PHP 8.0 to PHP 7.4

sudo a2dismod php8.0
sudo a2enmod php7.4
sudo service apache2 restart
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo service apache2 restart

UPGRADE PHP 7.4 to PHP 8.0

sudo a2dismod php7.4
sudo a2enmod php8.0
sudo service apache2 restart
sudo update-alternatives --config php
sudo update-alternatives --config phar
sudo update-alternatives --config phar.phar
sudo service apache2 restart

check the changes:

  1. run php -v in the console and you will got:

PHP 8.0.3 (cli) (built: Mar 5 2021 07:54:13) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.3, Copyright (c) Zend Technologies with Zend OPcache v8.0.3, Copyright (c), by Zend Technologies

OR

PHP 7.4.16 (cli) (built: Mar 5 2021 07:54:38) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies

  1. add a PHP file in your runnable local environment like /var/www/html/ path by adding phpinfo(); and get PHP info in the browser (in top of the page the version of PHP is available to see)

answered Mar 22, 2021 at 10:03

Debian set default php version

Pejman KheyriPejman Kheyri

2,9767 gold badges30 silver badges31 bronze badges

1

I think you should try this

From php5.6 to php7.1

sudo a2dismod php5.6
sudo a2enmod php7.1
sudo service apache2 restart

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 php7.1 to php5.6

sudo a2dismod php7.1
sudo a2enmod php5.6
sudo service apache2 restart

sudo update-alternatives --set php /usr/bin/php5.6
sudo update-alternatives --set phar /usr/bin/phar5.6
sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6

answered Sep 19, 2017 at 9:38

1

I actually wouldn't recommend using a2enmod for php 5 or 7. I would use update-alternatives. You can do sudo update-alternatives --config php to set which system wide version of PHP you want to use. This makes your command line and apache versions work the same. You can read more about update-alternatives on the man page.

answered Mar 6, 2017 at 7:02

Debian set default php version

FatBoyXPCFatBoyXPC

8036 silver badges14 bronze badges

1

You can create a script to switch from versions: sudo nano switch_php then type this:

#!/bin/sh
#!/bin/bash
echo "Switching to PHP$1..."
case $1 in
    "7")
        sudo a2dismod php5.6
        sudo a2enmod php7.0
        sudo service apache2 restart
        sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php;;
    "5.6")
        sudo a2dismod php7.0
        sudo a2enmod php5.6
        sudo service apache2 restart
        sudo ln -sfn /usr/bin/php5.6 /etc/alternatives/php;;
esac
echo "Current version: $( php -v | head -n 1 | cut -c-7 )"

exit and save make it executable: sudo chmod +x switch_php

To execute the script just type ./switch_php [VERSION_NUMBER] where the parameter is 7 or 5.6

That's it you can now easily switch form PHP7 to PHP 5.6!

chepe263

2,73420 silver badges38 bronze badges

answered Jul 22, 2017 at 18:38

ihakozihakoz

1211 silver badge3 bronze badges

2

May be you might have an old PHP version like PHP 5.6 in your system and you installed PHP 7.2 too so thats multiple PHP in your machine. There are some applications which were developed when older PHP 5.6 was latest version, they are still live and you working on those applications, You might be working on Laravel simultaneously but Laravel requires PHP 7+ to get started. Getting the picture ?

In that case you can switch between the PHP versions to suit your requirements.

Switch From PHP 5.6 => PHP 7.2

Apache:-

sudo a2dismod php5.6
sudo a2enmod php7.2
sudo service apache2 restart

Command Line:-

sudo update-alternatives --set php /usr/bin/php7.2
sudo update-alternatives --set phar /usr/bin/phar7.2
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.2
sudo update-alternatives --set phpize /usr/bin/phpize7.2
sudo update-alternatives --set php-config /usr/bin/php-config7.2

And vice-versa, Switch From PHP 7.2 => PHP 5.6

Apache:-

sudo a2dismod php7.2
sudo a2enmod php5.6
sudo service apache2 restart

Command Line:-

sudo update-alternatives --set php /usr/bin/php5.6
sudo update-alternatives --set phar /usr/bin/phar5.6
sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6
sudo update-alternatives --set phpize /usr/bin/phpize5.6
sudo update-alternatives --set php-config /usr/bin/php-config5.6

answered Aug 18, 2019 at 6:30

Debian set default php version

1

You can use below command lines to switch between two PHP version.

E.g.

I want to switch PHP Version from 7.1 to 7.2 we can use below command

sudo a2dismod php7.1 &&  sudo update-alternatives --set php /usr/bin/php7.2 && sudo a2enmod php7.2 && sudo service apache2 restart

a2dismod is use to disable the current php version and a2enmod is use to enable the version

answered Feb 14, 2019 at 5:12

Debian set default php version

this worked for me:-

sudo update-alternatives --set php /usr/bin/php7.4

just change the PHP version to whichever version you need I have changed it to php7.4

answered Apr 29, 2021 at 1:13

AahadAahad

4304 silver badges9 bronze badges

1

Type given command in your terminal..

For disable the selected PHP version...

    • sudo a2dismod php5
    • sudo service apache2 restart
  1. For enable other PHP version....

    • sudo a2enmod php5.6
    • sudo service apache2 restart

It will upgrade Php version, same thing reverse if you want version downgrade, you can see it by PHP_INFO();

answered Jan 24, 2018 at 13:02

Debian set default php version

0

Switch from PHP 5.6 to PHP 7.2 using:

sudo a2dismod php5.6 && sudo a2enmod php7.2 && sudo service apache2 restart

Switch from PHP 7.2 to PHP 5.6 using:

sudo a2dismod php7.2 && sudo a2enmod php5.6 && sudo service apache2 restart

Debian set default php version

Omar Einea

2,4306 gold badges22 silver badges35 bronze badges

answered Mar 7, 2018 at 17:48

0

You could use these open source PHP Switch Scripts, which were designed specifically for use in Ubuntu 16.04 LTS.

https://github.com/rapidwebltd/php-switch-scripts

There is a setup.sh script which installs all required dependencies for PHP 5.6, 7.0, 7.1 & 7.2. Once this is complete, you can just run one of the following switch scripts to change the PHP CLI and Apache 2 module version.

./switch-to-php-5.6.sh
./switch-to-php-7.0.sh
./switch-to-php-7.1.sh
./switch-to-php-7.2.sh

answered Mar 15, 2018 at 17:03

DivineOmegaDivineOmega

3891 gold badge5 silver badges11 bronze badges

0

I made a bash script to switch between different PHP versions on Ubuntu.

Hope it helps someone.

Here's the script: (save it in /usr/local/bin/sphp.sh, don't forget to add +x flag with command: sudo chmod +x /usr/local/bin/sphp.sh)

#!/bin/bash

# Usage
if [ $# -ne 1 ]; then
  echo "Usage: sphp [phpversion]"
  echo "Example: sphp 7.2"
  exit 1
fi

currentversion="`php -r \"error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));\"`"
newversion="$1"

majorOld=${currentversion:0:1}
minorOld=${currentversion:1:1}
majorNew=${newversion:0:1}
minorNew=${newversion:2:1}

if [ $? -eq 0 ]; then
  if [ "${newversion}" == "${currentversion}" ]; then
    echo "PHP version [${newversion}] is already being used"
    exit 1
  fi

  echo "PHP version [$newversion] found"
  echo "Switching from [php${currentversion}] to [php${newversion}] ... "

  printf "a2dismod php$majorOld.$minorOld ... "
  sudo a2dismod "php${majorOld}.${minorOld}"
  printf "[OK] and "

  printf "a2enmod php${newversion} ... "
  sudo a2enmod "php${majorNew}.${minorNew}"
  printf "[OK]\n"

  printf "update-alternatives ... "
  sudo update-alternatives --set php "/usr/bin/php${majorNew}.${minorNew}"
  printf "[OK]\n"

  sudo service apache2 restart
  printf "[OK] apache2 restarted\n"
else
  echo "PHP version $majorNew.$minorNew was not found."
  echo "Try \`sudo apt install php@${newversion}\` first."
  exit 1
fi

echo "DONE!"

answered Feb 10, 2020 at 22:42

From PHP 5.6 => PHP 7.1

$ sudo a2dismod php5.6
$ sudo a2enmod php7.1

for old linux versions

 $ sudo service apache2 restart

for more recent version

$ systemctl restart apache2

answered Sep 24, 2018 at 14:42

Debian set default php version

0

When installing laravel on Ubuntu 18.04, be default PHP 7.3.0RC3 install selected, but laravel and symfony will not install properly complaining about missin php-xml and php-zip, even though they are installed. You need to switch to php 7.1, using the instructions above or,

 sudo update-alternatives --set php /usr/bin/php7.1

now, running laravel new blog, will proceed correctly

answered Oct 22, 2018 at 20:36

pingle60pingle60

6567 silver badges9 bronze badges

please follow the steps :

i.e : your current version is : current_version = 7.3 , and you want to change it to : new_version = 7.2

1) sudo a2dismod php(current_version) 
2) sudo a2enmod php(new_version)
3) sudo update-alternatives --config php (here you need to select php version number) 
4) restart apache through : 
  sudo /etc/init.d/apache2 restart OR
  sudo service apache2 restart

answered Jan 9, 2020 at 12:51

YashYash

1311 silver badge5 bronze badges