DEV Community

Sam
Sam

Posted on

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!

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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();
?>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

2) Updates

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

Let's update APT first.

sudo apt update
Enter fullscreen mode Exit fullscreen mode

And then install any applicable updates.

sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

5) Install PHP 7.4

We can now install PHP 7.4 like any other package.

sudo apt -y install php7.4
Enter fullscreen mode Exit fullscreen mode

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

php --version
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

sudo a2enmod php7.4
Enter fullscreen mode Exit fullscreen mode

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

sudo service apache2 restart
Enter fullscreen mode Exit fullscreen mode

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


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.

Originally posted on my blog

Top comments (0)