Running php7 and php5.6 on Ubuntu 16.04 LTS

You have undoubtedly noticed, PHP7 is installed by default on Ubuntu 16.04 LTS.
This can be problematic if you have an old WordPress project that uses the php mysql extension, which is no longer available under php7.
The solution is to install an apache 2 configuration for both php 5.6 and php 7, and to enable one or the other as needed.

We start by installing PHP 5.6

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0 php5.6-xml

To switch from php 5.6 to php 7

sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart
sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php

To switch from php7 to 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

We can then automate the switching from one to the other with an alias in our bashrc:

vi  ~/.bashrc

alias setphp7='sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart; sudo ln -sfn /usr/bin/php7.0 /etc/alternatives/php'
alias setphp5='sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart; sudo ln -sfn /usr/bin/php5.6 /etc/alternatives/php'