Pre-flight Check:
- These instructions are intended specifically for cPanel-based servers running WHM prior to version 58.
- Command line access via SSH may be necessary to follow the examples.
- The server will need to have EasyApache 4 active.
- PHP 7 will be necessary to follow the provided examples.
Option #1: Directly Call the PHP Binary
The most basic option to workaround these issues is to directly call the PHP binary you need before executing the script. While this can be effective and will work as expected most of the time, certain utilities will not work when using this method.You may want to attempt this method first and move on to Option #2 if this does not work as expected; generally though, this should work for executing basic PHP code.
/opt/cpanel/ea-php70/root/usr/bin/php someScript.php
The above command will specifically call the 7.0 version of PHP and will execute the someScript.php file within that version of PHP. If they are also installed on the server, the alternative PHP versions can be found at:
- /opt/cpanel/ea-php55/root/usr/bin/php
- /opt/cpanel/ea-php56/root/usr/bin/php
- /opt/cpanel/ea-php70/root/usr/bin/php
Option #2: Use scl to Specify PHP Versions
As mentioned, certain PHP utilities are not provided as basic PHP files and are stored in the PHAR format. These utilities will not execute properly with the above method for specifying the PHP version. For these types of utilities you will need to specify the PHP version using a command called `scl`. This command is a utility that allows running software in an environment packaged as a ‘Software Collection’. To be brief a ‘Software Collection’ is a way of defining an alternate location to a certain software. Fortunately, WHM provides predefined collections for the various versions of PHP supported by EasyApache 4. The syntax of this command is as follows:scl enable {SoftwareCollection} '{CommandToRun}'
Where you replace {SoftwareCollection} with the needed collection [ea-php55, ea-php56, or ea-php70], and you replace {CommandToRun} with the command, utility, or script you need to run.
A good way to highlight this is to review the difference in the following outputs:
root@noms [~]# php -v
PHP 5.6.25 (cli) (built: Aug 25 2016 17:00:38)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with the ionCube PHP Loader v4.7.5, Copyright (c) 2002-2014, by ionCube Ltd., and
with Zend Guard Loader v3.3, Copyright (c) 1998-2014, by Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
root@noms [~]# scl enable ea-php70 'php -v'
PHP 7.0.10 (cli) (built: Aug 25 2016 18:04:55) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.10, Copyright (c) 1999-2016, by Zend Technologies
As you can see when calling PHP by itself we get the default version, but when using scl we explicitly called the PHP 7.0 collection and get results to match.
Example #1: Executing a utility [Composer]:
root@noms [~]# scl enable ea-php70 'composer --version'
Composer version 1.2.0 2016-07-19 01:28:52
Example #2: Running a PHP script:
root@noms [~]# scl enable ea-php70 './someScript.php'
With these key tips and tricks you should now be equipped with the necessary tools to run CLI utilities on a server using EasyApache 4. If you have any questions or are not comfortable making these changes yourself, please feel free to contact Heroic Support®.