How to Install PHP 8.1 on Fedora 36 Linux

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 among new features and changes.

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

Update Fedora

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

Currently, PHP 8.1 is featured in Fedora. Still, you should import the PHP Remi repository to get the latest stable updates, the same version installed in Fedora comes from Remi regardless, so it’s all the same, just faster to receive updates.

First, import and install the Remi repo; use the following terminal command.

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

Next, verify the installation.

dnf repolist | grep remi

Example output:

How to Install PHP 8.1 on Fedora Linux 36 Linux

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:

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 -y

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 -y

Nginx Users:

sudo dnf install php-fpm -y

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.

To view modules loaded at any time, you can use the following command.

php -m

Example output (They will 100% vary):

How to Install PHP 8.1 on Fedora Linux 36 Linux

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 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 Fedora Linux 36 Linux

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 Linux 36 Linux

For nano editor, 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

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, PHP is considered more 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.

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!