Sometimes you just need to switch the systems default php version to achieve something.
Perfect use scenario would be if you get stuck with an old 2.1 version of Moodle and you want to get up to 3.11+, it is not as difficult as some sites make out. In fact it is quite simple. We had an old 2.1 Moodle running on a server that was going to be put to rest and we could not get php 7.2 onto it. We needed to get the system onto a new server and onto Moodle 3.11.
1st setup quick entry level VM on Google cloud/Azure/AWS running Debian 9, this can be done for free on their entry levels.
2nd install apache and php and mysql (unless you going to leave db on its current server)
sudo apt install apache2
1. sudo apt-get install apt-transport-https lsb-release ca-certificates
2. sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
3. sudo curl https://packages.sury.org/php/apt.gpg | apt-key add -
add the below line to /etc/apt/sources.list.d/php.list : do not add the inverted commas the word stretch refers to Debian 9
4. sudo /etc/apt/sources.list.d/php.list
add the text inside the inverted commas "deb https://packages.sury.org/php/ stretch main"
5. sudo apt-get update
6. sudo apt-get install php5.6
7. sudo apt-get install php7.2 / or 7.4 your choice
install all these modules for php 5.6 and 7.2
bz2 calendar curl exif gd intl json mbstring mysqli mysqlnd posix simplexml soap tokenizer xml xmlreader xmlrpc xmlwriter xsl zip
so run sudo apt install php5.6-zip for each of the above modules. Do same for php version 7
To switch php version it is super simple.
sudo update-alternatives --config php
and select the php version you need to work with.
3rd transfer the current site. Html and MOODLEDATA directory to this VM and get the site running. You can leave the db on its current server just make sure the VM can access the DB on that server or simply move that DB to the VM as well, wither option works (faster on the VM obviously). I simply put the moodle folder under /var/www/html and the moodledata folder under /var/www updated the config.php to indicate this and moved forward.
Now you can use php5.6 all the way up to Moodle 3.1 when you hit the 3.1 update you will need to have php7.2 or 7.4 installed does not really matter it will carry you all the way to 3.11+
The most common problem you will get is missing php modules. Just make sure you have these installed and you should be fine, the updater will let you know.
So if doing this to jump from Moodle 2.* to 3.11+ do this for PHP5.6 and php7.2 or 7.4 your choice just make sure you install the php modules for both versions of php
e.g. apt install php7.2-xmlreader or apt install php5.6-xmlreader (dont worry the CLI update will notify if you are missing at the time)
Ok so lets begin.
You have the site working under /var/www/html/moodle and var/www/moodledata and the moodle/config.php file has been updated to indicate the new location. Go into the MOODLEDATA directory delete the config.php file under the muc directory.
So moodledata/muc/config.php <- delete this file. trust me it might not be needed does no harm but can cause problems and the new install will recreate it properly. so delete and forget.
You are now ready to start the upgrade process.
Here are the last restrictions.
You can only upgrade to Moodle 3.11 from Moodle 3.2 or later.
You can only upgrade to Moodle 2.7 from Moodle 2.2 or later.
You can only upgrade to Moodle 2.2 from Moodle 1.9 or later.
Get your first update in place, if going from 2.2 to 2.7
Here a recommended path 1.x -> 1.9.19+ -> 2.2.11 -> 2.7.20 -> 3.2.9 on PHP7.2 -> 3.5 on PHP7.2 -> 3.11
move the current moodle folder to moodle2.2 just so you know whats in there.
unzip the 2.7 moodle installer it will create the new moodle folder and now copy to config.php file into the new moodle folder.
e.g. /var/www/html/moodle
mv /var/www/html/moodle to /var/www/html/moodle2.2
uncompress the moodle2.7 installer so this is now /var/www/html/moodle
copy config.php from /var/www/html/moodle2.2/config.php to /var/www/moodle/config.php
go into the moodle folder and run this command
sudo -u www-data /usr/bin/php admin/cli/upgrade.php
hit Y when asked and let it update your moodle db to 2.7 it takes a few minutes but updates you as it progresses.
Now do this again for the next Moodle version, you can increment jump as you move up the version numbers it is up to you. But you have to follow the main rules about for the major release updates. I did all the incremental steps for the first system then just did the major version for the next one. It is your choice.
Now when you do the jump to 3.2 and you need PHP 7.2 then this is how you tell the system to use the specific php version. Choose the version you need for the current requirement i.e. Moodle <3.2 then 5.6 anything above php 7.2/7.4.
then once update finished just move the current moodle folder to what every version it is. i.e. moodle2.7 uncompress the moodle3.2, copy the config.php file to the new moodle folder and run the CLI update line again.
sudo -u www-data /usr/bin/php admin/cli/upgrade.php
To switch to php 7.2 when needed follow below, it takes about 5 seconds.
sudo update-alternatives --config php
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/php7.4 74 auto mode
1 /usr/bin/php5.6 56 manual mode
2 /usr/bin/php7.2 72 manual mode
* 3 /usr/bin/php7.4 74 manual mode
select the version you want to use.
There you have it. This is a quick simple path to updating Moodle to the latest version from any version you have installed. The whole process from 2.2 to 3.11 took 18 minutes stepping through the main builds along the way.
for reference on Moodle command line options.
https://docs.moodle.org/311/en/Administration_via_command_line