How to Install PHP on CentOS and Rocky Linux

May 24, 2019

Introduction

PHP is a programming language often used to automate server tasks. It is part of the LAMP (Linux, Apache, MySQL, PHP) stack, a software bundle used for running internet servers and services. PHP handles dynamic content, database requests, and processing and displaying data.

This step-by-step guide shows how to install PHP on CentOS or Rocky Linux.

How to install PHP on CentOS and Rocky Linux - a tutorial.

Prerequisites

  • A user account with root privileges.
  • Access to a terminal window.
  • A third-party software repository, detailed below.

How to Install PHP with Apache on CentOS and Rocky Linux

CentOS and Rocky Linux use the same package manager, dnf (or its predecessor, yum, in older CentOS versions). Since Rocky Linux is a community-supported rebuild of Red Hat Enterprise Linux (RHEL), just like CentOS was, their package management and software installation steps are identical.

Note: Learn about the CentOS EOL and see how it differs from Rocky Linux.

Follow the steps below to install PHP on your preferred distribution.

Step 1: Choose PHP Version to Install

The latest stable PHP version at the time this article was written was PHP 8.3. However, some software repositories default to older versions of the software.

While older releases provide high stability and reliability, newer releases may include more features, but are often more experimental and could cause system instability. If you cannot decide which version is right for you, version 8.3 is a great place to start.

Step 2: Enable Additional Software Repositories

By default, the yum/dnf package managers do not include access to the repositories that store the PHP packages. Therefore, you need to enable access to these software packages.

1. Ensure your system is updated by running:

sudo dnf update -y

2. Next, enable the epel-release (Extra Packages for Enterprise Linux) repository with the command below:

sudo dnf install epel-release -y

3. Finally, add the Remi software repository that provides the latest PHP versions:

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

Step 3: Install PHP

At this point, you can install PHP. Follow the steps below:

1. Run the following command to reset the module, which removes any custom PHP module stream selection:

sudo dnf module reset php
Resetting the PHP module on Rocky Linux.

2. Enable the PHP version you want to install with:

sudo dnf module install php:remi-8.3 -y
Installing PHP and additional modules on Rocky Linux.

The command installs many of the modules and add-ons that help PHP integrate with your local server configuration.

Step 4: PHP Modules

The installation in the previous step included more than just the base PHP package. Many of these are modules that provide basic functionality to PHP. Installing this set helps ensure that your PHP installation meets your expectations for usage.

Like many other Linux applications, you can enhance your system's PHP functionality using modules.

To search for available modules and generate a list of modules, run the following command:

sudo dnf search php | more
Searching for PHP modules after installation.

To install a module from the list, use the syntax below:

sudo dnf install [module_name]

Step 6: Verify PHP Version

Finally, verify your installation was successful. Check your PHP version with the command:

php -v
Checking php version after installation on Rocky Linux.

Conclusion

Installing PHP on CentOS or Rocky Linux is fairly straightforward. PHP helps developers create dynamic and interactive web applications, powers popular platforms like WordPress and seamlessly integrates with databases like MySQL.

Next, see how to install phpMyAdmin on CentOS or Rocky Linux.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Install and Use PHP Composer on CentOS 7
September 10, 2019

Composer is an application that tracks dependencies for a project, allowing you to specify a set of libraries for a specific project. It identifies the versions and dependencies and installs them to the project. This guide will...
Read more
How To Install XAMPP On CentOS/RHEL 7.0
September 5, 2019

The XAMPP stack is an open-source Apache distribution of a PHP development environment consisting of cross-platform software (X): Apache (A), MariaDB (M), PHP and Perl (P). In this article, you will find simple instructions on ...
Read more
How to Connect to MySQL using PHP
April 23, 2024

To access and add content to a MySQL database, you must first establish a connection between the database and a PHP script. In this guide learn how to use PHP to connect to...
Read more
How To Install PHP On Ubuntu 20.04 or 22.04
November 24, 2022

PHP is a script-based server-side programming language. PHP is often used to automate server tasks and is the component that handles tasks like dynamic content, database requests, and processing and displaying data. This guide...
Read more