On your system, if you have installed multiple versions of PHP (eg PHP 7.4 and PHP 5.6 both). PHP 7.4 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.4. You can simply switch your PHP version to default used for Apache and command line.
For example, your server has PHP 7.4 and PHP 5.6 both version’s installed. Now following example will help you to switch between both versions. The PHP 5.6 is configured as default PHP version for Apache and CLI. Let’s make PHP 7.4 as default PHP for Apache server and CLI.
Switch From PHP 5.6 => PHP 7.4
The PHP 5.6 is set as default version in your system, and you need to switch to PHP 7.4. Run the following commands to switch for Apache and command line.
Apache:-
sudo a2dismod php5.6 sudo a2enmod php7.4 sudo service apache2 restart
Command Line:-
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.
Switch From PHP 7.4 => PHP 5.6
Default PHP 7.4 is set on your system and you need to switch to PHP 5.6. Now run the following commands to switch for Apache and command line.
Apache:-
sudo a2dismod php7.4 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
This tutorial helps you to switch between multiple PHP versions on Ubuntu system. This helps you to change default PHP version on your Apache server and command line interface.
33 Comments
This is a topic which is close to myy heart… Cheers!
Exactly where are your contact details though?
i am so satisfacted.my english is poor, sorry :). thx for approving my user greetings wally
Would you expect the output of my test info.php to show PHP 5.6?
#php -i | grep “Loaded Configuration File”
Loaded Configuration File => /etc/php/5.6/cli/php.ini
But the info.php still shows
Configuration File (php.ini) Path /etc/php/7.4/fpm
Loaded Configuration File /etc/php/7.4/fpm/php.ini
i am so satisfacted. greetings wally
thanks….
I made a bash function for this process. i hope it be useful:
#disable current php and enable php with passed version
function switch_php {
sudo a2dismod php$(php -v | grep -o -E ‘[0-9.]+’ -m 1 | cut -c 1-3 | head -n 1);
sudo a2enmod php$1;
sudo service apache2 restart;
sudo update-alternatives –set php /usr/bin/php$1;
sudo update-alternatives –set phar /usr/bin/phar$1;
sudo update-alternatives –set phar.phar /usr/bin/phar.phar$1;
sudo update-alternatives –set phpize /usr/bin/phpize$1;
sudo update-alternatives –set php-config /usr/bin/php-config$1;
}
//sample:
$ switch_php 7.1
or
$ switch_php 5.6
thank you
Thanks for the tutorial, I was struggling trying to use PHP 7.3 in Ubuntu 20 and this helped me to configure things as needed.
BTW, do you know of any way besides pecl install –force to build an extension for different PHP version?
thanks, it helped much!
Not touching the tutorial itself, but it’s time to abandon the 5.6 version
How can I check which modules have each php versions without having to switch them and run “php -m” ?
Thank you
Use the command with PHP version like:
php7.2 -m
php5.6 -m
For enable/disable modules read: https://tecadmin.net/enable-disable-php-modules-ubuntu/
thanks…
I hope this
Also I hope
I’m using Centos 7 and I want to switch default version from ” PHP 5.4.16 (cli) ” to ” PHP 7.0.27 ” in shall so for that what would be the command?
Thanks, Rahul. This is exactly what I was looking for.
Great post Rahul,
Even I was on the same issue few months back. Similar article I found on a tech blog which saved my day.
Great explanation and detailing. Keep up the good work.
Thank you my friend.:)
Excelente, Buen aporte, de php 7.2 a 7.1,
I’m hosting 5 sites using nginx virtual sites .. I want to use PHP 7.1 for just one site only how can I do this ?
How abot switching from php7.1 to php7.2 ?
phpize and php-config throw error messages (no alternatives)
Hi, What error are you getting?
just install php7.2 and check php version
thanks, you save my time)
thanxx
Good Work! Thanks for the blog
Ne fonctionne pas du tout sur un ubuntu 16.04
Pour se la raconter dans la signature y a du monde, mais pour filer des informations pertinentes la y a plus personne
Nice, thank you very much, I could make the change easily with your tutorial
Also Consider:
From PHP 5.6 => PHP 7.1
update-alternatives –set phar /usr/bin/phar7.1 && update-alternatives –set phar.phar /usr/bin/phar.phar7.1 && systemctl restart apache2
From PHP 7.1 => PHP 5.6
update-alternatives –set phar /usr/bin/phar5.6 && update-alternatives –set phar.phar /usr/bin/phar.phar5.6 && systemctl restart apache2
Thanks Eduardo,
Article updated
Great post you have here, Rahul. Did you know that if you manage your server with RunCloud, you can just change the PHP version just a click. We developed RunCloud which will configure your server to the best configuration so you can focus only on things that matters. Check out http://runcloud.io to get your 15 days free trial.
what is your selling point over something like ISPConfig3 which is provided free of charge?