Read in 5 minutes

last updated 

Install PHP 7 on CentOS 7

This tutorial is also available for:

debian 9 ubuntu 18.04

CentOS 7 ships with PHP version 5.4 which has been officially EOL for quite some time and is no longer supported.

By using PHP 7 your applications will load faster and consume less system resources.

In this tutorial we will explain how to install or upgrade to PHP 7.x on a CentOS 7 system. Before installing a specific PHP 7.x version make sure that it is supported by your application.

Prerequisites

Before starting with this tutorial, make sure you are logged into your server with a user account with sudo privileges or with the root user. It is best practice to run administrative commands as sudo user instead of root. If you don’t have sudo user on your system you can create one by following these instructions.

Install Remi repository

PHP 7.x packages are available in several different repositories. We’ll use the Remi repository which provides newer versions of various software packages including PHP.

The Remi repository depends on the EPEL repository, so we need to install the EPEL repository first.

Run the following commands to install both EPEL and Remi repositories:

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

Yum may prompt you to import the repository GPG key. Type y and hit Enter.

In the following sections we will be covering how to install PHP 7.x by enabling the appropriate Remi repository. If you already have PHP 5.4 installed on your system yum will update the PHP packages.

Installing PHP 7.2 on CentOS 7

PHP 7.2 is the latest stable release of PHP. Most modern PHP frameworks and applications including WordPress, Drupal, Joomla and Laravel are fully supporting PHP 7.2.

The following steps describe how to install PHP 7.2 on a CentOS 7 machine.

  1. First enable the PHP 7.2 Remi repository by running the following command:

    sudo yum-config-manager --enable remi-php72
  2. Once the repository is enabled install PHP 7.2 and few most common PHP modules:

    sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd
    
  3. Verify the PHP installation, by typing the following command which will print the PHP version:

    php -v
    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

Use PHP 7.1 only if you’re going to install an applications such as Magento 2 that is not compatible with PHP 7.2.

Follow the steps below to install PHP 7.1.

  1. Enable the PHP 7.1 repository by typing:

    sudo yum-config-manager --enable remi-php71
  2. Install PHP 7.1 and few most common PHP modules:

    sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql
    
  3. To verify the installation, run the following command which will print the PHP version:

    php -v
    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 are using Apache as your web server then just restart the Apache service using the following command and you are good to go:

sudo systemctl restart httpd
Advertisement

Configuring PHP 7.x to work with Nginx

Unlike Apache, Nginx doesn’t have built in support for processing PHP files so we need to install a separate application such as PHP FPM which will handle the PHP files.

To install the PHP FPM package run the following command:

sudo yum install php-fpm

By default PHP FPM will run as user apache on port 9000. Change the user to nginx and switch from TCP socket to Unix socket edit the lines highlighted in yellow:

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

Once you made the changes, enable and start the PHP FPM service:

```bash
sudo systemctl enable php-fpm
sudo systemctl start php-fpm
```

Next, edit the Nginx virtual host directive and add the following location block so that Nginx can process PHP files:

<div class="highlight"><pre class="chroma"><code class="language-nginx" data-lang="nginx"><span class="k">server</span> <span class="p">{</span>

    <span class="c1"># . . . other code
</span><span class="c1"></span>
<span class="hl">    <span class="kn">location</span> <span class="p">~</span> <span class="sr">\.php$</span> <span class="p">{</span>
</span><span class="hl">        <span class="kn">try_files</span> <span class="nv">$uri</span> <span class="p">=</span><span class="mi">404</span><span class="p">;</span>
</span><span class="hl">        <span class="kn">fastcgi_pass</span> <span class="s">unix:/run/php-fpm/www.sock</span><span class="p">;</span>
</span><span class="hl">        <span class="kn">fastcgi_index</span> <span class="s">index.php</span><span class="p">;</span>
</span><span class="hl">        <span class="kn">fastcgi_param</span> <span class="s">SCRIPT_FILENAME</span> <span class="nv">$document_root$fastcgi_script_name</span><span class="p">;</span>
</span><span class="hl">        <span class="kn">include</span> <span class="s">fastcgi_params</span><span class="p">;</span>
</span><span class="hl">    <span class="p">}</span>
</span><span class="p">}</span>
<span class="k">```</span>

<span class="s">For</span> <span class="s">the</span> <span class="s">new</span> <span class="s">configuration</span> <span class="s">to</span> <span class="s">take</span> <span class="s">effect,</span> <span class="s">restart</span> <span class="s">the</span> <span class="s">Nginx</span> <span class="s">service:</span>

<span class="s">```bash</span>
<span class="s">sudo</span> <span class="s">systemctl</span> <span class="s">restart</span> <span class="s">nginx</span>
<span class="s">```</span>

<span class="c1">## Conclusion
</span><span class="c1"></span>
<span class="s">You</span> <span class="s">learned</span> <span class="s">how</span> <span class="s">to</span> <span class="s">install</span> <span class="s">PHP</span> <span class="mi">7</span> <span class="no">on</span> <span class="s">your</span> <span class="s">CentOS</span> <span class="s">server</span> <span class="s">and</span> <span class="s">how</span> <span class="s">to</span> <span class="s">configure</span> <span class="s">your</span> <span class="s">web</span> <span class="s">server</span> <span class="s">to</span> <span class="s">be</span> <span class="s">able</span> <span class="s">to</span> <span class="s">handle</span> <span class="s">PHP</span> <span class="s">files.</span>

<span class="s">If</span> <span class="s">you</span> <span class="s">have</span> <span class="s">any</span> <span class="s">question</span> <span class="s">or</span> <span class="s">feedback</span> <span class="s">do</span> <span class="s">not</span> <span class="s">hesitate</span> <span class="s">to</span> <span class="s">leave</span> <span class="s">a</span> <span class="s">comment.</span></code></pre></div>

This post is a part of the Install LEMP Stack on CentOS 7 series.
Other posts in this series:

Install PHP 7 on CentOS 7