Home » Blog » How TO » How To Install PHP 8.2 on CentOS 7 / RHEL 7

How To Install PHP 8.2 on CentOS 7 / RHEL 7

How To Install PHP 8.2 on CentOS 7 / RHEL 7

How To Install PHP 8.2 on CentOS 7 / RHEL 7. PHP is an open-source server-side scripting language widely supported by web hosting providers, making it easy to deploy any kind of PHP-based web application. Combined with its ease of use, PHP is a popular choice for small to medium-sized websites and web applications. The language is relatively easy to learn, making it a popular choice for beginners and experienced developers alike. PHP has libraries such as MySQLi and PDO (PHP Data Objects), that enables it to interact with various types of databases, including MariaDB, MySQL, PostgreSQL, and more.

In addition to its use in websites and web applications development, PHP can also be used to create command-line scripts, mobile, and even desktop applications. It also works well with other web technologies such as CSS, HTML, and JavaScript, making it easy to create modern, responsive, and interactive web pages.

In today’s article we discuss the installation and usage of PHP 8.2 on CentOS 7 / RHEL 7. In our recent guides we covered installation of PHP 8.2 on Debian based Linux systems available in the links below.

What’s new in PHP 8.2?

  • Type System Improvements – Added support for true type, and allowing null and false types to be used as stand-alone types, and support for DNF types.
  • Readonly Classes –  When a class is declared as readonly, all of its properties are automatically declared readonly
  • Constants in Traits – In PHP 8.2, it is now possible to declare constants in traits
  • New random extension – Provides a new OOP API to generate random numbers with a pluggable architecture.
  • Sensitive Parameter Value Redaction – Addition of new built-in parameter Attribute named #[\SensitiveParameter] that PHP makes sure to redact the actual value in stack traces and error messages.
  • New Functions and Classes – ini_parse_quantity functioncurl_upkeep function , openssl_cipher_key_length,  memory_reset_peak_usage
  • utf8_encode and utf8_decode Functions Deprecated
  • Dynamic Properties Deprecated – PHP 8.2 deprecates class properties that are dynamically declared

Install PHP 8.2 on CentOS 7 / RHEL 7

Installation of PHP 8.2 can be done on CentOS 7 / RHEL 7 Linux system from third-party repositories such as Remi or IUS. These repositories provide updated versions of PHP for these systems, which are not available in default OS repos. However, it’s important to note that for existing applications running on an older version of PHP consider doing testing of newer PHP on a non-production environment first to ascertain your application is compatible with it.

Step 1: Update CentOS 7 / RHEL 7 system

Update the package list on your CentOS / RHEL system by executing the following commands in the terminal.

sudo yum update -y

Then reboot to ensure that all updates are properly applied:

sudo reboot

Step 2: Add EPEL & REMI repository

EPEL and REMI are two popular third-party repositories that provides additional software packages for Enterprise Linux distributions. REMI provides the latest versions of PHP such as PHP 8.2.

Enable EPEL repository on your CentOS 7 / RHEL 7 system:

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

Also add Remi repository which provides the latest version of PHP for CentOS and RHEL.

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

Once the repositories are added enable REMI for PHP 8.2

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

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

Since the required packages are available in the repository we just enabled, installation of PHP 8.2 on CentOS 7 / RHEL 7 can be done using PHP using yum (Yellowdog Updater Modified) package manager.

sudo yum -y install php

Confirm version of PHP currently default in the system.

$ php -v
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 on CentOS 7 / RHEL 7

Depending on the requirements of the application to be run on the system, there could be a need to install additional PHP modules, such as php-curl, php-mysqlnd, php-zip, etc.

The commands shared will install a number of PHP extensions often required by PHP applications.

sudo yum install php-{common,pear,cgi,curl,mbstring,gd,mysqlnd,gettext,bcmath,json,xml,fpm,intl,zip,imap}

Agree to continue with the installation.

....
Transaction Summary
======================================================================================================================================================================================================
Install  10 Packages (+26 Dependent packages)

Total download size: 20 M
Installed size: 68 M
Is this ok [y/d/N]: y

Step 5: Using PHP with Nginx / Apache web server

You can use PHP with either Nginx or Apache web server to create your dynamic and interactive web pages.

Using PHP 8.2 with Nginx on CentOS 7 / RHEL 7

PHP-FPM (FastCGI Process Manager) is a daemon that listens for incoming PHP requests and executes them. In this instance Nginx acts as a reverse proxy, forwarding incoming requests to PHP-FPM to be executed.

Install Nginx and FPM PHP extension on your system.

sudo yum install vim nginx php-fpm -y

Start and enable nginx and php-fpm services.

$ sudo systemctl enable --now nginx php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

Confirm the status of services started.

$ systemctl status nginx php-fpm
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2023-01-20 12:20:26 UTC; 19s ago
  Process: 8645 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 8642 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 8640 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 8647 (nginx)
   CGroup: /system.slice/nginx.service
           ├─8647 nginx: master process /usr/sbin/nginx
           ├─8648 nginx: worker process
           └─8649 nginx: worker process

Jan 20 12:20:26 cent7.mylab.io systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 20 12:20:26 cent7.mylab.io nginx[8642]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jan 20 12:20:26 cent7.mylab.io nginx[8642]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jan 20 12:20:26 cent7.mylab.io systemd[1]: Started The nginx HTTP and reverse proxy server.

● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2023-01-20 12:20:26 UTC; 19s ago
 Main PID: 8641 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/php-fpm.service
           ├─8641 php-fpm: master process (/etc/php-fpm.conf)
           ├─8650 php-fpm: pool www
           ├─8651 php-fpm: pool www
           ├─8652 php-fpm: pool www
           ├─8653 php-fpm: pool www
           └─8654 php-fpm: pool www

Jan 20 12:20:26 cent7.mylab.io systemd[1]: Starting The PHP FastCGI Process Manager...
Jan 20 12:20:26 cent7.mylab.io systemd[1]: Started The PHP FastCGI Process Manager.

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

$ sudo vim /etc/php-fpm.d/www.conf
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 after the changes are made.

sudo systemctl restart php-fpm

Edit Nginx configuration file and add the following block inside the http block to configure Nginx to forward PHP requests to PHP-FPM.

VirtualHost configuration sample:

$ sudo vim /etc/nginx/nginx.conf
server {
    listen 80;
    server_name myapp.example.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.

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Using PHP 8.2 with Apache on CentOS 7 / RHEL 7

When Apache web server PHP code is typically executed using the mod_php module, which is built into Apache. The mod_php is an Apache module responsible for processing PHP code directly within the Apache process.

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

Add PHP hello world page to confirm it works.

sudo tee /var/www/html/info.php<<EOF
<?php phpinfo(); ?>
EOF

Access PHP info page on your web browser http://localhost_or_serverip/info.php, you should see the PHP information page.

Conclusion

By following the steps provided in this guide, you should have successfully installed PHP 8.2 on your CentOS 7 or RHEL 7 Linux system using REMI third party repository. You can now proceed with the installation and configuration of any additional software or modules that your application will require to function and serve your clients. Don’t forget check if your application will support PHP 8.2, this is critical when upgrading from older versions of PHP.

Scroll to Top