The newest Nextcloud instances require at least PHP 8.2 or above. Nextcloud 32 will not support PHP 8.1, and you may have already seen a deprecation message on Nextcloud 30 and above. While this article is not just about Nextcloud but also about PHP, it primarily addresses the issue of upgrading PHP for Nextcloud servers. However, the steps can be applied to any server that requires a newer version of PHP. Read on to learn how to upgrade PHP on Ubuntu servers or desktops.
The error you may see on Nextcloud instances will look like the following:
Error: You are currently running PHP 8.1.2-1ubuntu2.20. PHP 8.1 is deprecated since Nextcloud 30. Nextcloud 32 may require at least PHP 8.2. Please upgrade to one of the officially supported PHP versions provided by the PHP Group as soon as possible.
So let’s dive into it.
Add repository
First of all update your system if not done yet.
apt update && apt upgrade
Add the ondrej repository.
add-apt-repository ppa:ondrej/php
apt update && apt upgrade
Upgrade PHP
To be safe stop the PHP-FPM (if installed) and Apache or Nginx server.
systemctl stop php8.1-fpm
systemctl stop apache2
Next, run the following command to upgrade to PHP 8.3. If you prefer to use PHP 8.2 or 8.4, adjust accordingly. I need PHP 8.3, so I will proceed with it. Some of the modules are specific to Nextcloud; if you don’t need them, you can remove them.
apt install libapache2-mod-php8.3 php8.3 php8.3-gmp php8.3-bcmath php8.3-gd php8.3-mysql php8.3-curl php8.3-mbstring php8.3-intl php8.3-imagick php8.3-xml php8.3-zip php8.3-fpm php8.3-redis php8.3-fileinfo php8.3-apcu php8.3-opcache php8.3-ldap php8.3-bz2
Once done, I assume you have PHP 8.1 with FPM. We will disable its configuration and remove it from boot.
a2disconf php8.1-fpm
systemctl disable php8.1-fpm
You can now start and enable PHP 8.3.
a2enconf php8.3-fpm
systemctl enable php8.3-fpm
systemctl start php8.3-fpm
To switch versions, use the following command and select the version you want.
update-alternatives --config php
PHP configuration files are in /etc/php/8.1
or /etc/php/8.3
.
Remove old versions
If you want to remove any version, you can do so by running the following command.
apt purge php8.1-common
This will remove all the PHP 8.1-related modules and files.