How to Install PHP 7.4 on CentOS 9 Stream

PHP 7.4 is a significant update of the PHP language that was “officially” released on November 28, 2019. This is a standard upgrade from now on from the existing PHP 7.3 release to PHP 7.4, which is the last version in the 7 PHP series that brings in arrow functions for cleaner one-liners, preloading for improved performance, typed properties in classes, improved type variances, spread operator in arrays and much more.

In the following tutorial, you will learn how to install PHP 7.4 on Centos 9 Stream by importing the EPEL 9 and REMI repositories. The tutorial will focus on installing the development kit extensions and some tips for Nginx and PHP-FPM using the command line terminal.

Situational – Remove DNF Subscription Manager

For users who encounter issues with “This system is not registered with an entitlement server. You can use subscription-manager to register,” then use the following command to remove the subscription manager.

sudo dnf erase subscription-manager -y

Example output:

How to Install PHP 7.4 on CentOS 9 Stream

As mentioned above, all the subscription-based lockouts have been removed.

Update CentOS Stream

Before you continue, your system is advised to ensure all existing packages are up to date to avoid system conflicts during the installation and good system maintenance.

sudo dnf upgrade --refresh -y

Install PHP Remi Repository

Currently, PHP 7.4 is not featured in CentOS Stream’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 the (Remi) repository. This free-to-use third-party repository deploys the latest PHP 7.4 builds and the option to upgrade to 8.0, 8.1, or higher later on.

The first task is to install the (EPEL) repository, which stands for (Extra Packages for Enterprise Linux). For newer users of CentOS Stream 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:

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

Next, refresh your repository cache to import the GPG keys automatically.

sudo dnf update --refresh -y

Situational – Remove Existing PHP Installation

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

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 7.4 (Remi) Repository

PHP 7.2 is the default PHP choice for standard installation on Rhel/CentOS Stream 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.

Example:

How to Install PHP 7.4 on CentOS 9 Stream

Next, enable PHP 7.4 with the following command:

sudo dnf module enable php:remi-7.4 -y

Install PHP 7.4

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

Apache (HTTPD) Users:

sudo dnf install php -y

Nginx Users:

sudo dnf install php-fpm -y

If you would like to install the most commonly used extensions for PHP 7.4, use the following command:

sudo dnf install php-cli 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.

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

php -m

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 any PHP 7.4 and the extensions, check the version with the following command:

php -v

Example output:

How to Install PHP 7.4 on CentOS 9 Stream

PHP-FPM Installations of PHP 7.4

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 CentOS Stream, 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:

How to Install PHP 7.4 on CentOS 9 Stream

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

Next, 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 of 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 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 7.4 on CentOS 9 Stream for developers or, for the most part, servers that require it for their web application or CMS, such as WordPress.

7.4 is considered old stable, with PHP 8.0 reaching the maturity of being stable; however, for non-developers and users installing PHP to run CMS systems such as WordPress often you will find developers still are updating to the PHP 8.0/8.1 series, so using 7.4 is a solid option and still performs well even though its a generation behind now.

PHP 7.4 end of life will be reached on 28th November 2022, migrating to PHP 8.0.



Follow LinuxCapable.com!

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