Another post to document something incase I need it in the future!

I’ve had a couple of self-hosted PHP applications nagging me of late about running PHP7.2. Nextcloud, in particular, will soon require at least version 7.3 of PHP. I had to search around a bit to find instructions that worked for me, so I figured I’d amalgamate them here.

  • Note that if you’re using a Linux distribution that already packages PHP7.3/4 etc this doesn’t apply. However, Ubuntu 18.04 LTS seems only to offer 7.2 and some of my LXC containers needed the update.

Finding out which packages were in use on my system

First i needed to know which PHP packages were in use so that they all got upgraded to the new versions;

$ sudo dpkg --get-selections | grep -i php

Once I had a list of the packages that were version 7.2, I copy pasted it into a text editor and replaced with 7.4. This way I knew that the new version of the package would be pulled down and I wouldn’t have to go chasing missing packages.

Adding the Ondrej/PHP repo

This is pretty well documented across the web. On a Debian derived distribution (I largely use Ubuntu) the following should work:

$ sudo apt update
$ sudo apt -y install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update

Installing PHP7.4

This is easy enough:

$ sudo apt install php7.4
$ php -v

The output of php -v command should show the updated PHP version. Go ahead and install the updated versions of the packages you need from the first step:

$ sudo apt install php7.4-<package name>

Enabling PHP for Apache

This is the bit that took me a while to figure out! After I updated my PHP version I found that the PHP applications were still using the 7.2 version. Uninstalling PHP7.2 did NOT make the application suddenly use the new version…it just broke the application. Hmmm.

The solution is frustratingly simple - the Apache module needs to be updated!

$ sudo a2dismod php7.2
$ sudo a2enmod php7.4
$ sudo systemctl restart apache2

Cleaning up configurations

Occasionally PHP web applications want php.ini changes. You can copy these files from the old version to the new version (although be aware this might break things…)

$ sudo mv /etc/php/7.4/apache2/php.ini /etc/php/7.4/apache2/php.ini.backup
$ sudo cp /etc/php/7.2/apache2/php.ini /etc/php/7.4/apache2/php.ini
$ sudo systemctl restart apache2

That should be everything! The latest version of PHP should be working on the system.