Switch between PHP versions

There are cases when you have multiple php versions installed in your system and different projects used different php versions. In this case, switch between php versions is frequently required. You can switch between php versions by few commands. Here i am showing example for switch between php5.6 and php7.1

Switch From PHP 5.6 to PHP 7.1

Switching between two php versions is very easy. What you have to do is just disable current apache module for active php version. And enable apache module for php version you want to be active.

To switch from php5.6 to php7.1, currently active php version is 5.6. So you need to disable apache module for php5.6 and enable apache module for php7.1. After change in active apache module, you need to restart the apache service.

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
sudo update-alternatives --set phpize /usr/bin/phpize7.1
sudo update-alternatives --set php-config /usr/bin/php-config7.1

Switch From PHP 7.1 to PHP 5.6

Switching from php7.1 to php5.6 is similar to switch from php5.6 to php7.1 as shown above. You can just change php version in above commands as shown below.

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 
sudo update-alternatives --set phpize /usr/bin/phpize5.6
sudo update-alternatives --set php-config /usr/bin/php-config5.6

Re-Install apache

When trying to reinstall apache, only apt-get remove is not sufficient to uninstall apache. You should use purge to remove completely.

sudo apt-get purge apache2
sudo apt-get install apache2
apt-get purge libapache2-mod-php5 php5 && \ apt-get install libapache2-mod-php5 php5

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top
Shares