Upgrading Apache from PHP 7.3 to PHP 7.4
Published: 2021-06-03 17:49:42

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</h2>
##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 <a href="https://deb.sury.org/" target="_blank" rel="noreferrer noopener">Ondřej Surý's</a> PPA repository GPG key.
```
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
```
And finally the <a href="https://deb.sury.org/#packages" target="_blank" rel="noreferrer noopener nofollow">repository</a> 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, <a href="https://deb.sury.org/#donate" target="_blank" rel="noreferrer noopener nofollow">please consider donating</a> 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 the `phpinfo.php` file you created in step 1 and confirm the **PHP Version** has changed to 7.4.

---
<center>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.</center>

---
<center><small>_Originally posted on [my blog](https://words.samtarling.co.uk/2021/06/03/upgrading-apache-from-php-7-3-to-php-7-4)_</small></center>