Upgrading PHP with the ondrej/php repository on an Ubuntu server

Published on 2022-07-25 • Modified on 2022-07-25

This post shows how to upgrade PHP with the ondrej/php repository on an Ubuntu server, step by step, to avoid forgetting something. Let's go! 😎

Configuration

The migration was done starting with the following versions:

  • Ubuntu Ubuntu 22.04.3 LTS
  • Apache 2.4.29
  • PHP 8.2

To migrate to PHP 8.3.

Introduction

The last time I upgraded PHP on my server, I did it by looking at some random post on the Internet. I wanted a memo here on my blog, so I don't have to search again and again the steps to migrate efficiently without fearing breaking something. As you can see I use an old Ubuntu LTS release, but thanks to the ondrej/php ppa, we have access to all PHP versions that would otherwise be unavailable.

Goal

The goal is to have the exact steps, so we don't forget anything, and we are sure all will go smoothly. It should almost be a copy and past process.

Save the server

I have a VPS (Virtual Private Server) hosted on OVH. In my dashboard, I have an option to create a complete snapshot of the server. So, I just have to make it before starting the migration.

Save the PHP extensions list currently installed

So we can compare it with the list related to the new PHP version with want to upgrade.

php -m > php82-modules.txt

We have something like this. This list can be helpful in case of a problem.

[PHP Modules]
calendar
Core
ctype
curl
date
dom
exif
FFI
...
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

Locate the current php.ini file

php -i | grep php.ini
Configuration File (php.ini) Path => /etc/php/8.2/cli
Loaded Configuration File => /etc/php/8.2/cli/php.ini

We can check and save the most important parameters. For example:

date.timezone = Europe/Paris

Update apt

sudo apt update

Check the installed PHP alternatives

update-alternatives --config php
There are 3 choices for the alternative php (providing /usr/bin/php).

  Selection    Path                  Priority   Status
------------------------------------------------------------
  0            /usr/bin/php.default   100       auto mode
  1            /usr/bin/php.default   100       manual mode
* 3            /usr/bin/php8.1        81        manual mode
  4            /usr/bin/php8.2        82        manual mode

We check that the version we want to migrate to is available. That's the case here. Exit without changing the version for now.

List the installed package for the current PHP version

apt list --installed | grep php8.2
php8.2-cli/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed,automatic]
php8.2-common/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed,automatic]
php8.2-curl/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-fpm/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-gd/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-imagick/jammy,now 3.7.0-4+ubuntu22.04.1+deb.sury.org+2 amd64 [installed]
php8.2-intl/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-mbstring/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-mysql/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-opcache/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-pgsql/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-readline/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed,automatic]
php8.2-soap/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-sqlite3/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-xml/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2-zip/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 amd64 [installed]
php8.2/jammy,now 8.2.14-1+ubuntu22.04.1+deb.sury.org+1 all [installed]

Do the same for the new version, compare both and list missing ones.

Install missing packages for the new version

Take the missing packages from the previous step, replace them in the following command, and then run it. We can install all of them in a single command.

sudo apt install php8.3-{curl,gd,imagick,intl,mbstring,mysql,opcache,pgsql,soap,sqlite3,xml,zip}

Install the Apache module for PHP 8.1

apt install libapache2-mod-php8.3

Deactivate the Apache module for PHP 8.0

a2dismod php8.2

Activate the Apache module for PHP 8.1

a2enmod php8.3

Select the new PHP alternative

update-alternatives --config php

This time, select the new version. Now, check that the CLI has the correct PHP version :

php -v
PHP 8.3.1 (cli) (built: Dec 21 2023 20:12:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.1, Copyright (c) Zend Technologies
with Zend OPcache v8.3.1, Copyright (c), by Zend Technologies

Conclusion

Upgrading PHP with the ondrej/php ppa is straightforward. I have migrated all my website on a shiny new VPS with Ubuntu 22.04 LTS in december 2023. I'll edit this blog post for each new PHP minor version.

That's it! I hope you like it. Check out the links below to have additional information related to the post. As always, feedback, likes and retweets are welcome. (see the box below) See you! COil. 😊

 More on the web  More on Stackoverflow

  Work with me!


Call to action

Did you like this post? You can help me back in several ways: (use the Tweet on the right to comment or to contact me )

Thank you for reading! And see you soon on Strangebuzz! πŸ˜‰

COil