How to Install PHP on RHEL 7

PHP is the most widely used server-side scripting language for web development, powering applications ranging from simple contact forms to enterprise content management systems. Red Hat Enterprise Linux 7 ships with an older version of PHP in its base repositories, so for modern frameworks and security patches you need a third-party repository. The Remi repository is the recommended source for PHP 7.x and 8.x on RHEL 7, providing packages that are fully compatible with RHEL’s package management system. This guide walks you through enabling the Remi repository, installing PHP 7.4, configuring key runtime settings, and setting up PHP-FPM as a process manager for use with web servers like Nginx or Apache.

Prerequisites

  • A running RHEL 7 server with root or sudo access
  • An active Red Hat subscription or equivalent access to base repositories
  • The EPEL repository enabled (required by Remi as a dependency)
  • Basic familiarity with the command line and yum package management

Step 1: Enable the EPEL Repository

The Extra Packages for Enterprise Linux (EPEL) repository provides many packages that Remi depends on. Install the EPEL release package before proceeding:

sudo yum install -y epel-release

If EPEL is not available directly through yum on your system, download and install it manually:

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

Step 2: Install the Remi Repository

Download and install the Remi repository release package for RHEL 7:

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

Next, install yum-utils which provides the yum-config-manager command needed to enable specific Remi PHP streams:

sudo yum install -y yum-utils

Step 3: Enable the PHP 7.4 Remi Repository

The Remi repository ships multiple PHP versions, each in its own sub-repository. Only one PHP version stream should be enabled at a time. Enable PHP 7.4:

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

To verify which Remi PHP repositories are available and confirm remi-php74 is enabled:

yum repolist all | grep remi-php

Step 4: Install PHP and Common Extensions

Install the PHP core package along with extensions commonly required by web applications and frameworks:

sudo yum install -y php php-fpm php-mysqlnd php-opcache php-xml php-json 
    php-mbstring php-gd php-curl php-zip php-intl php-bcmath

A brief description of each extension:

  • php-fpm — FastCGI Process Manager for use with Nginx
  • php-mysqlnd — Native MySQL driver for PDO and mysqli
  • php-opcache — Bytecode cache that dramatically improves performance
  • php-xml — XML parsing and DOM manipulation
  • php-json — JSON encode/decode functions
  • php-mbstring — Multi-byte string handling (required by many frameworks)
  • php-gd — Image processing library
  • php-curl — HTTP client functions via libcurl
  • php-zip — Zip archive support (required by Composer)
  • php-intl — Internationalization functions
  • php-bcmath — Arbitrary precision mathematics

Step 5: Verify the PHP Version

Confirm that PHP 7.4 was installed correctly:

php -v

Expected output:

PHP 7.4.33 (cli) (built: Oct  3 2023 10:10:07) ( 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

List all loaded extensions to verify the ones you installed are present:

php -m

Step 6: Locate and Configure php.ini

PHP reads its configuration from php.ini at startup. On RHEL 7 with Remi, the CLI configuration is at /etc/php.ini and per-module settings live in /etc/php.d/. For PHP-FPM, the same /etc/php.ini is used. Open the file for editing:

sudo vi /etc/php.ini

Adjust the following key settings for a production web server:

; Maximum memory a script may consume
memory_limit = 256M

; Maximum size of an uploaded file
upload_max_filesize = 64M

; Maximum POST data size
post_max_size = 64M

; Maximum execution time in seconds
max_execution_time = 60

; Maximum input time for parsing request data
max_input_time = 60

; Timezone — set to your local zone
date.timezone = America/New_York

; Hide PHP version in response headers (security)
expose_php = Off

; Error display — Off on production, On for development
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log

Save the file. Changes take effect when PHP-FPM is restarted.

Step 7: Configure OPcache

OPcache is installed with php-opcache and its configuration lives in /etc/php.d/10-opcache.ini. Edit it for production workloads:

sudo vi /etc/php.d/10-opcache.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1

Step 8: Configure PHP-FPM

PHP-FPM runs as a separate daemon that web servers communicate with via a socket or TCP port. The main configuration file is /etc/php-fpm.conf and pool configurations are in /etc/php-fpm.d/. The default pool is defined in /etc/php-fpm.d/www.conf.

Open the default pool configuration:

sudo vi /etc/php-fpm.d/www.conf

Key settings to review and adjust:

; Pool name
[www]

; User and group the FPM worker processes run as
user = nginx
group = nginx

; Listen on a Unix socket (recommended for Nginx on same server)
listen = /run/php-fpm/www.sock

; Socket permissions
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

; Process management — dynamic adjusts workers based on load
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

; Restart workers after this many requests (prevents memory leaks)
pm.max_requests = 500

Step 9: Enable and Start PHP-FPM

Enable PHP-FPM to start on boot and start it immediately:

sudo systemctl enable php-fpm
sudo systemctl start php-fpm

Check the service status to confirm it is running:

sudo systemctl status php-fpm

Verify the socket file was created:

ls -la /run/php-fpm/www.sock

Step 10: Test PHP Processing

Create a test PHP file in the web root to confirm PHP is processing correctly. If you have Apache or Nginx configured, place the file in its document root (e.g., /var/www/html/):

sudo bash -c 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php'

Access the file in your browser at http://<server-ip>/phpinfo.php. You should see the PHP information page confirming version 7.4 and all loaded extensions. Remove this file after testing as it exposes system configuration details:

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

Conclusion

You have successfully installed PHP 7.4 on RHEL 7 using the Remi repository, configured essential runtime parameters in php.ini, tuned OPcache for production performance, and set up PHP-FPM as the process manager. PHP-FPM is now ready to handle requests forwarded from Nginx or Apache via the Unix socket. From here, you can install PHP frameworks like Laravel or Symfony, manage PHP libraries with Composer, or continue hardening the configuration for your specific workload. Remember to reload or restart PHP-FPM any time you change php.ini or pool configuration files with sudo systemctl reload php-fpm.