{"id":4924,"date":"2025-04-18T04:16:33","date_gmt":"2025-04-18T03:16:33","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=4924"},"modified":"2025-04-18T06:43:18","modified_gmt":"2025-04-18T05:43:18","slug":"path-to-php","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/files\/path-to-php","title":{"rendered":"Path to php"},"content":{"rendered":"\n<p>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&#8217;t been stored on your server (and it often hasn&#8217;t)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Simple approach<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$PathToPhp = PHP_BINARY;<\/code><\/pre>\n\n\n\n<p>However, if $PathToPhp returns a path to a file name of &#8216;php-fpm&#8217; then this is no use, because php-fpm is a manager for php, not php itself. It can&#8217;t run php scripts (if you try you&#8217;ll get a response listing &#8220;Usage: php-fpm [options]&#8221;)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The harder approach<\/h4>\n\n\n\n<p>phpinfo(); wont help you, it doesn&#8217;t provide the path to php in it. It can sometimes offer clues, but its not guaranteed.<\/p>\n\n\n\n<p>To find it, you&#8217;ve gotta use a SSH terminal if your php binary is somewhere nonstandard you can&#8217;t guess. This it the command you need to use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>which php<\/code><\/pre>\n\n\n\n<p>This will give you the path.<\/p>\n\n\n\n<p>Unfortunately you can&#8217;t usually call it using shell_exec() from your php code, because the &#8220;which&#8221; command is typically blocked for security.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Verifying your path to php is good<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/*******************************************\n\/\/*******************************************\n\/\/********** IS PATH TO PHP VALID? **********\n\/\/*******************************************\n\/\/*******************************************\n\/\/Returns True, or error message string\n\/*\n  $PathToPhp = '\/usr\/bin\/php';\n  $Result = IsPathToPhpValid($PathToPhp);\n  if ($Result !== True)\n    echo \"$Result&lt;br>\";\n*\/\nfunction IsPathToPhpValid($PathToPhp)\n{\n  if(!function_exists('shell_exec'))\n    return('shell_exec() is not available. Please check your PHP configuration.');\n\n  $EscapedPathToPhp = escapeshellcmd($PathToPhp);\n  $Command = \"$EscapedPathToPhp -r \\\"echo 'php-ok';\\\"\";     \/\/We'll run: php -r \"echo 'php-ok';\"\n  $Output = trim(shell_exec($Command . \" 2>&amp;1\"));\n  \n  if ($Output === 'php-ok')\n      return(True);\n  else\n    return(\"Path is NOT valid or failed to run. Error: $Output\");\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;t been stored on your server (and it often hasn&#8217;t) Simple approach However, if $PathToPhp returns a path to a file name of &#8216;php-fpm&#8217; then this is no [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58,372],"tags":[],"class_list":["post-4924","post","type-post","status-publish","format-standard","hentry","category-files","category-php-specific"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=4924"}],"version-history":[{"count":8,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4924\/revisions"}],"predecessor-version":[{"id":4938,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/4924\/revisions\/4938"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=4924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=4924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=4924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}