When you want to run a php process, for example run a php file script using shell_exec(), you need the path to php if the php path hasn’t been stored on your server (and it often hasn’t)
Simple approach
$PathToPhp = PHP_BINARY;
However, if $PathToPhp returns a path to a file name of ‘php-fpm’ then this is no use, because php-fpm is a manager for php, not php itself. It can’t run php scripts (if you try you’ll get a response listing “Usage: php-fpm [options]”)
The harder approach
phpinfo(); wont help you, it doesn’t provide the path to php in it. It can sometimes offer clues, but its not guaranteed.
To find it, you’ve gotta use a SSH terminal if your php binary is somewhere nonstandard you can’t guess. This it the command you need to use:
which php
This will give you the path.
Unfortunately you can’t usually call it using shell_exec() from your php code, because the “which” command is typically blocked for security.
Verifying your path to php is good
//*******************************************
//*******************************************
//********** IS PATH TO PHP VALID? **********
//*******************************************
//*******************************************
//Returns True, or error message string
/*
$PathToPhp = '/usr/bin/php';
$Result = IsPathToPhpValid($PathToPhp);
if ($Result !== True)
echo "$Result<br>";
*/
function IsPathToPhpValid($PathToPhp)
{
if(!function_exists('shell_exec'))
return('shell_exec() is not available. Please check your PHP configuration.');
$EscapedPathToPhp = escapeshellcmd($PathToPhp);
$Command = "$EscapedPathToPhp -r \"echo 'php-ok';\""; //We'll run: php -r "echo 'php-ok';"
$Output = trim(shell_exec($Command . " 2>&1"));
if ($Output === 'php-ok')
return(True);
else
return("Path is NOT valid or failed to run. Error: $Output");
}