How to Install PHP 8.1 on Rocky Linux 9

PHP 8.1 is a significant update of the PHP language released on November 25, 2021. This new release focuses on adding stability and improving performance in existing codebases while also introducing new features. One of the most notable new features is the addition of enums, which will provide a way to create more robust and type-safe code. Additionally, fibers have been added as a new way to manage concurrency, and the never return type will help developers catch errors early on.

Other new features include final class constants, intersection types, and read-only properties. This update also brings numerous changes to the existing PHP 8.0 release, including type handling and error reporting improvements. PHP 8.1 is a welcome addition that will help make PHP development more efficient and reliable.

In the following tutorial, you will learn how to import the REMI PHP Module and install PHP 8.1 on Rocky Linux 9 server or workstation desktop, along with some tips about configuring Nginx with FPM using the command line terminal.

Update Rocky Linux

First, update your system to ensure all existing packages are up to date. This will ensure no conflicts arise as best as possible during the installation.

sudo dnf upgrade --refresh -y

Install/Import PHP Remi Repository

The first task is to install the (EPEL) repository, which stands for (Extra Packages for Enterprise Linux). For newer users of Rocky Linux and similar distributions based on RHEL, EPEL contains the most commonly used software packages for Enterprise Linux.


First, enable the CRB.

sudo dnf config-manager --set-enabled crb

Next, install EPEL using the following (dnf) terminal command.

sudo dnf install \
    https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm \
    https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-9.noarch.rpm

Now that you have added the EPEL repository, enable the (Remi repository) with the following command.

sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-9.rpm -y

Situational – Remove Existing PHP Installation

Remove the previous version for users wanting to install PHP 8.1 but have already installed PHP or PHP-FPM. For example, remove PHP 8.0 for PHP 8.1.

Make sure to back up any configuration files.

sudo dnf remove php php-fpm -y

Then remove the rest of the package extensions.


sudo dnf remove php* -y

To reset the PHP module list is easy with the following command:

sudo dnf module list reset php -y

Now you can proceed to the next part of the tutorial.

Enable PHP 8.1 (Remi) Repository

A quick tip is to use the dnf module list command to see the options available and the default.

The following dnf module list command can do this:

dnf module list php

You will be prompted to import the GPG key for Remi’s repository.

Type (Y) to proceed if you encounter it.


Example output:

How to Install PHP 8.1 on Rocky Linux 9

Next, enable PHP 8.1 with the following command.

sudo dnf module enable php:remi-8.1 -y

Install PHP 8.1 / PHP-FPM 8.1

Now that you have added the Remi PHP repository and enabled PHP 8.1 to be the default version on your Rocky Linux system, you can install PHP 8.1 with the following command:

Apache (httpd) Users:

sudo dnf install php -y

Nginx Users:

sudo dnf install php-fpm -y

If you want to install the most commonly used extensions for PHP 8.1, use the following command.


sudo dnf install php-cli php-fpm php-curl php-mysqlnd php-gd php-opcache php-zip php-intl php-common php-bcmath php-imagick php-xmlrpc php-json php-readline php-memcached php-redis php-mbstring php-apcu php-xml php-dom php-redis php-memcached php-memcache

Note, remove the options you do not want; this is optional. It is highly recommended to only install and keep what modules you require from a performance and security standard.

You can use the following command to view modules loaded at any time.

php -m

Example output (They will 100% vary):

[PHP Modules]
apcu
bcmath
calendar
Core
ctype
curl
date
dom
exif
FFI
fileinfo
filter
ftp
gd
gettext
hash
iconv
igbinary
imagick
imap
intl
json
libxml
mbstring
memcache
memcached
msgpack
mysqli
mysqlnd
openssl
pcntl
pcov
pcre
PDO
pdo_mysql
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Xdebug
Zend OPcache

Depending on how many modules you have installed, this can be pretty large, and it is always recommended to keep an eye on this and remove any you do not need.

Lastly, use the following command for anyone interested in installing the development branch.

sudo dnf install php-devel

Additional developments tool, such as debugging tools, use the following command.


sudo dnf install php-xdebug php-pcov

This will install many dependencies, and unless you are developing with PHP or have some special requirement to install it, do not use this version.

Now that you have installed PHP 8.1 and the extensions, check the version with the following command:

php -v

Example output:

How to Install PHP 8.1 on Rocky Linux 9

PHP-FPM Installations of PHP 8.1

Unlike PHP-FPM installations on Debian/Ubuntu that use the (www-data) user, this isn’t the case with RHEL family installations. By default, the PHP-FPM service is designed to be run (Apache) user, which is incorrect since we are using Nginx, and this needed to be corrected.

Firstly, open following (www.conf) configuration file:

sudo nano /etc/php-fpm.d/www.conf

Next, replace the (Apache) user and group with the (Nginx) user and group.


Example from:

How to Install PHP 8.1 on Rocky Linux 9

Example to:

How to Install PHP 8.1 on Rocky Linux 9

To save, press (CTRL+O), then exit (CTRL+X).

Now you will too reload or restart your PHP-FPM service:

sudo systemctl restart php-fpm

Nginx Configurement

The Nginx server block needs the following example below for Nginx to process the PHP files.

Below is an example for all server {} blocks that process PHP files that need the location ~ .php$ added.


Example ONLY:

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

Test Nginx to make sure you have no errors with the adjustments made with the code above; enter the following.

sudo nginx -t

Example output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the Nginx service for installation to be complete.

sudo systemctl restart nginx

Comments and Conclusion

PHP is a server-side scripting language that powers many dynamic websites and web applications. PHP 8.1 is the latest major release of PHP, which brings several new features and improvements, including support for Unicode 13.0, JIT Compilation, and Named Arguments.

While PHP 8.1 is exciting, it is important to note that it is not yet considered stable 8.0 or the old stable 7.4; however that support is ending soon, so beware, you may find that many of your favorite software like WordPress or Plugins/Themes for CMS software may conflict until developers can update their code to work with PHP 8.1. In the meantime, if you’re looking to try out the new features of PHP 8.1, follow the tutorial on How to Install PHP 8.1 and configure it for use with Apache and Nginx.



Not what you were looking for? Try searching for additional tutorials.

Leave a Comment