How to Upgrade to PHP 7.4 on CentOS 7

I have seen several different versions of this process, but none of them worked… Here is the summary, but make sure you keep track of all changes in case you need to revert.

Note: If you are using CentOS Web Panel, the update cannot be done manually. That CWP update is currently under testing, coming soon.

Version #1

1. Verify current version of PHP

Type in the following to see the current PHP version:

php -v

The output should be something like this:

PHP 5.4.16 (cli) (built: Apr 12 2018 19:02:01)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

2. Install the REMI and EPEL repositories

If you don’t already have them, install the Remi and EPEL repositories:

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://rpms.remirepo.net/enterprise/remi-release-7.rpm

Enable the Remi repository globally:

/etc/yum.repos.d/remi.repo

Under the section that looks like [remi] make the following changes (enabled=0):

[remi]
name=Remi's RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/remi/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/7/remi/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/remi/mirror
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Enable the Remi PHP7.4 Repository:

/etc/yum.repos.d/remi-php74.repo

Under the section that looks like [remi-php74] make the following changes (enabled=1):

[remi-php74]
name=Remi's PHP 7.4 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php74/$basearch/
mirrorlist=https://rpms.remirepo.net/enterprise/7/php74/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/php74/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

3. Install required extensions

You can list the existing extensions using command below :

yum --enablerepo=remi-php73 search php73-

Simply prefix php7.4- with an extension that you need to install.
For example to get the php-curl extension use php7.4-curl

To view enabled modules, run:

$ php --modules

You can run the following command to search other available PHP modules under configured yum repositories. Below example command search for all modules for PHP 7.3.

yum --enablerepo=remi-php73 search php | grep php73
sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd
yum --enablerepo=remi-php70 install php74-cli php74-common php74w-bcmath php74-dba php74-devel php74-embedded php74-fpm php74-gd php74-imap php74-interbase php74-intl php74-ldap php74-mbstring php74-mcrypt php74-mysql php74-odbc php74-opcache php74-pdo php74-pdo_dblib php74-pear php74-process php74-pspell php74-recode php74-tidy php74-xml php74-xmlrpc

4. Upgrade PHP 5.4 to PHP 7.4

Now we can upgrade PHP. Just type in the following command:

yum -y upgrade php*

This above procedure installed the updated PHP, however it isn’t active.

 

Version #2

Here is another version. Most steps are similar, but there are additional ones:

1. Download Remi and EPEL Repository packages

First, download Remi and EPEL Repository packages:

$ wget -q http://rpms.remirepo.net/enterprise/remi-release-7.rpm
$ wget -q https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2. Enable Remi and EPEL Repository

Install both previously downloaded packages and enable Remi PHP 7 repo:

# rpm -i remi-release-7.rpm epel-release-latest-7.noarch.rpm

FOR PHP 7.0 EXECUTE:
# yum-config-manager --enable remi-php70
FOR PHP 7.1 EXECUTE:
# yum-config-manager --enable remi-php71
FOR PHP 7.2 EXECUTE:
# yum-config-manager --enable remi-php72
FOR PHP 7.3 EXECUTE:
# yum-config-manager --enable remi-php73
FOR PHP 7.4 EXECUTE: 
# yum-config-manager --enable remi-php74

Current latest PHP version from Remi repository is 7.4. Amend the above command for any other subsequent PHP releases.

??? This may be an alternative comman:

yum --enablerepo=remi update remi-release

These are the missing steps cut from another tutorial:

Step 5 – Now delete current PHP installation but make sure you stopped Apache first:

service httpd stop

yum -y remove php

Step 6 – Finally, issue this command to install PHP 7.4 with all necessary modules:

yum –enablerepo=remi-php74 install php74-php php74-php-pear php74-php-bcmath php74-php-pecl-jsond-devel php74-php-mysqlnd php74-php-gd php74-php-common php74-php-fpm php74-php-intl php74-php-cli php74-php php74-php-xml php74-php-opcache php74-php-pecl-apcu php74-php-pecl-jsond php74-php-pdo php74-php-gmp php74-php-process php74-php-pecl-imagick php74-php-devel php74-php-mbstring

Step 7 – Next, at this point, you can simply stop the old PHP-fpm service and start the newly installed PHP74-fpm

(Obviously you should use the alternative if you have php-cgi)

service php-fpm stop

service php70-php-fpm start

Step 8 – Finally you can delete the old php symblink and create a new one:

rm /usr/bin/php
ln -s /usr/bin/php70 /usr/bin/php

Also do not forget to restart Apache service / httpd:

service httpd restart

These may or may not be relevant:

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

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 file

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. We’ll change the user to nginx and switch from TCP socket to Unix socket. To do so edit the lines highlighted in yellow:

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

Make sure the /var/lib/php directory has the correct ownership:

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

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

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:

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

For the new configuration to take effect, restart the Nginx service:

sudo systemctl restart nginx