Install/Upgrade PHP 8.1 on Fedora Linux 35

PHP 8.1 is a significant update of the PHP language released on November 25, 2021. This is a standard upgrade in the future from the existing PHP 8.0 release. The new PHP 8.1 brings enums, fibers, never return type, final class constants, intersection types, and 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 your Fedora 35 system.

Update Fedora System

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

sudo dnf upgrade --refresh -y

Install/Import PHP 8.1 Remi Repository

Currently, PHP 8.1 is not featured in Fedora 35. However, this can be easily fixed by installing and enabling the Remi Repository, a free and stable YUM/DNF repository mainly for the PHP stack. It contains packages for the latest versions of PHP.

To install import and install the remi repo, use the following (dnf) terminal command:

sudo dnf install http://rpms.remirepo.net/fedora/remi-release-35.rpm

Next, verify the installation.

dnf repolist | grep remi

Example output:

remi-modular                  Remi's Modular repository - Fedora 35 - x86_64

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 8.0 is the default PHP choice for standard Fedora 35 installations.

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 be prompted to import the GPG key for Remi’s repository. Type (Y) to proceed if you encounter it.

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 Fedora 35

Install PHP 8.1 on Fedora

Now that you have added the Remi PHP repository and enabled PHP 8.1 to be the default version on your Fedora 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 Fedora 35

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

Note, you will be prompted for more GPG key imports.

Example:

How to Install PHP 8.1 on Fedora 35

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

Optionally, 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-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 Fedora 35

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

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 Fedora 35

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 Fedora, 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 Fedora 35

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 coming out of beta and not considered stable, such as 8.0 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.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!