CentOSLinuxTutorials

How To Install PHP 7.4 on CentOS 7

Install PHP 7.4 on CentOS 7

In this tutorial, we will show you how to install and configuration of PHP 7.4 on CentOS 7. PHP 7.4 represents a significant milestone in web development, offering enhanced performance, improved security features, and powerful new functionalities that make it essential for modern applications. For CentOS 7 users, upgrading to PHP 7.4 provides access to features like typed properties, arrow functions, and substantial performance improvements over older versions.

This comprehensive guide walks you through the complete process of installing PHP 7.4 on CentOS 7 using the REMI repository method. Whether you’re a system administrator managing production servers or a developer setting up a testing environment, this tutorial provides detailed instructions, troubleshooting solutions, and optimization tips to ensure a successful installation.

Prerequisites and System Requirements

Before beginning the PHP 7.4 installation process, ensure your CentOS 7 system meets the necessary requirements and you have proper access credentials.

System Requirements:

  • CentOS 7 (any minor version)
  • Root or sudo access to the system
  • Minimum 1GB RAM (2GB recommended for production)
  • At least 2GB free disk space
  • Active internet connection for downloading packages

Pre-Installation Checklist:
Start by updating your system to ensure all packages are current. This prevents potential conflicts during the PHP installation process.

sudo yum update -y

Check if PHP is already installed on your system:

php -v

If PHP is currently installed, note the version for potential compatibility considerations. Back up any existing PHP configurations before proceeding with the installation.

Important Considerations:
Production environments require careful planning. Consider scheduling maintenance windows and testing the installation process in a staging environment first. Existing applications may require compatibility testing with PHP 7.4 features.

Understanding the REMI Repository

The REMI repository serves as the most reliable source for installing modern PHP versions on Enterprise Linux distributions like CentOS 7. Maintained by Remi Collet, this repository provides up-to-date, well-tested PHP packages that aren’t available in default CentOS repositories.

Why Choose REMI Repository:
Default CentOS 7 repositories only provide PHP 5.4, which lacks modern features and security updates. REMI repository offers multiple PHP versions including 7.4, with regular security patches and performance improvements.

The repository works in conjunction with EPEL (Extra Packages for Enterprise Linux), providing comprehensive package management and dependency resolution. This approach ensures system stability while delivering cutting-edge PHP functionality.

REMI Repository Advantages:

  • Regular security updates and bug fixes
  • Multiple PHP versions available simultaneously
  • Extensive extension library support
  • Community-driven development with professional maintenance
  • Seamless integration with existing CentOS package management

Step-by-Step Installation Process

Step 1: Installing Required Repositories

The foundation of PHP 7.4 installation begins with setting up EPEL and REMI repositories on your CentOS 7 system.

Installing EPEL Repository:
EPEL repository provides essential dependencies required by REMI packages.

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

Verify EPEL installation:

yum repolist | grep epel

Installing REMI Repository:
Download and install the REMI repository package:

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

The installation process will prompt for GPG key acceptance. Type ‘y’ to accept the key and continue.

Step 2: Installing YUM Utilities

YUM utilities provide advanced repository management capabilities essential for PHP version selection.

sudo yum install -y yum-utils

These utilities enable repository enabling/disabling and configuration management through the yum-config-manager command.

Step 3: Enabling PHP 7.4 Repository

Configure the system to use PHP 7.4 packages from the REMI repository.

First, disable any conflicting PHP repositories:

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

Enable the specific PHP 7.4 repository:

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

Verify repository configuration:

yum repolist enabled | grep remi-php74

Step 4: System Update and PHP Installation

Update the system to incorporate new repository configurations:

sudo yum update -y

Install PHP 7.4 core packages:

sudo yum install php php-cli

The installation process displays package information and dependencies. Review the installation summary and type ‘y’ to proceed.

Step 5: Installing Essential PHP Extensions

PHP extensions provide additional functionality required by most web applications.

Install commonly used extensions:

sudo yum install php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-ldap php-fileinfo php-imap

Extension Descriptions:

  • php-fpm: FastCGI Process Manager for improved performance
  • php-mysqlnd: MySQL Native Driver for database connectivity
  • php-zip: Archive manipulation capabilities
  • php-gd: Graphics library support for image processing
  • php-mbstring: Multi-byte string handling for internationalization
  • php-curl: HTTP client functionality for API integrations
  • php-xml: XML parsing and manipulation support

Step 6: Service Configuration

Configure PHP-FPM service for optimal performance:

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

Check service status:

sudo systemctl status php-fpm

PHP Configuration and Optimization

Basic PHP Configuration

Locate and modify the main PHP configuration file for your environment needs.

The primary configuration file is located at /etc/php.ini. Key settings to review include:

Memory Management:

memory_limit = 256M
max_execution_time = 300
max_input_time = 300

Error Reporting (Development):

display_errors = On
error_reporting = E_ALL
log_errors = On

Production Environment Settings:

display_errors = Off
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
log_errors = On

PHP-FPM Optimization

Configure PHP-FPM pools for enhanced performance. Edit the main pool configuration:

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

Key Configuration Parameters:

pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 35

Restart PHP-FPM after configuration changes:

sudo systemctl restart php-fpm

Web Server Integration

Apache Integration:
Install and configure Apache web server:

sudo yum install httpd
sudo systemctl start httpd
sudo systemctl enable httpd

Create a PHP test file:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Nginx Integration:
For Nginx users, configure PHP-FPM socket communication:

location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Verification and Testing

Version Verification

Confirm successful PHP 7.4 installation:

php -v

Expected output:

PHP 7.4.33 (cli) (built: Aug 1 2023 09:00:17) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Module Verification

List all installed PHP modules:

php --modules

This command displays core modules and extensions, helping verify complete installation.

Functionality Testing

Create comprehensive test scripts to validate PHP functionality:

Basic Test Script:

<?php
echo "PHP Version: " . phpversion() . "\n";
echo "Server API: " . php_sapi_name() . "\n";

// Test MySQL connectivity
if (extension_loaded('mysqlnd')) {
    echo "MySQL Native Driver: Available\n";
}

// Test file operations
if (extension_loaded('zip')) {
    echo "ZIP Extension: Available\n";
}
?>

Web-based Testing:
Access http://your-server-ip/info.php to view comprehensive PHP configuration information.

Common Issues and Troubleshooting

Repository Configuration Problems

GPG Key Issues:
If GPG key verification fails, manually import keys:

sudo rpm --import https://rpms.remirepo.net/RPM-GPG-KEY-remi

Package Conflicts:
Resolve dependency conflicts by removing conflicting packages:

sudo yum remove php-common
sudo yum autoremove

Installation Conflicts

Existing PHP Versions:
When multiple PHP versions conflict, remove old installations completely:

sudo yum remove php*
sudo yum clean all

Permission Issues:
Ensure proper file permissions for PHP-FPM:

sudo chown -R apache:apache /var/lib/php/session
sudo chmod 755 /var/lib/php/session

Service Startup Problems

PHP-FPM Won’t Start:
Check error logs for specific issues:

sudo journalctl -u php-fpm -f

Common fixes include:

  • Correcting configuration syntax errors
  • Adjusting memory limits
  • Verifying user permissions
  • Checking port availability

Security Best Practices

PHP Security Configuration

Implement security hardening measures in php.ini:

expose_php = Off
allow_url_fopen = Off
allow_url_include = Off
disable_functions = exec,passthru,shell_exec,system,proc_open,popen

File System Security

Set appropriate permissions for PHP files and directories:

sudo find /var/www/html -type f -exec chmod 644 {} \;
sudo find /var/www/html -type d -exec chmod 755 {} \;

Session Security

Configure secure session handling:

session.cookie_httponly = 1
session.cookie_secure = 1
session.use_strict_mode = 1

Performance Optimization

OPcache Configuration

Enable and configure OPcache for improved performance:

sudo yum install php-opcache

Configure OPcache settings:

opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2

System-Level Optimizations

Memory Allocation:
Optimize system memory allocation for PHP processes:

echo 'vm.swappiness = 10' | sudo tee -a /etc/sysctl.conf

File System Performance:
Consider using faster file systems like XFS for better I/O performance with PHP applications.

Maintenance and Updates

Regular Update Procedures

Keep PHP 7.4 updated with security patches:

sudo yum update php\*

Monitoring and Health Checks

Create monitoring scripts to track PHP performance:

#!/bin/bash
php -m > /tmp/php_modules.txt
php -v > /tmp/php_version.txt
systemctl status php-fpm > /tmp/php_fpm_status.txt

Backup Strategies

Implement regular configuration backups:

sudo tar -czf /backup/php-config-$(date +%Y%m%d).tar.gz /etc/php.ini /etc/php-fpm.d/

Advanced Configuration Tips

Multiple PHP Versions

REMI repository supports running multiple PHP versions simultaneously using Software Collections. Install alternative versions:

sudo yum install php56-php php72-php php74-php

Custom Extension Installation

Install additional PHP extensions from PECL:

sudo yum install php-devel gcc
sudo pecl install extension_name

Development vs Production Configurations

Create environment-specific configurations by maintaining separate php.ini files and switching between them based on deployment requirements.

Congratulations! You have successfully installed PHP 7. Thanks for using this tutorial for installing PHP 7.4 on your CentOS 7 system. For additional help or useful information, we recommend you check the official PHP website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button