Upgrade/Installation guide for PHP 7.4 on Ubuntu and Debian

Published On2019-11-28

PHP 7.4 was released today just in time for the thanks giving!

I have written posts about upgrading PHP to 7.3 last year, and for other versions in the previous years. It's time to upgrade to PHP 7.4!

SPOOKY WARNING

PHP 7.4 deprecates a bunch of features that your existing applications might be using. Although unlikely, there is a non-zero chance of the upgrade breaking your existing sites. Please make sure to make a proper backup of your server before you proceed.

1. Add ondrej/php PPA

Thanks to Ondrej, we have the PHP 7.4 builds ready at his PPA. These packages are not official builds from Ubuntu, but they are used by practically every modern Ubuntu stack, and I can't recommend them highly enough. If your wallet allows it, I suggest that you pay forward for his efforts with a donation.

Ubuntu
sudo add-apt-repository ppa:ondrej/php # Press enter when prompted.
sudo apt-get update
Debian
sudo apt install apt-transport-https lsb-release ca-certificates curl -y
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg 
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update

2. Get a list of current PHP packages

If you are upgrading from a previous PHP version, we will not take a note of the current PHP packages you have in your server. This list will help us to figure out the packages we should install for PHP 7.4. Note that PHP no longer bundles some less used extensions such as wddx, which you will need to compile yourself if you need it. Take a look at what's new and changing in PHP 7.4 for information on what's being removed from core.

dpkg -l | grep php | tee packages.txt

This will list all packages, filter them for those who have php in their name, and write them to packages.txt file at your current directory in addition to show them on screen.

This list will come handy to refer after we install PHP 7.4 to install the PHP 7.4 counterparts of the packages.

A screenshot of the existing package in an Ubuntu server

3. Install PHP 7.4

*Breathes heavily...

PHP 7.4 core

sudo apt install php7.4 php7.4-common php7.4-cli

This is to install the basic PHP 7.4 binaries. You will have the bare minimum PHP installation after this. Note that we did not install fpm or any other server integration package just yet

PHP 7.4 extensions

It's now time for the extensions. If you are upgrading from a previous installation, you can now take a look at the contents of your packages.txt file (cat packages.txt).

In Ondrej's PPA/Debian repo, PHP packages follow the php7.4-PACKAGE pattern. For example, mbstring is at php7.4-mbstring, and curl is at php7.4-mbstring.

To install:

sudo apt install php7.4-curl php7.4-mbstring php7.4-bz2 php7.4-readline php7.4-intl

Extend the list of packages above to match to the contents of your packages.txt file, or to all packages that are desired.

Here is my recommended list of extensions:

sudo apt install php7.4-bcmath php7.4-bz2 php7.4-curl php7.4-intl php7.4-mbstring php7.4-mysql php7.4-readline php7.4-xml php7.4-zip`

PHP already includes extensions such as curl, hash, ctype in core.

Web server integration

If you are planning to use PHP in a web server, you need to integrate the web server with PHP. There are two packages that you can use:

For Nginx web server, or Apache server using mod_event MPM, you need php7.4-fpm package.
If you are using PHP as an embedded Apache module (often a bad call), you will need libapache2-mod-php7.4 package. Apache users can run apachectl -V to get more information about the server integration. If it says prefork, you need libapache2-mod-php7.4 package. php7.4-fpm otherwise.

Nginx or Apache with event MPM
sudo apt install php7.4-fpm
sudo a2enconf php7.4-fpm # For Apache only
Apache with prefork MPM
sudo apt install libapache2-mod-php7.4

3. Test PHP 7.4 installation.

Run php -v to make sure PHP 7.4 (CLI) is properly installed. You can run php -m to get a list of extensions enabled in your PHP 7.4 setup.

PHP 7.4 'php -v' command.

4. Remove old PHP versions

After a successful PHP 7.4 installation, you can remove the old version if desired. Unless there is a strong reason to keep the old version running and hogging your system resources, I recommend that you remove the old versions.

apt purge php7.3 php7.3-common # Change 7.3 with all versions you want to purge.

... aaand that's it! You should now have a running PHP 7.4 with common packages/replacement packages of old version. Keep an eye on the error logs for potential backwards incompatible bugs in your code.
I have compiled a list of things changed, deprecated, added, and removed in PHP 7.4.