Best Way To Install PHP 8.2 on Rocky Linux 9

In this tutorial, we want to teach you How To Install PHP 8.2 on Rocky Linux 9.

PHP is known as a general-purpose scripting language that can be used to develop dynamic and interactive websites. It was among the first server-side languages that could be embedded into HTML, making it easier to add functionality to web pages without needing to call external files for data. Its use has evolved over the years, with regular upgrades (PHP 8.2 Latest Stable Release) adding features and unlocking new capabilities.

Steps To Install PHP 8.2 on Rocky Linux 9

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Rocky Linux 9.

Install PHP 8.2 on Rocky Linux 9

To install the latest release of PHP, you must add the Remi repository to your server. First, update your local package index with the following command:

sudo dnf update -y

Then, install the Epel Repository on your Rocky Linux server:

sudo dnf install epel-release -y

Add Remi Repository

Now you can easily use the command below to install the Remi repository:

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

Rerun the system update:

sudo dnf update -y

Note: If you have installed previous versions of PHP and PHP-FPM, you need to remove them by using the command below:

sudo dnf remove php php-fpm -y

Then remove the rest of the package extensions with the command below:

sudo dnf remove php* -y

Enable PHP 8.2 Remi Repository

At this point, you need to reset the PHP module list on Rocky Linux 9:

sudo dnf module list reset php -y
Output

Rocky Linux 9 - AppStream
Name      Stream       Profiles                       Summary
php       8.1          common [d], devel, minimal     PHP scripting language

Remi's Modular repository for Enterprise Linux 9 - x86_64
Name      Stream       Profiles                       Summary
php       remi-7.4     common [d], devel, minimal     PHP scripting language
php       remi-8.0     common [d], devel, minimal     PHP scripting language
php       remi-8.1     common [d], devel, minimal     PHP scripting language
php       remi-8.2     common [d], devel, minimal     PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

From the output, you can see that the (d) tag is next to PHP 8.1, which you will need to reset and change to install PHP 8.2.

Now you need to enable PHP 8.2 on Rocky Linux 9 with the command below:

sudo dnf module enable php:remi-8.2

At this point, you can easily use the command below to install PHP 8.2:

sudo dnf install php -y

If you would like to install the most commonly used extensions for PHP 8.2, 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

Finally, you can use the following command for anyone interested in installing the development branch:

sudo dnf install php-devel -y

To verify your PHP 8.2 installation on Rocky Linux 9, you can check its version:

php -v
Output
PHP 8.2.0 (cli) (built: Dec  6 2022 14:26:47) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.0, Copyright (c), by Zend Technologies

Configure PHP-FPM As an Nginx User

By default on Rocky Linux 9, the PHP-FPM service is designed to be run by (Apache) user. If you are using Nginx you need to make configuration changes at (www.conf).

You can open the file with your favorite text editor, here we use vi:

sudo vi /etc/php-fpm.d/www.conf

Find the user and group directives and change them to Nginx as shown below:

user = nginx
group = nginx

When you are done, save and close the file.

Reload the PHP-FPM service to apply the changes:

sudo systemctl restart php-fpm

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.

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

For more information, you can visit the PHP Documentation page.

Conclusion

At this point, you have learned to Install PHP 8.2 on Rocky Linux 9. You can easily add a Remi repository and enable your desired PHP version on Rocky Linux 9. I hope you enjoy it.

You may like these articles on the Orcacore website:

How To Install Docker on Rocky Linux 9

Reset Root Password on Rocky Linux 9

Install LEMP Stack on Rocky Linux 9

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!