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");
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *