How to Install PHP on CentOS

How to Install PHP on CentOS

In this tutorial, we’re going to show you how to install PHP on CentOS. Beginner-friendly, step-by-step instructions on how to install different versions of PHP on different versions of CentOS.

Prerequisites

This is what you’ll need for our how to install PHP on CentOS tutorial:

  • The root user or a user with sudo privileges
  • SSH access to the server (or just open Terminal if you’re on a desktop)
  • A CentOS system. We tested this tutorial on CentOS 7 and CentOS 8 servers from Linode, but these instructions will work on any other RHEL-based distro. You can get a Linux VPS from any provider.

Depending on what kind of a distro setup you’re using and when you’re reading this, CentOS might have different versions included in their repos. As of writing, by default, CentOS 8 has no PHP installed, and the default version in the repos is PHP 7.2.24 – which is already past its EOL. CentOS 7 also has no PHP installed and the default version in the repos is 5.4.16 – which is way past its EOL. So if you just run the yum install php command on CentOS, you’ll get outdated versions.

Follow our tutorial to get the latest version of PHP on CentOS.

Step 1: Update CentOS

As always, you need to update your CentOS first:

yum update

Step 2: Install and configure necessary packages

First, you need to install the necessary (extra) packages:

yum install epel-release yum-utils

Next, we’ll need the Remi RPM repository.

If you’re using CentOS 7, run:

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

If you’re using CentOS 8, run:

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

Then, enable the PHP 8.1 module.

If you’re using CentOS 7, run:

yum-config-manager --enable remi-php81

If you’re using CentOS 8, run:

yum module enable php:remi-8.1

You can also install any other version of PHP by updating the command for the PHP module above.

Update the repos again:

yum update

Step 3: Install PHP on CentOS

And finally, after you enabled the PHP 8.1 module, you can install PHP 8.1 (or whatever version you enabled) with the following command:

yum install php

You can verify if PHP was installed by running

php -v

Which should give you a result similar to:

PHP 8.1.2 (cli) (built: Jan 18 2022 23:52:03) (NTS gcc x86_64)

Step 4: Install PHP extensions

There are some PHP extensions that are often used and needed for various apps and use-cases. To install a PHP package, just run the yum install php-<extension-name> command.

Here’s how to install some of the most common PHP extensions:

yum install php-fpm php-curl php-cli php-json php-mysql php-opcache php-dom php-exif php-fileinfo php-zip php-mbstring php-hash php-imagick php-openssl php-pcre php-xml php-bcmath php-filter php-pear php-gd php-mcrypt php-intl php-iconv php-zlib php-xmlreader

These are also most of the extensions you’ll need if you are installing WordPress.

And that’s all. You successfully installed the latest version of PHP on your CentOS.

Leave a comment

Your email address will not be published.