Terug naar het artikeloverzicht

How to install PHP 8 on CentOS & RHEL

Laptop with code on screen
Photo by Ben Griffiths on Unsplash

In this guide, I will show you how to install PHP 8 on CentOS/RHEL 7 and 8 on a Linux web server running either Apache or Nginx.

Prerequisites

Before we get started, let's make sure we have everything we need to install PHP 8 properly. You will need:

  • A server running CentOS or RHEL (Red Hat Enterprise Linux). For this guide, we will use a dedicated server from Spin Servers.
  • Either the Apache or Nginx web server installed.
  • A cup of hot tea or coffee.

CentOS/RHEL 8 uses the dnf package manager, while CentOS/RHEL 7 uses yum. Where the commands differ, both versions are shown.

Step 1: Enable the EPEL and Remi repositories

Before installing PHP 8, we need to enable two repositories: EPEL (Extra Packages for Enterprise Linux) and Remi, which provides many versions of PHP for RHEL-based systems.

Step 1.1: Enable EPEL

Enable EPEL first with this command:

For CentOS/RHEL 7:

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

For CentOS/RHEL 8:

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

Step 1.2: Install the Remi repository

Now install the Remi repository:

For CentOS/RHEL 7:

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

For CentOS/RHEL 8:

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

Step 2: Enable PHP 8

Now that the repositories are set up, let's tell the system to use PHP 8.

For CentOS/RHEL 7: enable the Remi PHP 8.0 repository with yum-config-manager:

sudo yum install -y yum-utils
sudo yum-config-manager --enable remi-php80

For CentOS/RHEL 8: first, list the available PHP modules:

sudo dnf module list php

This command outputs a list of the PHP modules (versions) you can install. Near the bottom of the list, you should see php remi-8.0. Reset any previously selected PHP version, then enable 8.0:

sudo dnf module reset php -y
sudo dnf module enable php:remi-8.0 -y

The output should end with Complete!.

Step 3: Install PHP 8 for Apache or Nginx

Next, install PHP 8 for the web server you have on your system. Don't worry, these steps are painless. On CentOS/RHEL 7, use yum instead of dnf in the commands below.

Step 3.1: PHP 8 for Apache

For Apache-based systems, use this command:

sudo dnf install php php-cli php-common

Step 3.2: PHP 8 for Nginx

For Nginx-based systems, install PHP-FPM as well, then start it and enable it at boot:

sudo dnf install php php-cli php-common php-fpm
sudo systemctl enable --now php-fpm

Step 4: Verify the PHP 8 installation

Let's verify that the right version of PHP is installed and working correctly. First, check the PHP version:

php -v

The output should show PHP 8.0.x, followed by the build date and some more information.

Another way to verify the version is to create a PHP information page that you can open in your browser using the server's IP address. This command creates an info.php file and opens it in the vim editor:

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

Insert this text, then save and exit:

<?php
phpinfo();
?>

Now restart your web server so it picks up the changes. Run the command for your web server:

Apache:

sudo systemctl restart httpd

Nginx:

sudo systemctl restart nginx

In a web browser, you can now view the build information by going to:

http://server-ip/info.php

Important: info.php exposes details about your server's configuration. Once you have checked the version, delete it:

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

Bonus step: Add PHP extensions

You can extend PHP's functionality by adding extensions such as openssl.

To see a list of compiled PHP extensions, use:

php -m

To install a PHP extension, use this command, replacing {extension} with the extension's name:

sudo dnf install php-{extension}

Look at you! You have successfully installed PHP 8 on your web server. If you still have some tea or coffee left, I have plenty more tutorials on the Spin Servers blog.


If you have questions about any of our services, please contact us via email at [email protected] or by using our live chat at spinservers.com.

Spin Servers is a US-based provider of dedicated servers & affordable private cloud solutions. Our motto is to let you "spin up servers" fast, seamlessly, and affordably.