Additional PHP versions on CentOS

Category: CentOS
Created: 2019-10-23
Updated: 2021-08-20

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
    
  • PHP 8.0: CentOS/RHEL 7/8

    yum install lc-php80-remi
    
  • PHP 8.1 (beta): CentOS/RHEL 7/8

    yum install lc-php81-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.

Change default PHP version

LiveConfig uses the PHP version provided by the respective Linux distribution as “default version”.

If you want to use a different PHP interpreter as the standard version, create a file /etc/liveconfig/lua.d/php-default.lua and enter the following line there:

-- change PHP default version
LC.web.PHPDEFAULT = 'php73'

Instead of php73 please enter the code of the desired PHP version (see listing when running liveconfig --diag).

If you run liveconfig --diag again, the selected version should then be marked with [DEFAULT]. Restart LiveConfig to apply the changes.

Remove old PHP versions

The best way to delete outdated PHP versions is as follows:

  1. In LiveConfig go to Server Management -> Web. In the box PHP versions you can see the available PHP versions on the server and how many subdomains they are used by.

    To switch off the use step by step, you can set the restricted option from LiveConfig 2.11 - the affected PHP version can then still to be used with the subdomains configured in this way, but can no longer be selected for new configurations / subdomains.

  2. If a PHP version is ultimately no longer used (number of subdomains in the Usage column = 0), then delete the corresponding PHP interpreter from the server, e.g.:

    yum erase php56
    
  3. Then check in LiveConfig that the PHP version you just deleted no longer appears in the list of available PHP versions. Old PHP packages or packages not provided by LiveConfig may have to be removed manually from custom.lua, and LiveConfig or lcclient may have to be restarted on the affected server.

  4. Finally, you can delete the configuration files that are no longer required from the server, e.g.:

    root@srv:~# cd /var/www
    root@srv:/var/www# chattr -R -i web*/conf/php56
    root@srv:/var/www# rm -rf web*/conf/php56