Feb 14, 2023 5 min read

How to Install PHP 7 on CentOS 7

Install PHP 7 on CentOS 7 with this beginner-friendly tutorial. Get step-by-step instructions and top FAQs.

Install PHP 7 on CentOS 7
Table of Contents

Choose a different version or distribution

Introduction

PHP is a popular server-side scripting language used for developing dynamic web applications. CentOS 7 is a widely used Linux distribution and in this tutorial, we will explain how to install PHP 7 on CentOS 7.

CentOS 7 ships with PHP version 5.4, which has been officially EOL for a while and is no longer supported.

Your applications will load faster and use fewer system resources if you use PHP 7. Check that your application supports a specific PHP 7.x version before installing it.

This tutorial will provide step-by-step instructions to help you install PHP 7 on CentOS 7.

Prerequisites

Before you begin, ensure that you are logged into your server as a user with sudo privileges or with the root user. It is recommended to use the sudo user instead of root when executing administrative commands. If your system does not already have a sudo user, you can create one by following these steps.

Enabling Remi repository

Packages for PHP 7.x are accessible from numerous repositories. We will make use of the Remi repository, which offers more recent versions of many software applications, including PHP.

The EPEL repository is a prerequisite for the Remi repository. To make the EPEL and Remi repositories available, use the following commands:

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

Yum might prompt you to import the repository's GPG key. Input y and press Enter.

We will go over how to install PHP 7.x by enabling the correct Remi repository in the sections that follow. yum will update the PHP packages on your system if PHP 5.4 is already installed.

Installing PHP 7.3 on CentOS 7

At the time of writing this tutorial, the current stable version of PHP is 7.3. The majority of modern PHP frameworks and applications, such as WordPress, Drupal, Joomla, and Laravel, fully support PHP 7.3.

Install PHP 7.3 on CentOS 7 by carrying out the subsequent steps.

1) Enable the PHP 7.3 Remi repository first:

sudo yum-config-manager --enable remi-php73

2) Install PHP version 7.3 together with some of the most popular PHP modules:

sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd
💡
Note: If you have already enabled the other version of PHP then first disable it using sudo yum-config-manager --disable remi-php7x and remove these packages and reinstall them.

3) By using the command that prints the PHP version, you can check the PHP installation:

php -v
Output

PHP 7.3.1 (cli) (built: Jan  8 2019 13:55:51) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.1, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.1, Copyright (c) 1999-2018, by Zend Technologies

Installing PHP 7.2 on CentOS 7

Use PHP 7.2 only if you want to install applications like Magento 2 that are incompatible with PHP 7.2.

The steps below demonstrate how to install PHP 7.2 on CentOS 7.

1) To begin, use the following command to enable the PHP 7.2 Remi repository:

sudo yum-config-manager --enable remi-php72

2) After the repository has been enabled, install PHP 7.2 and a few of the most popular PHP modules:

sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

3) Ensure PHP is installed correctly:

php -v
Output

PHP 7.2.9 (cli) (built: Aug 15 2018 09:19:33) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.9, Copyright (c) 1999-2018, by Zend Technologies

Installing PHP 7.1 on CentOS 7

Install PHP 7.1 by following the instructions below.

1) Enter the following to enable the PHP 7.1 repository:

sudo yum-config-manager --enable remi-php71

2) Set up PHP 7.1 and a few of the most popular PHP modules:

sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql
💡
Note: If you have already enabled the other version of PHP then first disable it using sudo yum-config-manager --disable remi-php7x and remove these packages and reinstall them.

3) Run the following command to print the PHP version and confirm the installation:

php -v
Output

PHP 7.1.21 (cli) (built: Aug 15 2018 17:56:55) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.21, Copyright (c) 1999-2018, by Zend Technologies

Configuring PHP 7.x to work with Apache

If you use Apache as your web server, all you need to do is use the following command to restart Apache and you are ready to go:

sudo systemctl restart httpd

Configuring PHP 7.x to work with Nginx

Nginx, unlike Apache, lacks built-in support for processing PHP files, so we must install a different application, such as PHP FPM, to manage the PHP files.

Run the following command to install the PHP FPM package:

sudo yum install php-fpm

PHP FPM will run as user apache on port 9000 by default. We will switch the TCP socket to a Unix socket and change the user to nginx. Edit these lines to do this:

sudo nano /etc/php-fpm.d/www.conf
...
user = nginx
...
group = nginx
...
listen = /run/php-fpm/www.sock
...
listen.owner = nginx
listen.group = nginx

Verify the ownership of the /var/lib/php directory:

chown -R root:nginx /var/lib/php

After making the necessary adjustments, activate and launch the PHP FPM service:

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

The following location block should be added to the Nginx virtual host directive so that Nginx can process PHP files:

server {

    # . . . other code

    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;
    }
}

Restart the Nginx service for the new configuration to take effect:

sudo systemctl restart nginx

FAQs to Install PHP 7 on CentOS 7

How do I check the version of PHP installed on my CentOS 7 server?

You can check the version of PHP installed on your CentOS 7 server by running the following command:
php -v

How do I install additional PHP modules?

You can install additional PHP modules by running the following command: sudo yum install php-module_name

How do I uninstall PHP 7 from CentOS 7?

You can uninstall PHP 7 by running the following command:

sudo yum remove php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql

How do I change the PHP time zone?

To change the PHP time zone, open the php.ini configuration file in your preferred text editor and find the line that starts with date.timezone. Change the value to your preferred time zone. For example, date.timezone = America/New_York.

How do I enable error reporting in PHP?

To enable error reporting in PHP, open the php.ini configuration file in your preferred text editor and find the line that starts with error_reporting. Change the value to E_ALL. Additionally, you can also set the display_errors directive to On to display errors on the web page. However, it is recommended to turn this off on a production server.

Conclusion

In this tutorial, we have explained how to install PHP 7 on CentOS 7. We have covered all the necessary steps, including installing the EPEL and Remi repositories, installing PHP 7 and verifying the installation.

We have also provided information on configuring PHP and restarting the Apache web server. Additionally, we have included the top 5 FAQs related to installing and configuring PHP on CentOS 7.

With this tutorial, you should now be able to install and configure PHP 7 on your CentOS 7 server.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.