Selecting different PHP versions for scripting using Core-Admin


#1

1. Introduction

In case you need a different php version for your console scripts or crons, you have several options:

  1. To use full path to desired PHP Core-Admin version:

    >> /usr/core-admin/php/<version>/bin/php

  2. To use Core-Admin php selection script so you can create an environment where all php commands will use activated version:

    >> core_admin_php <version>

The idea is to be able to select what php engine will be used without having to change scripts or default system php path (which might impact production applications).

2. Before continue, installing PHP engine needed

In order you can use a different PHP version provided by /usr/bin/php you can to install Core-Admin PHP packaged version first.

  1. For that, run the following to update your package list to the latest:

    >> crad-update.pyc -u

  2. Install/check requierd version. For example, to enable 7.3 it would be:

    >> core_admin_php 7.3
    >> php --version
    PHP 7.3.5 (cli) (built: May 20 2019 09:58:33) ( NTS )
    Copyright © 1997-2018 The PHP Group
    Zend Engine v3.3.5, Copyright © 1998-2018 Zend Technologies

3. Using full path to PHP engine needed

This is the easiest method because it allows to select a PHP engine with the same command. For example, to ejecute a script with PHP engine 7.3, run:

>> /usr/core-admin/php/7.3/bin/php /var/webs/yourwebhosting.com/html/script.php

IMPORTANT NOTE: check security notes next.

4. Activating an environment that makes needed PHP version to be the default

In case you are using an environment that needs a particular php version to work, without full path, you can use:

>> core_admin_php 7.3

That will install required version in case it is needed (in this case 7.3) and will create an environment with provided php version will be the default:

>> php --version
PHP 5.6.40-0+deb8u2 (cli) (built: Mar 30 2019 20:26:13)
Copyright © 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright © 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright © 1999-2016, by Zend Technologies

>> core_admin_php 7.3
>> php --version
PHP 7.3.5 (cli) (built: May 20 2019 09:47:16) ( NTS )
Copyright © 1997-2018 The PHP Group
Zend Engine v3.3.5, Copyright © 1998-2018 Zend Technologies

>> core_admin_php 7.2
>> php --version
PHP 7.2.18 (cli) (built: May 20 2019 11:09:27) ( NTS )
Copyright © 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright © 1998-2018 Zend Technologies
with Zend OPcache v7.2.18, Copyright © 1999-2018, by Zend Technologies

>> core_admin_php 7.1
>> php --version
PHP 7.1.29 (cli) (built: May 20 2019 11:40:34) ( NTS )
Copyright © 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright © 1998-2018 Zend Technologies
with Zend OPcache v7.1.29, Copyright © 1999-2018, by Zend Technologies

The advantage of using core_admin_php is that it makes no change with symbolic links nor default php paths like /usr/bin/php or /usr/local/bin/php, which might cause problems for applications already deployed and using those paths but expecting different versions.

5. Security notes about running php scripts as root

In general it is not recommended to run php code as root and and at the same time have that code available at at hosting. That creates a security threat to your system:

  1. An attacker can modify such script, adding undesired code

  2. Now, it is only needed that cron to do the rest of the job (as it runs as root):

Core-Admin detects these cases and updates scripts permissions to assign owner and group to root so these scripts are only visible and usable by root.