Sometimes while working on multiple projects, as a developer we generally needs to switch php version. Suppose we are working on multiple projects. One is being developed on php 7.2 version and another is based on php 5.6.
So in that situation if we enable only php 7.2 or php 5.2 for both project they will not work properly because of some features would not be supported by php 7.2 of 5.2. So we will need to switch appropriate php version for each project.
Here, there are some commands by using that we can easily switch php versions without any pain. i am assuming you have installed both the versions in your machine.
Switch From PHP 5.6 => PHP 7.2
Let suppose we are working on php 5.6 and need to switch on php 7.2. Execute the set of commands below
sudo a2dismod php5.6 sudo a2enmod php7.2 sudo service apache2 restart
After executing the above commands. Php versions would be changed. But if you check php version through terminal it will not be changed to php 7.2.
So for that you will need to change php cli also. and you will need the commands below to update the changes.
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
Switch From PHP 7.2 => PHP 5.6
And in the second situation if you need to switch php version just opposite to the above. Execute the commands below to achieve the appropriate result
sudo a2dismod php7.2 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
I hope this will help you out to switch php versions in your machine.