Best Way To Install PHP 8.2 on Rocky Linux 9

Posted on

Best Way To Install PHP 8.2 on Rocky Linux 9

Best Way To Install PHP 8.2 on Rocky Linux 9

PHP, a widely used open-source scripting language, empowers developers to create dynamic and interactive web experiences. Its ability to be embedded directly into HTML made it a game-changer for adding server-side functionality. With continuous updates, including the latest stable release of PHP 8.2, PHP continues to evolve, offering enhanced features and capabilities. This article guides you through the process of installing PHP 8.2 on Rocky Linux 9, and offers alternative approaches to achieve the same goal.

This guide, inspired by the expertise of Orcacore, provides a comprehensive walkthrough of installing PHP 8.2 on Rocky Linux 9. Let’s dive into the step-by-step instructions.

Before we begin, ensure you’re logged into your Rocky Linux 9 server as a non-root user with sudo privileges. If you haven’t already configured this, refer to a guide on Initial Server Setup with Rocky Linux 9 for assistance.

Set up PHP 8.2 on Rocky Linux 9

The primary method for installing PHP 8.2 involves leveraging the Remi repository. Let’s break down the process:

  1. Update Package Index: Start by refreshing your local package index using the following command:
sudo dnf update -y
  1. Install EPEL Repository: The Extra Packages for Enterprise Linux (EPEL) repository provides additional packages not included in the base Rocky Linux distribution. Install it with:
sudo dnf install epel-release -y

Add Remi Repository

The Remi repository is crucial for accessing PHP 8.2. Install it using:

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

After installation, update the system again:

sudo dnf update -y

Note: If you have older PHP versions or PHP-FPM installed, remove them to avoid conflicts:

sudo dnf remove php php-fpm -y

And remove any lingering package extensions:

sudo dnf remove php* -y

Enable PHP 8.2 Remi Repository

Before installing PHP 8.2, reset the PHP module list:

sudo dnf module list reset php -y
Enable PHP 8.2 Remi Repository

As the image illustrates, PHP 8.1 is often the default. To switch to PHP 8.2, enable the Remi repository for PHP 8.2:

sudo dnf module enable php:remi-8.2

Now, you can install PHP 8.2:

sudo dnf install php -y

For commonly used PHP 8.2 extensions, 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

If you’re interested in development packages, install them with:

sudo dnf install php-devel -y

Verify the installation by checking the PHP version:

php -v
Verify PHP 8.2 installation on Rocky Linux 9

Configure PHP-FPM As an Nginx User

By default, PHP-FPM is configured to run under the Apache user. If you’re using Nginx, you need to adjust the configuration.

Open the www.conf file:

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

Locate the user and group directives and change them to nginx:

user = nginx
group = nginx

Save the file and restart PHP-FPM:

sudo systemctl restart php-fpm

For Nginx to correctly process PHP files, ensure your server block includes the following:

    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 detailed information, consult the PHP Documentation page.

Alternative Installation Methods for PHP 8.2 on Rocky Linux 9

While the Remi repository method is widely recommended, here are two alternative approaches for installing PHP 8.2 on Rocky Linux 9:

1. Using a Software Collection Library (SCL)

SCL allows you to install multiple versions of software on the same system without conflicts. This is beneficial if you need to maintain older PHP versions alongside PHP 8.2.

  • Install SCL Utils:
sudo dnf install scl-utils -y
  • Enable the PHP 8.2 SCL: First, you need to find the correct SCL package name. This may vary, but a common name is rh-php82. You might need to search for available SCL packages using dnf search php. Once you have the correct package name (e.g., rh-php82), install it:
sudo dnf install rh-php82 -y
  • Enable PHP 8.2 in your shell: To use PHP 8.2, you need to enable it in your current shell session:
scl enable rh-php82 bash

This command modifies your environment variables for the current shell session. Any PHP commands you run in this shell will now use the PHP 8.2 version.

  • Verify the Installation:
php -v

You’ll notice the output shows PHP 8.2 is active in this specific shell. To make this permanent for a user, you can add the scl enable command to their .bashrc or .bash_profile file.

Code Example: Creating a PHP Info File within the SCL Environment

#!/bin/bash
scl enable rh-php82 bash <<EOF
echo "<?php phpinfo(); ?>" > /tmp/phpinfo.php
php /tmp/phpinfo.php | less
rm /tmp/phpinfo.php
EOF

This script first enables the rh-php82 SCL environment. Then, it creates a phpinfo.php file, executes it using the PHP 8.2 interpreter, and displays the output using less. Finally, it removes the temporary file. This demonstrates how to execute PHP code using the SCL-provided PHP version.

2. Building PHP 8.2 from Source

This method provides the most control over the installation process, allowing you to customize compilation options and install PHP in a specific location. However, it’s more complex and requires more technical expertise.

  • Install Development Tools and Dependencies:
sudo dnf groupinstall "Development Tools" -y
sudo dnf install libxml2-devel bzip2-devel libcurl-devel libpng-devel libjpeg-devel freetype-devel gmp-devel openssl-devel libzip-devel oniguruma-devel -y
wget https://www.php.net/distributions/php-8.2.x.tar.gz  # Replace x with the specific minor version
tar -xzf php-8.2.x.tar.gz
cd php-8.2.x
  • Configure the Build: The ./configure script prepares the source code for compilation. You can customize the build by enabling or disabling various extensions and features. Here’s a basic example:
./configure --prefix=/usr/local/php82 --with-config-file-path=/usr/local/php82/etc --with-fpm --with-mysqli --with-pdo-mysql --with-curl --with-openssl --enable-mbstring --enable-zip

Replace /usr/local/php82 with your desired installation directory. The --with-* options enable specific extensions. Consult the PHP documentation for a complete list of configuration options.

  • Compile and Install:
make
sudo make install
  • Configure PHP-FPM (if enabled): You’ll need to manually create a php.ini file in the /usr/local/php82/etc directory. You can copy the default php.ini-development or php.ini-production file and customize it. You’ll also need to configure PHP-FPM to run as a system service. This typically involves creating a systemd service file.

  • Update PATH: Add /usr/local/php82/bin to your system’s PATH environment variable so you can run the php command from anywhere.

Code Example: Sample PHP-FPM Systemd Service File (/etc/systemd/system/php82-fpm.service)

[Unit]
Description=PHP 8.2 FastCGI Process Manager
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/php82/var/run/php-fpm.pid
ExecStart=/usr/local/php82/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php82/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

This service file assumes you’ve configured PHP-FPM with a configuration file at /usr/local/php82/etc/php-fpm.conf and that the php-fpm executable is located at /usr/local/php82/sbin/php-fpm. You’ll need to create the php-fpm.conf file and adapt the paths to match your installation. After creating the service file, you can enable and start the PHP-FPM service:

sudo systemctl enable php82-fpm.service
sudo systemctl start php82-fpm.service

These alternative methods offer different levels of control and complexity. SCL is a good option for managing multiple PHP versions, while building from source provides maximum customization. However, the Remi repository method is generally the easiest and most straightforward approach for installing PHP 8.2 on Rocky Linux 9.

Conclusion

This article detailed how to install PHP 8.2 on Rocky Linux 9. Using the Remi repository, you can efficiently enable your desired PHP version. In addition, we explored two alternative methods: using Software Collection Libraries (SCL) and building from source. Choosing the best method depends on your specific needs and technical expertise.

You may like these articles:

  • Install Docker on Rocky Linux 9
  • Reset Root Password on Rocky Linux 9
  • Install LEMP Stack on Rocky Linux 9

FAQs

How do I enable the Remi repository to install PHP 8.2 on Rocky Linux 9?

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

How do I install PHP 8.2 modules and extensions on Rocky Linux 9?

As described in the guide steps on Install PHP 8.2 on Rocky Linux 9, install the most commonly used extensions for PHP 8.2 with 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

How do I check the installed PHP version on Rocky Linux 9?

Simply use the following command:
php -v

Leave a Reply

Your email address will not be published. Required fields are marked *