How to Install PHP 8.2, 8.1, 8.0, 7.4 on Rocky Linux EL9 or EL8


With PHP being a popular language for web development, Rocky Linux 9 and 8 now offer the ability to install multiple versions on your system with the Remi PHP repository. The versions available include 8.2, 8.1, 8.0, and 7.4 – all offered with their open-source nature, allowing them to integrate seamlessly into various servers, databases & technologies! With this guide, you can also learn how to ensure they are installed properly on your Rocky Linux system.

Step 1: Update Rocky Linux

Before installing any new software, updating the system is always a good idea. To do this, open a terminal and run the following commands:

sudo dnf upgrade --refresh

Step 2: Import PHP Remi Repository

The EPEL (Extra Packages for Enterprise Linux) repository must be installed to import the Remi PHP repository. EPEL is particularly useful for newer users of distributions such as Rocky Linux, which is based on RHEL and offers a wide range of commonly used software packages for Enterprise Linux.

Ensure you import the correct repository for your Rocky Linux distribution version below.

Import Remi PHP Repository for Rocky Linux 9

First, enable the CRB.

sudo dnf config-manager --set-enabled crb

With CRB enabled, run the following command to install both versions of EPEL.

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

Use the following command that will import the EL 9 Remi repository.

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

Import Remi PHP Repository for Rocky Linux 8

First, enable Rocky Linux 8 EPEL with the following command.

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

Now use the following command to import the Remi EL 8 repository that contains PHP.

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

Step 3: Enable PHP Remi Repository

First, list the modules of PHP to ensure you can see all versions of the REMI PHP repository, then you can fine-tune your selection once confirmed.

Run the following command in your terminal.

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:

Next, enable the version of PHP you want to install.

Enable PHP 8.2 on Rocky Linux.

sudo dnf module enable php:remi-8.2 -y

Enable PHP 8.1 on Rocky Linux.

sudo dnf module enable php:remi-8.1 -y

Enable PHP 8.0 on Rocky Linux.

sudo dnf module enable php:remi-8.0 -y

Enable PHP 7.4 on Rocky Linux.

sudo dnf module enable php:remi-7.4 -y

Step 4: Install PHP 8.2, 8.1, 8.0 or 7.4

With the Remi PHP repository enabled on your system, you can install the desired version of PHP. The options provided guide you in selecting between Apache and Nginx. Still, you can go the extra mile by installing specific extensions that cater to your CMS or software development requirements if you have the required knowledge.

See also
How to Install NVIDIA Drivers on Rocky Linux EL9 or EL8

Apache (httpd) PHP:

sudo dnf install php php-cli -y

Nginx PHP:

sudo dnf install php-fpm php-cli -y

Once installed, run the following command to confirm PHP is installed.

php -v

To acquire the most popular extensions for the desired version of PHP, execute the command specified below.

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 only to 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

As the number of modules installed can cause significant performance issues, monitoring and removing any unnecessary modules regularly is suggested.

For those looking to install the development branch, the following command can be used.

sudo dnf install php-devel

If you want to add more development tools, such as debugging tools, utilize the command below.

sudo dnf install php-xdebug php-pcov

It’s important to note that installing this version will bring in multiple dependencies, and it’s not recommended to do so unless you have a specific need for it in your PHP development or any special requirement.

Step 5: Configure PHP-FPM Nginx User

When it comes to PHP-FPM installations on distributions like Debian/Ubuntu, they typically use the “www-data” user. However, this is not the default for installations on the RHEL family. The PHP-FPM service is intended to run under the “Apache” user by default, which is unsuitable for using Nginx. Therefore, it’s necessary to make adjustments.

Firstly, open the 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:

Example to:

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

Now you will restart your PHP-FPM service.

sudo systemctl restart php-fpm

Example Nginx PHP-FPM Server Block Code

For Nginx to handle PHP files, the server block requires a specific configuration, as shown in the example below. This example applies to all server{} blocks that process PHP files and requires the inclusion of “location ~ \.php$.”

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;
    }

To verify that the adjustments made to the code above did not introduce any errors, run the following command to test Nginx.

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

To complete the PHP-FPM introduction, the Nginx service must be restarted.

sudo systemctl restart nginx

Conclusion

The article has gone over how to install PHP on a Rocky Linux by enabling the Remi PHP repository, setting the desired version of PHP as the default, installing commonly used extensions, and configuring the Nginx server block to process PHP files. It’s important to keep an eye on dependencies and remove unnecessary modules. It’s also important to test Nginx for errors before restarting the service in the future, especially in a live environment.

Leave a Comment

Share to...