Easy Steps To Install PHP 7.4 on Centos 7 – OrcaCore

Posted on

Easy Steps To Install PHP 7.4 on Centos 7 - OrcaCore

Easy Steps To Install PHP 7.4 on Centos 7 – OrcaCore

This tutorial is designed to guide you through the process of installing PHP 7.4 on Centos 7. While PHP 8.2 is currently the latest version, there may be specific reasons why you need to install PHP 7.4. This guide, brought to you by OrcaCore, provides step-by-step instructions for achieving this.

Important Note: It’s generally recommended to use the latest PHP version for security and performance reasons. Consider exploring our guide on Install PHP 8.2 on Centos 7 if you’re open to using the most up-to-date version.

To follow this tutorial, you’ll need to be logged in to your Centos 7 server as a non-root user with sudo privileges. If you haven’t already, refer to our guide on Initial Server Setup with Centos 7 for instructions.

Step 1 – Install Epel Repository on Centos 7

First, update your local package index to ensure you have the latest package information:

sudo yum update -y

Next, install the EPEL (Extra Packages for Enterprise Linux) repository, which provides additional packages not available in the default Centos repositories:

sudo yum install epel-release -y

Step 2 – Install Remi Repository on Centos 7

PHP 7.4 is not included in the standard Centos 7 repositories. To access it, you’ll need to install the Remi repository, which specifically provides PHP 7.4 packages. Use the following command:

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

After installing the Remi repository, update the system again:

sudo yum update -y

Step 3 – Remove the PHP version Installed

If you have any existing PHP installations, it’s crucial to remove them before proceeding. This prevents conflicts and ensures a clean installation of PHP 7.4. Remove existing PHP and PHP-FPM installations using:

sudo yum remove php php-fpm -y

Remove any remaining PHP extension packages:

sudo yum remove php* -y

Disable the Remi repository for other PHP versions to avoid accidentally installing them:

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

Step 4 – Enable PHP 7.4 on Centos 7

To specifically install PHP 7.4, you need to enable the Remi repository for PHP 7.4. This tells yum to use the Remi repository when installing PHP packages:

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

Verify that the Remi repository for PHP 7.4 is enabled by listing the enabled repositories:

sudo yum repolist

You should see output similar to this, confirming that the remi-php74 repository is enabled:

**Output**
remi-php74                                               | 3.0 kB     00:00
remi-php74/primary_db                                      | 263 kB   00:00
repo id          repo name                                                status
base/7/x86_64    CentOS-7 - Base                                          10,072
epel/x86_64      Extra Packages for Enterprise Linux 7 - x86_64           13,761
extras/7/x86_64  CentOS-7 - Extras                                           515
**remi-php74       Remi's PHP 7.4 RPM repository for Enterprise Linux 7** - x    459
remi-safe        Safe Remi's RPM repository for Enterprise Linux 7 - x86_  5,263
updates/7/x86_64 CentOS-7 - Updates                                        5,053
repolist: 35,123

Step 5 – Install PHP 7.4 on Centos 7

Now that the Remi repository for PHP 7.4 is enabled, you can proceed with the installation. Install PHP 7.4 along with some commonly used extensions using the following command:

sudo yum -y install php php-{cli,fpm,mysqlnd,zip,devel,gd,mbstring,curl,xml,pear,bcmath,json,opcache,redis,memcache}

If you want to install all available PHP extensions, you can use the following command, replacing xxx with the specific extension name:

sudo yum install php-xxx

Verify the PHP installation by checking the PHP version:

php --version

The output should display PHP 7.4.x, confirming a successful installation.

Verify PHP 7.4 on Centos 7

Step 6 – Configure PHP-FPM Service on Centos 7

By default, PHP-FPM (FastCGI Process Manager) on Centos 7 is configured to run under the Apache user. If you’re using Nginx as your web server, you need to adjust the configuration file.

Open the www.conf file with a text editor (e.g., vi):

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

Locate the user and group directives and change their values to nginx:

user = nginx
group = nginx

Save the changes and close the file.

Restart the PHP-FPM service to apply the new configuration:

sudo systemctl restart php-fpm

For Nginx to properly process PHP files, you need to add a specific configuration block to your server block configuration. Add the following within your server block:

    location ~ .php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

For detailed information about PHP, consult the PHP Documentation page.

Conclusion

You have now successfully installed PHP 7.4 on Centos 7. Keep in mind that PHP 7.4 reached its "End of Life" in November 2022. Therefore, using LTS (Long Term Support) versions of PHP is highly recommended for security updates and ongoing support. While this article provides steps to install PHP 7.4 on Centos 7, it is crucial to consider using a newer version when possible.

You might also find these articles helpful:

Install PHP 8.2 on Rocky Linux 8

Install PHP 8.2 on Ubuntu 22.04

How To Install PHP 8.2 on Debian 11

Alternative Solutions for Installing PHP 7.4 on Centos 7

While the Remi repository method is a common way to install PHP 7.4 on Centos 7, here are two alternative approaches you can consider:

1. Using Docker Containers:

Docker provides a containerization solution that allows you to run applications in isolated environments. This approach is excellent for managing different PHP versions without affecting the host system.

  • Explanation: Docker containers encapsulate the PHP runtime and its dependencies, ensuring consistency across different environments. You can pull a pre-built PHP 7.4 image from Docker Hub or create your own Dockerfile to customize the environment. This approach avoids modifying the system-level PHP installation and provides better isolation.

  • Steps:

    1. Install Docker: Follow the official Docker documentation to install Docker on your Centos 7 system.

    2. Pull a PHP 7.4 Image:

      docker pull php:7.4-fpm

      This command pulls the official PHP 7.4 FPM image from Docker Hub. You can choose other variants, such as php:7.4-cli for command-line usage.

    3. Run the Container:

      docker run -d -v /path/to/your/project:/var/www/html -p 9000:9000 php:7.4-fpm
      • -d: Runs the container in detached mode (background).
      • -v /path/to/your/project:/var/www/html: Mounts your project directory to the container’s web root. Replace /path/to/your/project with the actual path.
      • -p 9000:9000: Maps port 9000 on the host to port 9000 in the container (PHP-FPM default port).
    4. Configure Nginx (Reverse Proxy): Configure Nginx to forward requests to the PHP-FPM container.

      server {
          listen 80;
          server_name yourdomain.com;
          root /path/to/your/project;
          index index.php index.html;
      
          location ~ .php$ {
              try_files $uri =404;
              fastcgi_pass 127.0.0.1:9000; # Or the IP address of your container
              fastcgi_index index.php;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
          }
      }

2. Using Software Collections (SCL):

Software Collections (SCL) enable you to install multiple versions of software on the same system without conflicts. This is achieved by installing the software in a separate directory and providing environment variables to access it.

  • Explanation: SCL allows you to install PHP 7.4 alongside other PHP versions. This is particularly useful if you need to maintain compatibility with older applications while using newer versions for other projects.

  • Steps:

    1. Install SCL Repository:

      sudo yum install centos-release-scl -y
    2. Install PHP 7.4 from SCL:

      sudo yum install rh-php74 -y
    3. Enable PHP 7.4 SCL: To use PHP 7.4, you need to enable the SCL environment.

      scl enable rh-php74 bash

      This command opens a new shell with PHP 7.4 enabled. To make this permanent, you can add the scl enable command to your .bashrc or .bash_profile file.

    4. Verify Installation:

      php --version
    5. Using PHP-FPM with SCL: PHP-FPM installed via SCL will have its configuration files located in /opt/rh/rh-php74/root/etc/php-fpm.d/. You will also need to configure Nginx to point to the correct socket for PHP-FPM.

      location ~ .php$ {
          try_files $uri =404;
          fastcgi_pass unix:/opt/rh/rh-php74/root/var/run/php-fpm/www.sock;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
      }

These alternative methods offer different approaches to installing and managing PHP 7.4 on Centos 7, providing flexibility based on your specific needs and environment. Docker offers isolation and portability, while SCL provides a way to manage multiple PHP versions on the same system without conflicts. When you install PHP 7.4 on Centos 7, consider these alternative solutions to find the best fit for your requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *