How To Install PHP 8.4 on CentOS Stream 10 / CentOS Stream 9

Greetings and salutations. Our guide today will focus on the latest release of PHP i.e PHP 8.4. In the previous blog posts, we have done wonderful articles on PHP and I will give a list of a few at the end of this guide.

PHP is a general-purpose server-side scripting language used for making powerful, dynamic, and interactive websites. It was initially created by a Canadian developer by the name of Rasmus Lerdorf in 1993 but was released to the general public in 1995. PHP is now maintained by the PHP Group. Since then several versions have been released as described on the ChangeLog

PHP 8.4 was released on November 21, 2024, and comes with very amazing features. PHP 8.4 is a stable release with support up to 2026. Many WordPress developers will definitely fall in love with this release.

I will briefly look at the features that come with PHP 8.4. PHP 8.4 is designed for better performance, better syntax, and improved type safety.

Features PHP 8.4

Some of the features released with PHP 8.4 are as briefly described below. The examples used in this blog are from PHP 8.4.0 Release notes.

  • Property Hooks: Property hooks eliminate the need to write docblock comments that could become out of sync by supporting computed properties that are natively understood by IDEs and static analysis tools. Additionally, they make it possible to reliably pre- or post-process values without having to verify that the class contains a matching getter or setter.
  • Asymmetric Visibility: In order to expose a property’s value without permitting alteration from outside of a class, boilerplate getter methods are no longer necessary because the scope to write to and read a property can now be controlled individually.
# In earlier versions
class PhpVersion
{
    private string $version = '8.3';

    public function getVersion(): string
    {
        return $this->version;
    }

    public function increment(): void
    {
        [$major, $minor] = explode('.', $this->version);
        $minor++;
        $this->version = "{$major}.{$minor}";
    }
}
# In PHP 8.4 versions
class PhpVersion
{
    public private(set) string $version = '8.4';

    public function increment(): void
    {
        [$major, $minor] = explode('.', $this->version);
        $minor++;
        $this->version = "{$major}.{$minor}";
    }
}
  • #[\Deprecated] Attribute : The new #[\Deprecated] attribute makes PHP’s existing deprecation mechanism available to user-defined functions, methods, and class constants.
#In earlier PHP versions
class PhpVersion
{
    /**
     * @deprecated 8.3 use PhpVersion::getVersion() instead
     */
    public function getPhpVersion(): string
    {
        return $this->getVersion();
    }

    public function getVersion(): string
    {
        return '8.3';
    }
}

$phpVersion = new PhpVersion();
// No indication that the method is deprecated.
echo $phpVersion->getPhpVersion();
#In PHP 8.4 versions
class PhpVersion
{
    #[\Deprecated(
        message: "use PhpVersion::getVersion() instead",
        since: "8.4",
    )]
    public function getPhpVersion(): string
    {
        return $this->getVersion();
    }

    public function getVersion(): string
    {
        return '8.4';
    }
}

$phpVersion = new PhpVersion();
// Deprecated: Method PhpVersion::getPhpVersion() is deprecated since 8.4, use PhpVersion::getVersion() instead
echo $phpVersion->getPhpVersion();
  • New ext-dom features and HTML5 support: The new DOM API provides a number of features to make dealing with documents easier, fixes a number of long-standing compliance flaws in the behavior of the DOM capability, and supports processing HTML5 documents in a standards-compliant manner.
# In earlier PHP versions
$dom = new DOMDocument();
$dom->loadHTML(
    <<<'HTML'
        <main>
            <article>PHP 8.4 is a feature-rich release!</article>
            <article class="featured">PHP 8.4 adds new DOM classes that are spec-compliant, keeping the old ones for compatibility.</article>
        </main>
        HTML,
    LIBXML_NOERROR,
);

$xpath = new DOMXPath($dom);
$node = $xpath->query(".//main/article[not(following-sibling::*)]")[0];
$classes = explode(" ", $node->className); // Simplified
var_dump(in_array("featured", $classes)); // bool(true)

# In PHP 8.4
$dom = Dom\HTMLDocument::createFromString(
    <<<'HTML'
        <main>
            <article>PHP 8.4 is a feature-rich release!</article>
            <article class="featured">PHP 8.4 adds new DOM classes that are spec-compliant, keeping the old ones for compatibility.</article>
        </main>
        HTML,
    LIBXML_NOERROR,
);

$node = $dom->querySelector('main > article:last-child');
var_dump($node->classList->contains("featured")); // bool(true)
  • Object API for BCMath: When working with arbitrary precision numbers, the new BcMath\Number object allows for standard mathematical operators and object-oriented usage. These objects can be used in string contexts like echo $num since they implement the Stringable interface and are immutable.
#In earlier PHP versions
$num1 = '0.12345';
$num2 = 2;
$result = bcadd($num1, $num2, 5);

echo $result; // '2.12345'
var_dump(bccomp($num1, $num2) > 0); // false

#In PHP Version 8.4
use BcMath\Number;

$num1 = new Number('0.12345');
$num2 = new Number('2');
$result = $num1 + $num2;

echo $result; // '2.12345'
var_dump($num1 > $num2); // false
#In earlier PHP versions
$animal = null;
foreach (['dog', 'cat', 'cow', 'duck', 'goose'] as $value) {
    if (str_starts_with($value, 'c')) {
        $animal = $value;
        break;
    }
}

var_dump($animal); // string(3) "cat"

#In PHP 8.4
$animal = array_find(
    ['dog', 'cat', 'cow', 'duck', 'goose'],
    static fn (string $value): bool => str_starts_with($value, 'c'),
);

var_dump($animal); // string(3) "cat"
  • new MyClass()->method() without parentheses: Properties and methods of a newly instantiated object can now be accessed without wrapping the new expression in parentheses.
#In earlier PHP versions
class PhpVersion
{
    public function getVersion(): string
    {
        return 'PHP 8.3';
    }
}

var_dump((new PhpVersion())->getVersion());

#In PHP 8.4
class PhpVersion
{
    public function getVersion(): string
    {
        return 'PHP 8.4';
    }
}

var_dump(new PhpVersion()->getVersion());

How To Install PHP 8.4 on CentOS Stream 10 / CentOS Stream 9

We will now briefly look at PHP 8.4 installation on CentOS Stream 10 / CentOS Stream 9.

Step 1: Update CentOS Stream system.

Confirm your system version.

$ cat /etc/os-release 
NAME="CentOS Stream"
VERSION="9"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="9"
PLATFORM_ID="platform:el9"
PRETTY_NAME="CentOS Stream 9"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:9"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 9"
REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream"

Update your system packages to refresh your package cache.

sudo dnf update -y && sudo reboot 

Step 2: Add EPEL & REMI repository

By default, PHP 8.4 repositories are not available on CentOS Stream 9. For this reason, we will install EPEL & REMI repository third-party repositories that ship PHP 8.4.

Enable the EPEL repository by running this command.

dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

Enable REMI for PHP 8.4:

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

Once you have enabled EPEL and REMI repositories, install the yum-utils to manage yum repositories.

sudo dnf -y install yum-utils

Enable php 8.4 Remi repository.

sudo dnf module reset php
sudo dnf module enable php:remi-8.4
sudo dnf install php-cli php-fpm php-mbstring php-xml

Step 3: Install PHP 8.4 on CentOS Stream 10 / CentOS Stream 9

In the previous step, we enabled the EPEL and REMI repositories and we can thus install PHP 8.4 with the dnf package manager. Execute the command below.

$ sudo dnf module install php:remi-8.4
....
Transaction Summary
================================================================================
Install  8 Packages

Total download size: 9.6 M
Installed size: 44 M
Is this ok [y/N]: y

From the output, all the required dependencies for PHP 8.4 are going to be installed. During the installation process, you will be prompted to accept the REMI GPG key. Confirm by typing Y and pressing enter.

You will be expected to update the system packages so that the new changes can be effected in the system.

sudo dnf update -y

Confirm the version of PHP installed in your system.

$ php -v
PHP 8.4.2 (cli) (built: Dec 17 2024 15:31:31) (NTS gcc x86_64)
Copyright (c) The PHP Group
Built by Remi's RPM repository  #StandWithUkraine
Zend Engine v4.4.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.2, Copyright (c), by Zend Technologies

Step 4: Install PHP 8.4 extensions

We will now install the PHP 8.4 extensions/modules to help PHP perform excellently. Some of these extensions include httpd-tools, libsodium, mod_http2, php-opcache, php-curl, php-mysqlnd, php-zip, and so on. To install the extensions, execute the command below.

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

Step 5: Using PHP with Nginx / Apache web server

With PHP 8.4 successfully installed, I will briefly describe how to use PHP with Nginx / Apache web servers. These are the two most commonly used web servers. These are used by the majority of developers to create dynamic & interactive websites as well as load balance the traffic in a website.

PHP 8.2 Usage with Nginx web server

PHP uses PHP-FPM (FastCGI Process Manager) to listen to incoming PHP requests and executes them within Nginx. When NGINX forwards incoming requests to PHP-FPM to be executed, it acts as a reverse proxy server.

To install Nginx and FPM PHP extensions on your system, run the command:

$ sudo dnf install vim nginx php-fpm -y
# Installed:
  nginx-1:1.14.1-9.module_el8.0.0+1060+3ab382d3.x86_64                          
  nginx-all-modules-1:1.14.1-9.module_el8.0.0+1060+3ab382d3.noarch              
  nginx-mod-http-image-filter-1:1.14.1-9.module_el8.0.0+1060+3ab382d3.x86_64    
  nginx-mod-http-perl-1:1.14.1-9.module_el8.0.0+1060+3ab382d3.x86_64            
  nginx-mod-http-xslt-filter-1:1.14.1-9.module_el8.0.0+1060+3ab382d3.x86_64     
  nginx-mod-mail-1:1.14.1-9.module_el8.0.0+1060+3ab382d3.x86_64                 
  nginx-mod-stream-1:1.14.1-9.module_el8.0.0+1060+3ab382d3.x86_64               

Start and enable nginx and php-fpm services by executing the command below.

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

Check Nginx status:

$ systemctl status nginx php-fpm
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor prese>
  Drop-In: /usr/lib/systemd/system/nginx.service.d
           └─php-fpm.conf
  Active: active (running) since Mon 2024-12-30 18:18:08 CET; 47s ago
  Process: 80728 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 80725 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 80723 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, statu>
 Main PID: 80729 (nginx)
    Tasks: 9 (limit: 23496)
   Memory: 15.4M
   CGroup: /system.slice/nginx.service
           ├─80729 nginx: master process /usr/sbin/nginx
           ├─80730 nginx: worker process
           ├─80731 nginx: worker process
           ├─80732 nginx: worker process
           ├─80733 nginx: worker process
           ├─80734 nginx: worker process
           ├─80735 nginx: worker process
           ├─80736 nginx: worker process
           └─80737 nginx: worker process

Dec 30 18:18:07 centos-9-stream-lab systemd[1]: Starting The nginx HTTP and rev>

● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor pre>
   Active: active (running) since Mon 2024-12-30 18:18:08 CET; 47s ago
 Main PID: 80722 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/s>
    Tasks: 6 (limit: 23496)
   Memory: 15.8M
   CGroup: /system.slice/php-fpm.service
           ├─80722 php-fpm: master process (/etc/php-fpm.conf)
           ├─80738 php-fpm: pool www
           ├─80739 php-fpm: pool www
           ├─80740 php-fpm: pool www
           ├─80741 php-fpm: pool www
           └─80742 php-fpm: pool www

Dec 30 18:18:07 centos-9-stream-lab systemd[1]: Starting The PHP FastCGI Proces>
Dec 30 18:18:08 centos-9-stream-lab systemd[1]: Started The PHP FastCGI Process>

Next, set the PHP-FPM to listen on a socket instead of an IP address or port number. This is achieved by editing the following file with the text editor of your choice and setting the following parameters.

Make a backup of the configuration file before any changes to the file.

sudo cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.bak

Then edit the file as follows.

$ sudo vim /etc/php-fpm.d/www.conf
#Make the following modifications.
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

After making the changes, restart the PHP-fpm service.

sudo systemctl restart php-fpm

Now edit the nginx configuration files with the parameters below on the http section. This is to block Nginx from forwarding PHP requests to PHP-FPM.

$ sudo vim /etc/nginx/nginx.conf
 server {
        listen       80;
        server_name  mywebsiteapp.com;
        root         /var/www/mywebsiteapp.com;
        index index.php index.html;
        location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm.sock;

        }

}

Validate Nginx configurations to see if the configurations are okay. Run the command below.

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

PHP 8.4 Usage with Apache web server

For the Apache web server, we use the mod_php module to process the PHP code directly within the Apache process. To install the Apache httpd, run the command below.

sudo dnf install httpd

You must stop the Nginx service if it is running because both cannot work perfectly when their services are running.

$ sudo systemctl disable --now nginx
Removed /etc/systemd/system/multi-user.target.wants/nginx.service.

Start and enable the httpd service by the following command.

$ sudo systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

The default configuration file for Apache is /etc/httpd/conf and /etc/httpd/conf.d . THese two files contain all the configuration files for Apache. The website data resides in /var/www/html but can be changed if you wished to.

The primary Apache configuration file is located in /etc/httpd/conf/httpd.conf. This is where all the necessary configurations are made.

Edit the configuration file with the following parameters:

$ sudo vim /etc/httpd/conf/httpd.conf
#make the following changes
DocumentRoot /var/www/html
Listen 80

Create a file in /var/www/html called info.php for test purposes.

sudo vim /var/www/html/info.php

Add the following code.

<?php
phpinfo();
?>

Save and exit from the editor.

Open Port 80 through the firewall to allow communication with the outside world by running the following commands.

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --reload

Manage the Apache service by running the following commands:

sudo systemctl start httpd
sudo systemctl restart php-fpm
sudo systemctl restart httpd

Check the status of the httpd service:

$ systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor pr>
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           └─php-fpm.conf
   Active: active (running) since Tue 2024-12-30 20:00:10 CET; 7s ago
     Docs: man:httpd.service(8)
 Main PID: 127521 (httpd)
   Status: "Started, listening on: port 80"
    Tasks: 213 (limit: 23496)
   Memory: 27.3M
   CGroup: /system.slice/httpd.service
           ├─127521 /usr/sbin/httpd -DFOREGROUND
           ├─127522 /usr/sbin/httpd -DFOREGROUND
           ├─127523 /usr/sbin/httpd -DFOREGROUND
           ├─127524 /usr/sbin/httpd -DFOREGROUND
           └─127525 /usr/sbin/httpd -DFOREGROUND

Dec 30 20:00:10 centos-9-stream-lab systemd[1]: Starting The Apache HTTP Ser>
Dec 30 20:00:10 centos-9-stream-lab systemd[1]: Started The Apache HTTP Serv>
Dec 30 20:00:10 centos-9-stream-lab httpd[127521]: Server configured, listen>

To test your server, navigate to your web browser and enter the address <http://localhost/info.php or http://<server_ip_address>/info.php>. If your server is properly set, this page should be loaded.

Alternatively, you can run the following code on your terminal.

$ curl -O http://IP/<your_file.php>

#As shown below
$ curl -0 http://192.168.1.180/info.php

You may wish to analyze the logs to know what is happening to your site.

$ sudo cat /var/log/httpd/access_log | grep -I info.php

Wrap up

That marks the end of our guide. In the guide, we analyzed the new features in PHP 8.4 and installed PHP 8.4 on CentOS Stream 10 / CentOS Stream 9. We also learned how to configure Apache with Nginx and Apache web servers and tested our server on the web browser. I hope your installation was successful. For production purposes, ensure you configure the security settings to secure your server from attacks. Let us know what you think.

See more guides:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

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

Related Post

Every now and again, a new technology enters the world of business intelligence that further innovates the field. While these […]

Greetings and salutations. In our guide today we will explore yet another Shell called Fish shell short form for the friendly interactive shell. […]

With so many Linux distributions available, it is hard to deploy applications to them. This is due to the fact […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.