Install PHP 8.2 on Rocky Linux 8 with Easy Steps – OrcaCore

Posted on

Install PHP 8.2 on Rocky Linux 8 with Easy Steps - OrcaCore

Install PHP 8.2 on Rocky Linux 8 with Easy Steps – OrcaCore

In this tutorial, we want to show you How To Install PHP 8.2 on Rocky Linux 8. PHP is the most widely used open-source and general-purpose server-side scripting language, and it is used mainly in web development to create dynamic websites and applications. Ensuring you have the latest, secure, and performant version is crucial for any web project. This article details how to Install PHP 8.2 on Rocky Linux 8, offering a step-by-step guide.

You can follow the steps below on the Orcacore website to install the current and latest PHP release, PHP 8.2.

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 8.

Install PHP 8.2 on Rocky Linux 8

In this guide, we will use the Remi RPM repositories which contain the new packages of PHP for RHEL-based systems such as CentOS, Rocky Linux, AlmaLinux, Fedora, etc. This is a common and reliable method for installing newer PHP versions on these distributions. Let’s get started to Install PHP 8.2 on Rocky Linux 8.

First, update your local package index with the following command:

sudo dnf update -y

Install Epel Repository

Then, you need to install the Epel repository on your server:

sudo dnf -y install epel-release

Enable PowerTools

Next, enable the PowerTools with the command below:

sudo dnf config-manager --set-enabled powertools

Install Remi Repository

At this point, you can install the Remi repository on Rocky Linux 8 with the following command:

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

Now you can verify all the repositories added are active with the commands below:

# sudo dnf -y makecache
# sudo dnf -y repolist
Install Remi Repository Rocky Linux 8

List Available PHP Versions

At this point, you can check the PHP modules on Rocky Linux 8:

sudo dnf module list php
List Available PHP Versions Rocky Linux 8

As you can see the latest version available for PHP is PHP 8.2. To install it on your Rocky Linux 8, follow the steps below.

First, reset the PHP default module in the AppStream repository:

sudo dnf -y module reset php

Enable Remi PHP 8.2

Then, enable the Remi repository for PHP 8.2 on Rocky Linux 8 with the command below:

sudo dnf module install php:remi-8.2

When you are finished, you can verify your PHP installation by checking its version:

php --version
Enable Remi PHP 8.2

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

Configure PHP-FPM Service

By default on Rocky Linux 8, 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 8. You just need to need to enable, epel repo, PowerTools, and Remi PHP 8.2. Then, you can easily install it on your Rocky Linux 8.

Hope you enjoy it. Please subscribe to us on Facebook and Twitter.

Also, you may like to read the following articles:

Install PowerDNS and PowerDNS-Admin on RockyLinux 8

Installing 7Zip on Rocky Linux 8

Installing Grafana Monitoring on Rocky Linux 9

Install PHP ionCube Loader on Rocky Linux 8

Enable Flatpak on Rocky Linux 8

FAQs

What prerequisites are needed before installing PHP 8.2 on Rocky Linux 8?

You’ll need to enable the EPEL and Remi repositories, which provide the necessary PHP 8.2 packages.

How to install PHP 8.2 and its common extensions on Rocky Linux 8?

After enabling the required repositories, you can run the command below:
sudo dnf install php php-cli php-gd php-curl php-zip php-mbstring

How to check the PHP version on Rocky Linux 8?

You can use the following command: php --version

Alternative Solutions for Installing PHP 8.2 on Rocky Linux 8

While the Remi repository method outlined above is a common and effective approach, alternative solutions exist for installing PHP 8.2 on Rocky Linux 8. These methods might be preferable in specific scenarios, such as when you require more control over the installation process or prefer containerized solutions. Here are two alternative approaches:

1. Using Software Collections (SCL)

Software Collections (SCL) allow you to install multiple versions of the same software on a single system without conflicts. This is beneficial if you need to maintain older PHP versions alongside the newer PHP 8.2.

Explanation:

SCL provides a mechanism to install and manage different versions of software in isolated environments. This prevents conflicts between different versions and allows you to choose which version to use for specific applications. The scl enable command activates the desired software collection for the current shell session.

Steps:

  1. Install the centos-release-scl package:

    sudo dnf install centos-release-scl
  2. Install PHP 8.2 from SCL (if available). Check the available collections first:

    sudo dnf search rh-php

    If a PHP 8.2 collection is available (e.g., rh-php82), install it:

    sudo dnf install rh-php82
  3. Enable the PHP 8.2 collection:

    scl enable rh-php82 bash

    This command starts a new shell session with PHP 8.2 enabled. Any PHP commands executed in this session will use the PHP 8.2 version.

  4. Verify the installation:

    php --version

Code Example:

Activating SCL for a specific command:

scl enable rh-php82 'php --version'

This command executes php --version with the PHP 8.2 environment enabled and then returns to the default environment.

2. Using Docker Containers

Docker provides a containerization platform that allows you to package applications and their dependencies into isolated containers. This ensures consistency across different environments and simplifies deployment.

Explanation:

Docker containers encapsulate the entire runtime environment for an application, including the operating system, libraries, and dependencies. This eliminates compatibility issues and ensures that the application runs consistently regardless of the underlying infrastructure. Docker images are built from Dockerfiles, which specify the instructions for creating the container.

Steps:

  1. Install Docker:

    Follow the official Docker documentation to install Docker on your Rocky Linux 8 system.

  2. Create a Dockerfile:

    Create a file named Dockerfile with the following content:

    FROM php:8.2-fpm-alpine
    
    # Install any necessary extensions
    RUN docker-php-ext-install mysqli pdo pdo_mysql
    
    # Set working directory
    WORKDIR /var/www/html
    
    # Copy your application code
    COPY . /var/www/html
    
    # Expose port 9000 for PHP-FPM
    EXPOSE 9000
    
    # Start PHP-FPM
    CMD ["php-fpm"]
  3. Build the Docker image:

    Navigate to the directory containing the Dockerfile and run the following command:

    docker build -t my-php-app .
  4. Run the Docker container:

    docker run -d -p 8080:9000 my-php-app

    This command starts the Docker container in detached mode (-d) and maps port 8080 on the host to port 9000 inside the container. You’ll need to configure your web server (e.g., Nginx) to proxy requests to port 8080.

Code Example:

Example Nginx configuration to proxy requests to the Docker container:

server {
    listen 80;
    server_name example.com;

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
        fastcgi_pass 127.0.0.1:8080; # Proxy to the Docker container
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

These alternative methods provide flexibility in how you Install PHP 8.2 on Rocky Linux 8, catering to different needs and preferences. Choose the method that best suits your specific requirements and technical expertise.

Leave a Reply

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