Home  »  ArticlesGuidesHow ToLibrariesTechnology   »   How To Install PHP 8.2 on CentOS 7 | RHEL 7

How To Install PHP 8.2 on CentOS 7 | RHEL 7

In this PHP 8.2 tutorial we will now show you how to install PHP 8.2 on CentOS 7 and RHEL 7 based Linux systems. If you are looking into installing PHP 8.2 on Ubuntu systems, we have a post for that as well as this post showing how to install on Debian 11 and Debian 10 Linux systems.

PHP 8.2 is a version of the PHP programming language released in December 2022 with many new features. Here are some highlights:

  • Readonly Classes: A class can be declared as readonly, making all of its properties to be automatically declared readonly
  • Type System Improvements: Added support for true type, and allowing null and false types to be used as stand-alone types as well as support for DNF types.
  • New random extension: This new feature provides a new OOP API to generate random numbers with a pluggable architecture.
  • Constants in Traits: It is now possible to declare constants in traits in In PHP 8.2.
  • Sensitive Parameter Value Redaction: This feature adds new built-in parameter Attribute named #[\SensitiveParameter] that ensures PHP redacts the actual value in stack traces and error messages.
  • New Functions and Classes: New classes and functions such as ini_parse_quantity, curl_upkeep, openssl_cipher_key_length, and memory_reset_peak_usage are now included.
  • Dynamic Properties Deprecated : In PHP 8.2 class properties that are dynamically declared
  • utf8_encode and utf8_decode Functions are now deprecated.

Prerequisites to Install PHP 8.2 on CentOS 7 | RHEL 7

CentOS 7 / RHEL 7 Linux systems rely on third-party repositories such as Remi or IUS for the installation of PHP 8.2. These repositories provide updated versions of PHP for these systems which are not available in default OS repos.

Step 1: Update CentOS 7 | RHEL 7

Update the package list by running the following commands:

$ sudo yum update -y

Step 2: Add EPEL & REMI Repository

EPEL and REMI are third-party repositories that provide additional software packages for Enterprise Linux distributions. REMI provides the latest versions of PHP.

Run this to add EPEL repository on your system:

$ sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Run this to add Remi repository on your system:

$ sudo yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Enable REMI for PHP 8.2

$ sudo yum-config-manager --enable remi-php82

Step 3: Install PHP 8.2 on CentOS 7 / RHEL 7

You can now install PHP 8.2 on CentOS 7 | RHEL 7 using yum (Yellowdog Updater Modified) package manager.

$ sudo yum -y install php

Confirm version of PHP currently default in the system.

$ php -v

Output

PHP 8.2.1 (cli) (built: Jan  3 2023 18:40:55) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.1, Copyright (c) Zend Technologies

Step 4: Install PHP 8.2 extensions

For example, assuming we want to install the following PHP extensions:

bz2, cli, common, curl, intl, mbstring, mysql, zip

The we would run the following command to install the above extensions:

$ sudo yum install php8.2-{bz2,cli,common,curl,intl,mbstring,mysql,zip}

How to use PHP 8.2 with Nginx on CentOS 7 | RHEL 7

In this section we will use Nginx as a reverse proxy to forward incoming PHP requests to PHP-FPM for execution. Therefore we need to install Nginx and FPM PHP extension on your system.

$ sudo yum install nginx php-fpm -y

Next we start and enable nginx and php-fpm services.

$ sudo systemctl enable --now nginx php-fpm

You can confirm the status of services started with this command.

$ systemctl status nginx php-fpm

Set PHP-FPM to listen on a socket instead of IP and port. Open the Nginx config file and make the following modifications.

$ sudo vim /etc/php-fpm.d/www.conf

File

user = nginx
group = nginx
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

Restart php-fpm service to apply changes.

$ sudo systemctl restart php-fpm

Edit Nginx config file and add the following block inside the http block to configure Nginx to forward PHP requests to PHP-FPM. This is the VirtualHost configuration sample:

$ sudo vim /etc/nginx/nginx.conf

File

server {
    listen 80;
    server_name myapp.com;
    root /var/www/myapp;
    index index.php index.html;

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm.sock;
    }
}

Validate your nginx configurations using this command.

$ sudo nginx -t

Using PHP 8.2 with Apache on CentOS 7 | RHEL 7

Install Apache httpd package.

$ sudo yum install httpd

Stop nginx service if running:

$ sudo systemctl disable --now nginx

Start and enable httpd service.

$ sudo systemctl enable --now httpd

Conclusion

In this guide, you should have been able to successfully install PHP 8.2 on your CentOS 7 or RHEL 7 Linux system using REMI third party repository. In addition we have shown you in brief how to use PHP 8.2 with Nginx and Apache. For more details on how to configure PHP 8.2 for web servers please visit this in-depth guide on how to run your PHP with Nginx or Apache web servers.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.