How to Install PHP 8.0 on CentOS 9 Stream

PHP 8.0 is major released from PHP 7 series with improvements and changes to the language, including many long-awaited additions by developers everywhere! This includes named arguments; union types. It also boasts improved JIT compilation tools which will help optimize your application code even further than before while increasing performance.

In the following tutorial, you will learn how to install PHP 8.0 on Centos 9 Stream by importing the EPEL 9 and REMI repositories. The tutorial will focus on installing the development kit extensions and some tips for Nginx and PHP-FPM using the command line terminal.

Situational – Remove DNF Subscription Manager

For users who encounter issues with “This system is not registered with an entitlement server. You can use subscription-manager to register,” then use the following command to remove the subscription manager as it is unnecessary for CentOS stream to receive updates, unlike RHEL.

sudo dnf erase subscription-manager -y

Example output:

How to Install PHP 8.0 on CentOS 9 Stream

As mentioned above, all the subscription-based lockouts have been removed.

Update CentOS Stream

Before you continue, your system is advised to make sure all existing packages are up to date to avoid system conflicts during the installation and for good system maintenance.

sudo dnf upgrade --refresh -y

Install PHP 8.0 Remi Repository

Currently, PHP 8.0 is not featured in CentOS Stream’s AppStream, given it’s still relatively new and hasn’t filtered down the chain in RHEL’s repositories yet. However, you can install PHP from (Remi) repository, a free-to-use third-party repository that deploys the latest PHP 8.0 builds.

The first task is to install the (EPEL) repository, which stands for (Extra Packages for Enterprise Linux). For newer users to CentOS Stream and similar distributions based on RHEL, EPEL contains the most commonly used software packages for Enterprise Linux.

First, enable the CRB.

sudo dnf config-manager --set-enabled crb

Next, install EPEL use the following (dnf) terminal command:

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

Now that you have added the EPEL repository, enable (Remi repository) with the following:

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

Next, refresh your repository cache to import the GPG keys automatically.

sudo dnf update --refresh -y

Situational – Remove Existing PHP Installation

Remove the previous version for users wanting to install PHP 8.0 but have already installed PHP or PHP-FPM. For example, remove PHP 7.4 for PHP 8.0.

Make sure to back up any configuration files.

sudo dnf remove php php-fpm -y

Then remove the rest of the package extensions.

sudo dnf remove php* -y

To reset the PHP module list is easy with the following command:

sudo dnf module list reset php -y

Now you can proceed to the next part of the tutorial.

Enable PHP 8.0 (Remi) Repository

PHP 7.2 is the default PHP choice for standard installation on Rhel/CentOS Stream with Remi’s repository. A quick tip is to use the (list php) command to see the options available and the default.

The following dnf module list command can do this:

sudo dnf module php

You will get the following output as below. Note the (d) tag for default PHP to be installed:

Example:

How to Install PHP 8.0 on CentOS 9 Stream

Next, enable PHP 8.0 with the following command:

sudo dnf module enable php:remi-8.0 -y

Install PHP 8.0

Now that you have added the Remi PHP repository and enabled PHP 8.0 to be the default version on your system, you can install PHP 8.0 with the following command:

Apache (HTTPD) Users:

sudo dnf install php -y

Nginx Users:

sudo dnf install php-fpm -y

Note, you will be prompted for more GPG key importsType Y to any that show in your terminal.

If you would like to install the most commonly used extensions for PHP 8.0, use the following command:

sudo dnf install php-cli 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

Note, remove the options you do not want this is optional. It is highly recommended to only install and keep what modules you require from a performance and security standard.

Lastly, use the following command for anyone interested in installing the development branch.

sudo dnf install php-devel -y

This will install lots of dependencies, and unless you are developing with PHP or have some special requirement to install it, do not use this version.

Now that you have installed PHP 8.0 and the extensions, check the version with the following command:

php -v

Example output:

How to Install PHP 8.0 on CentOS 9 Stream

PHP-FPM Installations of PHP 8.0

Unlike PHP-FPM installations on Debian/Ubuntu that use the (www-data) user, this isn’t the case with RHEL family installations. By default on CentOS Stream, the PHP-FPM service is designed to be run (Apache) user, which is incorrect since we are using Nginx, and this needed to be corrected.

Firstly, open following (www.conf) configuration file:

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

Next, replace the (Apache) user and group with the (Nginx) user and group:

Example:

How to Install PHP 8.0 on CentOS 9 Stream

To save, press (CTRL+O) then exit (CTRL+X).

Now you will too reload or restart your PHP-FPM service:

sudo systemctl restart php-fpm

Nginx Configurement

The Nginx server block needs the following example below for Nginx to process the PHP files.

Below is an example of 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;
    }

Test Nginx to make sure you have no errors with the adjustments made with the code above; enter the following.

sudo nginx -t

Example output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx service for installation to be complete.

sudo systemctl restart nginx

Comments and Conclusion

In the tutorial, you have learned how to install PHP 8.0 on CentOS 9 Stream for developers or, for the most part, servers that require it for their web application or CMS, such as WordPress.

All in all, PHP 8.0 is slowly becoming the new standard as more developers update their code to be compatible with it and take advantage of its advantages. WordPress has even pledged support for the next version of PHP, 8.1, which promises even more speed improvements and security enhancements over 8.0. So if you’re looking to make your website faster and more secure, start making the switch to PHP 8 now – it’s well worth it!



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!