How To Install PHP 7, 7.2 & 7.3 On CentOS 7

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, which is a bundle of software 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 7.

how to install php on centos

Prerequisites

  • Access to a user account with sudo privileges
  • Access to a terminal window/command-line
  • The yum package manager, included by default
  • A third-party software repository, detailed below

How to Install PHP 7.2 with Apache on CentOS

Step 1: Choose PHP Version to Install

The newest stable release version of PHP is PHP 7.3.1. However, some software repositories default to older versions of the software.

One advantage of using an older release is its 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 7.2 is a great place to start.

Step 2: Enable Additional Software Repositories

By default, the yum package manager does not include access to the repositories that store the PHP packages. Therefore, you need to enable access to these software packages.

1. First, start by installing the yum-utils package by entering the following command in a terminal window:

sudo yum install yum-utils –y

2. Then, enable the epel-release repository by entering the following:

sudo yum install epel-release –y

3. Finally, add the following software repositories that contain the PHP packages:

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

Note: This procedure uses Remi’s Software Repository. You can find more information about this utility by visiting the FAQ page.

Step 3: Enable Repository For PHP Version

Next, use the yum-config-manager to point your installer to the repository of the PHP version you want to install:

sudo yum-config-manager ––enable remi–php70

This command configures your system to download and install PHP 7.0.
To install PHP 7.1, change the entry as follows:

sudo yum-config-manager ––enable remi–php71

Likewise, replace the last two characters with 72 to install PHP 7.2.

Step 4: Install PHP and its Dependencies

At this point, you can install PHP. Enter the following command in the terminal:

sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql –y

As you see, the line includes many of the modules and add-ons that help PHP integrate with your local server configuration.

The output will also indicate which dependencies have been installed.

How to install PHP on CentOS

Step 5: PHP Modules

You may have noticed that the installation script 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, type in the following command:

sudo yum search php | more
Generate a list of PHP modules.

Step 6: Verify PHP Version

Finally, verify your installation was successful. Check which version of PHP you are running with the command:

php –v
How to check PHP version installed?

Optional: Install PHP 7.3

You can enable Remi’s PHP 7.3 repository the same way you enabled other repositories in Step 3. However, this installation may fail if you have extensions that are not compatible with PHP 7.3.

If this is a first-time installation and you are confident that your system is compatible, you can enable PHP 7.3. Install the release with the following commands:

sudo yum-config-manager ––enable remi–php73
sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql –y

Conclusion

As you can see, installing PHP on CentOS is fairly straightforward. You can find more information about the latest downloadable version on the developer’s download page.

Was this article helpful?
YesNo
Dejan Tucakov
Dejan is the Head of Content at phoenixNAP with over 8 years of experience in Web publishing and technical writing. Prior to joining PNAP, he was Chief Editor of several websites striving to advocate for emerging technologies. He is dedicated to simplifying complex notions and providing meaningful insight into data center and cloud technology.
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
May 6, 2019

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