Install/Upgrade PHP 8.1 on AlmaLinux 8

PHP 8.1 is a significant update of the PHP language that was “officially” released on November 25, 2021. As we advance from the existing PHP 8.0 release, this is a standard upgrade. The new PHP 8.1 brings enums, fibers, never return type, final class constants, intersection types, read-only properties amongst the long list of new features and changes.

In the following tutorial, you will learn how to import the REMI Module and install PHP 8.1 on AlmaLinux 8.


  • Recommended OS: AlmaLinux 8.
  • User account: A user account with sudo privilages or root access (su command).

Updating Operating System

Update your AlmaLinux operating system to make sure all existing packages are up to date:

sudo dnf upgrade --refresh -y

The tutorial will be using the sudo command and assuming you have sudo status.

To verify sudo status on your account:

sudo whoami

Example output showing sudo status:

[joshua@localhost ~]$ sudo whoami

To set up an existing or new sudo account, visit our tutorial on How to Add a User to Sudoers on AlmaLinux.

Install/Import PHP 8.1 Remi Repository

Currently, PHP 8.1 is not featured in AlmaLinux’s AppStream, given it’s still relatively new and hasn’t filtered down the chain in RHEL’s repositories yet. However, you can install PHP from (Remi) repository, a free-to-use third-party repository that deploys the latest PHP 8.1 builds.

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

To install EPEL, use the following (dnf) terminal command:

sudo dnf install epel-release

Example output:

How to Install PHP 8.1 on AlmaLinux 8

Type “Y,” then press the “ENTER KEY” to proceed with the installation.

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

sudo dnf install dnf-utils

Example output:

How to Install PHP 8.1 on AlmaLinux 8

Type “Y,” then press the “ENTER KEY” to proceed with the installation.

Next, use the (dnf) command to update your repository list:

sudo dnf update

You will notice a query in your terminal about importing the (GPG key) for the Remi repository.


How to Install PHP 8.1 on AlmaLinux 8

Type “Y,” then press the “ENTER KEY” to import the GPG key and complete the installation.

Note, you will be prompt x2 more times to type “Y” to import more GPG keys. This is fine to do.

Situational – Remove Existing PHP Installation

For users wanting to install PHP 8.1 but have already installed PHP or PHP-FPM, remove the previous version. 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

PHP 7.2 is the default PHP choice for standard installation on Rhel/AlmaLinux with Remi’s repository. A quick tip is to use the (list php) command to see the options available and the default.

The following dnf module list command can do this:

sudo dnf module list php

You will get the following output as below. Note the (d) tag for default PHP to be installed:


How to Install PHP 8.1 on AlmaLinux 8

As you can see above, the (d) tag is next to PHP 7.2, which you will need to reset and change to install PHP 8.1.

Next, enable PHP 8.1 with the following command:

sudo dnf module enable php:remi-8.1

Example output:

How to Install PHP 8.1 on AlmaLinux 8

Install PHP 8.1

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

Apache (HTTPD) Users:

sudo dnf install php

Nginx Users:

sudo dnf install php-fpm

Example output:

How to Install PHP 8.1 on AlmaLinux 8

Type “Y,” then press the “ENTER KEY” to proceed with the installation.

Note, you will be prompted for more GPG key imports, Type Y to any that show in your terminal.

If you would like 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-imap 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.

Example output:

How to Install PHP 8.1 on AlmaLinux 8

Type “Y,” then press the “ENTER KEY” to proceed with the installation.

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

sudo dnf install php-devel -y

This will install lots of 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 AlmaLinux 8

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 on AlmaLinux, 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:

How to Install PHP 8.1 on AlmaLinux 8

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, example for all server {} blocks that process PHP files that need the location ~ .php$ added.

    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 Nginx service for installation to be complete.

sudo systemctl restart nginx

Comments and Conclusion

In the tutorial, you have learned how to install PHP 8.1 and configure how to use it with Apache and Nginx. PHP 8.1 is exciting. However, at the current moment, it is still not as stable as the 8.0 release or the old stable 7.4, 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.

Do some research, prepare, and have PHP 7.4 or 8.0 installed and ready to replace if anything goes wrong when making the switch. The stable versions such as 8.0 are still actively developed, and packages are pushed simultaneously along with the 8.1 packages.

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

Leave a Comment