Additional PHP versions on CentOS

Category: CentOS
Created: 2019-10-23
Updated: 2020-03-31

CentOS is known for it’s stability, but also for some software packages being a little outdated. CentOS 7 comes with PHP 5.4.16 - the 5.4 series isn’t maintained any more since September 2015. Many web applications require more modern PHP version like 7.2 or later.

This article explains how to install additional PHP versions in parallel and register them with LiveConfig.

Adding the REMI repository

We recommend to install the ready-to-use PHP packages from the REMI repository. Alternatively you may install PHP from source or using another repository - in this case just jump to the registering PHP section.

Check if you have already enabled the REMI repository (yum repolist). If not done yet, add it:

  • CentOS / Red Hat Enterprise Linux 6.x:

    wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
    wget http://rpms.remirepo.net/enterprise/remi-release-6.rpm
    rpm -Uvh remi-release-6.rpm epel-release-latest-6.noarch.rpm
  • CentOS / Red Hat Enterprise Linux 7.x:

    wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    wget https://rpms.remirepo.net/enterprise/remi-release-7.rpm
    rpm -Uvh epel-release-latest-7.noarch.rpm remi-release-7.rpm
  • CentOS / Red Hat Enterprise Linux 8.x:

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

The REMI repository is not enabled by default. To enable, edit the file /etc/yum.repos.d/remi.repo. Find the line enabled=0 and change it to 1:

[...]
enabled=1
[...]

Installing PHP

The LiveConfig repository provides helpful packages which install the desired PHP version and automatically registers them with LiveConfig.

  • PHP 7.3: CentOS/RHEL 6/7/8

    yum install lc-php73-remi
  • PHP 7.4: CentOS/RHEL 7/8

    yum install lc-php74-remi

Registering PHP

Only if you have installed PHP manually or from another repository: for each additional version, create a corresponding Lua file in /etc/liveconfig/lua.d/. For example for PHP 7.3, name that file php73.lua.

-- register additional PHP interpreter with LiveConfig
LC.web.addPHP( {
  ['id']  = 'php73',
  ['cli'] = '/opt/php-7.3/bin/php',
  ['cgi'] = '/opt/php-7.3/bin/php-cgi',
  ['fpm'] = {
    ['bin']     = '/opt/php-7.3/sbin/php-fpm',
    ['start']   = 'service php73-fpm start',
    ['stop']    = 'service php73-fpm stop',
    ['reload']  = 'service php73-fpm reload',
    ['pool']    = '/etc/php-fpm/php73-fpm.d',
    ['sockets'] = '/var/run/php73-fpm'
  }
} )

Testing

Run liveconfig --diag (or on a client system: lcclient --diag) and watch the PHP section:

[root@centos#] liveconfig --diag
[...]
 - PHP 7.3.16 (code='php73')
   CGI/FastCGI: /opt/remi/php73/root/usr/bin/php-cgi
   FPM: /opt/remi/php73/root/usr/sbin/php-fpm
        pool config: /etc/opt/remi/php73/php-fpm.d
   default php.ini: '/etc/opt/remi/php73/php.ini'
[...]

Only if you have installed PHP manually or from another repository: restart LiveConfig (systemctl restart liveconfig) to enable the new PHP interpreter(s).

Installing PHP extensions

In most cases you also want to install popular PHP extensions. Just search for already available modules like

yum search php73-

Some frequently required modules are installed like this:

yum install php73-php-gd php73-php-imap php73-php-intl php73-php-json php73-php-mbstring php73-php-mysqlnd php73-php-pdo php73-php-xml php73-php-pecl-apcu php73-php-pecl-imagick

You only need to adjust the version number depending on your actual PHP version.