Upgrading Apache from PHP 7.3 to PHP 7.4

Okay, I know we’re not upgrading Apache, but y’know what I mean – let’s upgrade a Debian web server using PHP 7.3 to PHP 7.4!

I’ll also cover some pointers on if that web server is running WordPress.

0) Assumptions

I’m assuming:

  • The server runs Debian 9 (Stretch) or Debian 10 (Buster) with Apache version 2.4.38* (though it should work for other versions)
  • Apache is using the PHP module, not PHP-FPM
  • You have sudo and SSH access
  • You’ve made backups and any sites you’re hosting can tolerate downtime 😉

* You can get this info by running /usr/sbin/apache2 -v

1) Gather information

Default PHP version

Just as a sanity check, check to make sure the default PHP installation is 7.3 by running:

php --version

You should get something which looks like this:

PHP 7.3.27-1~deb10u1 (cli) (built: Feb 13 2021 16:31:40) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.27, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.27-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies

Apache PHP version

Create a PHP file named phpinfo.php with the following content in, and place it somewhere publicly accessible (e.g. /var/www/html/phpinfo.php)

<?php
phpinfo();
?>

Navigate to this file (e.g. http://{server IP}}/phpinfo.php) and take note of the PHP Version in the header.

Enabled PHP modules

A common issue people face once they’ve successfully completed a PHP upgrade is missing modules – do you know which PHP modules you have enabled at the moment?

Didn’t think so 😅 so let’s get a list. We can view the currently enabled PHP modules, and also save a copy to ~/php_mods.txt, by running:

php -m | tee ~/php_mods.txt

For example, here’s some of the enabled PHP modules I have on a test machine:

samt@testvm-1:~$ php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
ftp
gd
gettext
hash
iconv
imagick
intl
json
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

2) Updates

Before upgrading PHP, you should ensure your web server is up to date.

Let’s update APT first.

sudo apt update

And then install any applicable updates.

sudo apt upgrade -y

You should then ideally reboot your web server if possible by running sudo reboot

3) Add PPA repository

We’ll start by ensuring a couple of required packages are installed.

sudo apt -y install lsb-release apt-transport-https ca-certificates

And then we can add Ondřej Surý’s PPA repository GPG key.

sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

And finally the repository itself.

echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

If you use Ondřej’s repository to install PHP 7/8 on a server for your business, please consider donating to him!

4) Update package list

Now that we have Surý’s repository installed, let’s update our package list.

sudo apt update

5) Install PHP 7.4

We can now install PHP 7.4 like any other package.

sudo apt -y install php7.4

We can then check to see if this has automatically updated the default PHP by running:

php --version

Which should return something similar to:

PHP 7.4.18 (cli) (built: May  3 2021 11:59:44) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.18, Copyright (c), by Zend Technologies

6) Install PHP modules

You may find that some PHP modules (the ones you checked in step 1) are now missing – normally this presents as an error in the web application you might be hosting, such as WordPress.

Some common extensions can be installed in one go by running:

sudo apt-get install php7.4-{bcmath,bz2,intl,gd,mbstring,mysql,zip,curl}

Refer to the file you created in step 1 (~/php_mod.txt) for any other missing ones.

7) Configure Apache

Now that PHP 7.4 is installed, we need to load the relevant PHP module into Apache.

First we’ll remove the old module.

sudo a2dismod php7.3

You’ll be prompted to restart Apache, don’t do that yet, and instead run:

sudo a2enmod php7.4

Provided you don’t get any error messages, you can now restart Apache with:

sudo service apache2 restart

Finally, reload your phpinfo.php file you created in step 1 and confirm the PHP Version has changed to 7.4.


Nice work! 😁

If you’ve made it this far, and everything is still working – well done! You’ve taken a really important step to ensure your web server is secure, and you might even notice a performance increase.

If you’re running WordPress, read on..


WordPress on PHP 7.4

Your first port of call after upgrading PHP (or anything really..) on a web server which is running WordPress is the “Site Health” page in the admin dashboard.

You can find this under “Tools” -> “Site Health”

This will display a report, and its normally pretty good at finding errors caused by an upgrade – I blogged about the Failed to enable crypto stream_socket_client() error I faced when I upgraded my web server, and that was caused by a missing module.

This command will install (if required) all the recommended PHP modules for a WordPress server:

sudo apt install php7.4-curl \
php7.4-dom \
php7.4-exif \
php7.4-fileinfo \
php7.4-imagick \
php7.4-json \
php7.4-mbstring \
php7.4-mysqli \
php7.4-xml \
php7.4-zip

Alternatively, you can use a quick install script I wrote. First make sure you’ve got git installed:

sudo apt install -y git

Then clone the repo:

cd ~ && git clone https://github.com/samtarling/wordpress-php-module-installer.git

And then run the install script:

cd wordpress-php-module-installer && ./install.sh

Once you’re all done, it’s worth doing a restart of Apache

sudo service apache2 restart

Awesome! 😄


I don’t normally write these sorts of posts, but I actually quite enjoyed it – I’d love to know what you thought!

3 comments

  1. Thank you! You made me realize I had also php7.4-curl module missing, causing failed to load crypto error in my Drupal sites. It took me two days to figure this out and I learnt a lot, but I finally solved it thanks to you! All the best!

Leave a Reply