How to Upgrade PHP on AlmaLinux and Rocky Linux With Remi
In this article12 sections
AlmaLinux, Rocky Linux and Red Hat Enterprise Linux are built for stability, so their default repositories carry an older PHP version and keep it for years, applying only security fixes. That is a strength for a long-lived server, and a problem if your application needs a newer PHP. The usual answer is the Remi repository, which provides current PHP builds for this family of distributions, with a module stream you can switch between versions. This guide upgrades PHP on AlmaLinux or Rocky 9 using Remi, with Nginx or Apache, covering the repository, the module reset, the extensions, the PHP-FPM socket, SELinux and the rollback.
It applies to a SiteHarbour VPS or any server in this family where you have root access. The examples move to PHP 8.4. Check the version names on the Remi site for the release you run, because they follow the PHP release cycle.
How PHP is packaged on this family
These distributions use module streams: a set of packages for one PHP version that you enable as a group. Only one stream can be enabled at a time, and you must reset the old one before you enable a new one. Remi extends this with streams named like php:remi-8.4. The PHP-FPM service is called php-fpm, not versioned, so upgrading replaces the one service rather than adding a second one beside it. This differs from Ubuntu, and it means the rollback needs more care, so snapshot first.
Before you start
- Take a snapshot of the server and dump the databases.
- Record the current state:
php -v
php -m | sort > ~/php-modules-before.txt
sudo cp -a /etc/php.ini ~/php.ini.before
sudo cp -a /etc/php-fpm.d ~/php-fpm.d.before
dnf module list php
Expected: the version line, a saved extension list, backups of the main settings, and the list of PHP streams with the enabled one marked. The compatibility of your own code is covered in how to check your code for PHP compatibility before you upgrade.
Step 1: Add the Remi repository
Install the EPEL repository and the Remi release package for your major version. For version 9:
sudo dnf install -y epel-release
sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm
Expected: the packages install and the new repository appears in dnf repolist. Confirm the download source is the official Remi address, because you are about to take PHP updates from it.
Step 2: Switch the PHP module stream
Reset the enabled stream, then enable the new one:
sudo dnf module reset php -y
sudo dnf module enable php:remi-8.4 -y
dnf module list php | grep -i remi-8.4
Expected: the line for remi-8.4 shows [e], which means enabled. If you skip the reset, dnf refuses to switch streams.
Step 3: Upgrade the packages and the extensions
Update the PHP packages that are already installed, then add any extension that is on your old list but missing now. Extension package names start with php-:
sudo dnf upgrade -y 'php*'
sudo dnf install -y php-fpm php-cli php-common php-mysqlnd php-mbstring php-xml \
php-gd php-intl php-zip php-opcache php-bcmath
php -v
Expected: the new version. Compare php -m | sort with ~/php-modules-before.txt and install anything that is missing. A missing extension shows up later as an undefined function error.
Step 4: Check the settings the upgrade may have replaced
The package upgrade keeps your edited configuration files and writes new defaults beside them with a .rpmnew suffix. Look for them and compare:
sudo find /etc -name "php*.rpmnew" -o -name "www.conf.rpmnew"
sudo diff -u ~/php.ini.before /etc/php.ini
Check memory_limit, upload_max_filesize, post_max_size, max_execution_time, and the OPcache values. In the pool file /etc/php-fpm.d/www.conf, check user and group. On this family, the default is apache, which is wrong for Nginx. For Nginx, set both to nginx, and set the socket owner to match:
user = nginx
group = nginx
listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
Size pm.max_children from measured memory, as shown in how to optimize PHP-FPM workers on limited RAM, not from a copied number.
Step 5: Restart PHP-FPM and point the web server at it
sudo php-fpm -t
sudo systemctl enable --now php-fpm
sudo systemctl restart php-fpm
systemctl is-active php-fpm
Expected: the test is successful and the service is active. For Nginx, the socket line in the server block is fastcgi_pass unix:/run/php-fpm/www.sock;, and it does not change, since the socket name is not versioned. Reload Nginx to be safe:
sudo nginx -t && sudo systemctl reload nginx
For Apache, PHP-FPM is reached through proxy_fcgi, normally with a SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost" line, and the configuration file does not change either. Test and reload with sudo apachectl configtest && sudo systemctl reload httpd.
Step 6: Check SELinux
SELinux is on by default on this family, and it blocks things that are not obviously about PHP. After an upgrade, check its mode and recent denials:
getenforce
sudo ausearch -m avc -ts recent | tail -n 20
Expected: Enforcing, and no recent denials. If PHP cannot connect to a database on another host or write to a folder, the denial names the rule. Fix it with the boolean or the file context it names, such as sudo setsebool -P httpd_can_network_connect_db 1 or sudo semanage fcontext and restorecon for a writable folder. Do not turn SELinux off to make the error go away, because that removes a protection you did not need to remove.
Step 7: Confirm the version and watch the logs
echo '<?php echo PHP_VERSION;' | sudo tee /var/www/html/version-check.php
curl -s http://localhost/version-check.php
sudo rm /var/www/html/version-check.php
Expected: the new version. Check /var/log/php-fpm/www-error.log and the web server’s error log after a few hours and the next day. The usual problems and fixes are in how to fix common errors after a PHP upgrade. Remember cron jobs, queue workers and scheduled tasks use the command-line PHP, and workers keep the old code until you restart them.
Rollback
Because the module stream replaces the packages, a rollback is a downgrade, which is why the snapshot comes first. If you have a snapshot, restore it. Without one, reset the stream and enable the old one, then run sudo dnf distro-sync 'php*' and restore the configuration backups. Test the site and the database afterwards. This is slower and riskier than on Ubuntu, so for a production server test the whole upgrade on a clone first.
Common mistakes
- Forgetting
dnf module reset php. dnf refuses to switch streams. - Leaving the pool user as
apacheon an Nginx server. The socket permissions break, and the site returns 502. - Disabling SELinux to fix an error. Read the denial and fix the rule.
- Ignoring
.rpmnewfiles. New defaults sit beside your edited files and can differ. - No snapshot. The rollback on this family is a downgrade.
Frequently asked questions
Is Remi safe to use?
It is widely used on this family and is the repository most administrators use for current PHP. You take PHP updates from it, so keep the server updated and read the changes.
Can I run two PHP versions side by side?
Remi offers a separate set of versioned packages for that, with different service names. The module stream approach in this guide runs one version at a time.
Why does the site return 502 after the upgrade?
Usually the pool user or the socket permissions do not match the web server’s user. Check Step 4, and the web server’s error log.
Do I need a reboot?
No. Restarting PHP-FPM and reloading the web server is enough.
On AlmaLinux and Rocky, a PHP upgrade is a repository, a module stream switch, a package upgrade and a PHP-FPM restart, with SELinux and the pool user as the two traps. Snapshot first, record the old settings, reset and enable the new stream, install the extensions, fix the pool user for your web server, and watch the logs. Because the rollback is a downgrade, test on a clone before you do it on production. For the Ubuntu version of the same job, read how to upgrade PHP on Ubuntu with Nginx and PHP-FPM.
Want this done for you? If you would rather have an engineer plan and carry out the work on your own site, you can hire the author through the Upwork profile of Munaf Sheikh.