How To Install PHP 7 on CentOS 7 / Redhat (RHEL) 7

Published: December 2, 2017 (Updated: Dec 2, 2017)

Enjoying this content? Subscribe to the Channel!

Supercharge Your Server: A Step-by-Step Guide to Installing PHP 7 on CentOS 7 (RHEL Compatible!)

Introduction: Why Upgrade Your Server to PHP 7?

Welcome back to Darren’s Tech Tutorials!

If you’re running a web application on a stable server environment like CentOS 7, you know how crucial stability is. But stability doesn’t mean sacrificing performance! PHP 7 brought enormous gains in speed, efficiency, and crucial new features compared to its PHP 5 predecessors.

In this comprehensive guide, we are going to walk through the exact steps required to install PHP 7 on your CentOS 7 server. Best of all? The exact same commands and process work perfectly if you are running Red Hat Enterprise Linux (RHEL) 7 as well!

Let’s dive in and give your server the modern processing power it deserves.


Prerequisite: Enabling the Remi Repository

On CentOS 7, the default software repositories often contain older, conservative versions of popular software. To get the modern, stable PHP 7 packages we need, we rely on a trusted third-party repository maintained by Remi Collet.

Before we install PHP, we must first enable two critical repositories:

  1. EPEL (Extra Packages for Enterprise Linux): Required by many third-party packages.
  2. Remi: Contains the specific, well-maintained PHP 7 packages we are targeting.

Step 1: Install the EPEL and Remi Repositories

Use the following commands to install the necessary repository packages. Remember, you must be logged in as root or use sudo for these steps.

A. Install EPEL

sudo yum install epel-release -y

B. Install the Remi Repository

Next, we install the Remi repository package, which provides access to multiple PHP versions (7.0, 7.1, 7.2, 7.3, 7.4, etc.):

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

C. Install Repository Management Tools

We need a utility to easily manage which PHP stream is active on our system.

sudo yum install yum-utils -y

Step 2: Selecting and Enabling PHP 7

With the Remi repository installed, we now use yum-config-manager to explicitly enable the specific version of PHP 7 we want. While many specific versions of PHP 7 exist, we recommend enabling PHP 7.4 as it offers a fantastic blend of speed and stability.

Note: If you needed a different PHP 7 version (e.g., PHP 7.2), you would replace remi-php74 with the appropriate module name.

A. Disable Default PHP Modules

First, ensure that any default, older PHP streams are disabled:

sudo yum-config-manager --disable 'remi-php*'

B. Enable the Desired PHP 7 Stream (PHP 7.4 Example)

Now, enable PHP 7.4:

sudo yum-config-manager --enable remi-php74

Step 3: Install PHP 7 and Essential Modules

With the correct repository enabled, we can now install PHP 7.

When installing PHP, you rarely just need the core package. You’ll typically need essential modules like CLI (Command Line Interface), FPM (FastCGI Process Manager for web servers like Nginx or Apache), and database connectors (like MySQL/MariaDB).

Run the following command to install the core packages and some of the most common extensions:

sudo yum install php php-cli php-fpm php-mysqlnd php-zip php-gd php-opcache php-intl php-curl php-mbstring -y

What did we just install?

  • php: The core PHP engine.
  • php-cli: Allows you to run PHP scripts from the command line.
  • php-fpm: Essential for running PHP alongside high-performance web servers (like Nginx).
  • php-mysqlnd: The modern native driver for database connectivity.
  • php-opcache: Crucial performance booster by caching compiled script bytecode.

Step 4: Verify Your Installation

The hard work is done! Now it’s time to confirm that PHP 7 is installed and correctly configured.

Run the version check command:

php -v

You should see an output confirming the PHP 7 version you installed (e.g., 7.4.x).

PHP 7.4.33 (cli) (built: Dec  5 2022 14:38:13) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

If you see the 7.x output, congratulations—you are running modern PHP 7!

Step 5: Start and Enable PHP-FPM

If you are setting this server up to handle dynamic web traffic (which you almost certainly are), you need to start the PHP-FPM service and ensure it starts automatically upon server reboot.

# Start the PHP-FPM service
sudo systemctl start php-fpm

# Enable the service to start on boot
sudo systemctl enable php-fpm

# Check the status to ensure it's running
sudo systemctl status php-fpm

Conclusion

Installing modern software on an enterprise Linux distribution can sometimes feel tricky, but by utilizing reliable repositories like Remi, we can successfully deploy high-performance solutions like PHP 7 on CentOS 7 and RHEL 7 quickly and easily!

You now have a powerful, faster, and more efficient server ready to handle whatever application you throw at it.

Did this tutorial help you speed up your server? Let me know in the comments below! Don’t forget to Like this post and Subscribe to Darren’s Tech Tutorials for more clear, actionable guides! Happy coding!