2

I am currently trying to run a wp cli command from a php script. For those unfamiliar with WP-CLI its a cool command line interface for Wordpress. Below is my following script:

<?php
// lets create a basic test
$cmd = '/usr/local/bin/wp core download';

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("file", "path-to-error-log/error-output.txt", "a") // stderr is a file to   write to
);

$process = proc_open($cmd, $descriptorspec, $pipes);

if (is_resource($process)) {
   // $pipes now looks like this:
   // 0 => writeable handle connected to child stdin
   // 1 => readable handle connected to child stdout

   $output = stream_get_contents($pipes[1]);
   fclose($pipes[1]);

   // It is important that you close any pipes before calling
   // proc_close in order to avoid a deadlock
   $return_value = proc_close($process);

   echo $output;

}


?>

Inside error-output.txt i receive the following error:

sh: /usr/local/bin/wp: Permission denied

From what I can gather this is a permission issue, where apache (the current user executing my php script) is unable to for some reason execute the wp bin file.

Out of curiosity I have done the following:

  1. Changed the command i want to run to wp --info which outputs information about wp-cli and does not require writing to another directory, i assumed this could have been an issue but i was proved wrong since i still get the permission denied error.
  2. I tried to move the wp bin to usr/bin instead. No effect here either.
  3. Out of desperation I even tried to give wp 777 permissions and apache was still unable to execute.

I realise this is more of a server setup/permission issue (possibly) but I felt this would be the best place to ask.

Scopey
  • 6,269
  • 1
  • 22
  • 34
  • You (almost) never need to `chmod 777`, to make the file readable & executable for everyone use `chmod 555`; [overview of UNIX permissions](http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/permissions.html) (it's the same for all UNIX-y systems) – Martin Tournoij Aug 08 '14 at 09:42
  • Judging by this I don't think the issue is a permission issue anymore on the `wp` bin since I have tried 555 just now and received the same error, I expected this since 777 didn't work either. – user3355366 Aug 08 '14 at 09:49
  • Yes, it was just a generic comment. – Martin Tournoij Aug 08 '14 at 09:50
  • Have you tried to: * Execute other system commands from your PHP script? * Moved the WP-CLI Phar file to the same directory as your script, and executed there? Could also be one of these: * http://stackoverflow.com/questions/12232427/proc-open-fails-with-permission-denied * http://stackoverflow.com/questions/5673740/php-or-apache-exec-popen-system-and-proc-open-commands-do-not-execute-any-com – Daniel Bachhuber Aug 08 '14 at 14:00
  • @DanielBachhuber Yes I am able to execute git --help, it will output the contents to the page. I have also followed some of the recommendations inside the 2 links. I did not change SE Linux to permissive as I'm unsure what consequences it will hold on my server. I have also moved wp, into my public_html, just above public_html, changed its permissions from root to apache, gave it 777 and then back to the recommend 555, and i still get the same error. – user3355366 Aug 18 '14 at 07:49

0 Answers0