← Other topics

Upgrading to PHP 8.2 (Ubuntu with Apache)

Video Notes

In this guide I’ll walk through the process of upgrading PHP on an Ubuntu server running Apache. For my example, I’ll do a PHP 8.0 → 8.2 upgrade, but you should be able to apply the same general principles to other version jumps.

In the notes that follow, when you see reference to php8.x swap out x with the relevant version number for the upgrade you’re doing.

Running an Nginx web server instead of Apache? Go here: Upgrading PHP (Ubuntu with Nginx)

Output phpinfo

Before you begin the upgrade, create a PHP file on your server that you can access in the browser that invokes PHP’s phpinfo function:

<?php

phpinfo();

This will output useful information about your server and PHP setup that can be used to confirm the update process was successful.

In addition to updating PHP, you will need to update any PHP related packages your server is using. To help with this, run the following command which will create a text file called php-packages.txt that will contain a list of all your currently installed PHP packages.

> sudo dpkg -l | awk '/^ii/{print $2}' | grep php | tee php-packages.txt

Example resulting php-packages.txt file:

> cat php-packages.txt
libapache2-mod-php8.1
php-common
php8.1
php8.1-apcu
php8.1-cli
php8.1-common
php8.1-gd
php8.1-mysql
php8.1-opcache
php8.1-readline

Add repository

Next, we’ll use the Linux package manager apt to target a repository called ondrej/php PPA which contains all the PHP software we’ll need:

> sudo add-apt-repository ppa:ondrej/php
> sudo apt update

Install PHP and packages

Now we’re ready to install PHP and its related packages referencing the text file you created in the above step. Here’s a template for a command you can use to do this:

> sudo apt install php8.x php8.x-packagename php8.x-packagename {etc}

Note that each package follows the pattern of php8.x-packagename, e.g. php8.2-mysql.

As an example, based on my above packages list and an update to PHP 8.2 my install command would look like this:

> sudo apt install php8.2 libapache2-mod-php8.2 php-common php8.2-apcu php8.2-cli php8.2-common php8.2-gd php8.2-mysql php8.2-opcache php8.2-readline

Update your server’s PHP handler

Next you need to configure your server to use the updated version of PHP. How you do this will depend on what PHP handler you’re using - either FPM (FastCGI Process Manager) or Apache’s PHP module.

To determine which handler you’re using, look at the output for Server API on your PHP info page. Once you know your handler, follow one of the two sets of instructions below...

Setup A. Apache with PHP as an Apache module

Enable the new Apache PHP module:

> sudo a2enmod php8.x

Disable the previous Apache PHP module (swap x with whatever previous version you were using):

> sudo a2dismod php8.x 

Restart Apache to make the changes take effect:

> sudo systemctl restart apache2

Setup B. Apache with PHP-FPM

Enable the new FPM:

> sudo a2enconf php8.x-fpm

Disable the previous version of FPM (swap x with whatever previous version you were using):

> sudo a2disconf php8.x-fpm 

Restart Apache to make the changes take effect:

> sudo service apache2 restart

Confirm it worked

Refresh your phpinfo page and confirm you’re seeing the updated PHP version number:

You can also confirm that PHP from command line is reporting the correct version with the command php --version:

> php --version
PHP 8.2.3 (cli) (built: Feb 14 2023 16:57:50) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.3, Copyright (c), by Zend Technologies

Sync changes to php.ini

Your final step is to make sure any configurations you might have made to PHP in your previous install are reflected in your upgraded install. To do this, you will need to synchronize differences between your old and new PHP config files, php.ini. To help with this, I made a simple PHP script that will examine both files and output any setting differences.

To use this script, you need to first locate the path to your new/current php.ini file from the Loaded Configuration File value in your phpinfo output:

Example path:

/etc/php/8.2/apache2/php.ini

From this path we can infer the path to your previous php.ini file by changing the version number:

/etc/php/8.1/apache2/php.ini

Next, create a PHP file on your server that you can access in the browser called ini-diff.php that contains this code. Within the code, update the first two variables to reflect your current and previous php.ini paths. E.g.:

# UPDATE THESE TWO PATHS:
$current = '/path/to/current/php.ini';
$previous = '/path/to/previous/php.ini';

When you run this file, it will outline any setting differences in your previous and current .ini file. E.g.:

Use this information to update your current php.ini file to reflect any settings you wish to persist in your upgraded PHP.

When you’re done, make the changes take effect by restarting Apache:

> sudo systemctl restart apache2

You can reload your phpinfo page to confirm any setting changes were applied.

Clean up

When you’re done you can (but don’t have to) purge your old unused PHP packages using the following command:

> sudo apt purge php8.x*
If this info helped you out, toss a coin in the tip jar?
← Other topics