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:
- 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 thepermission denied
error. - I tried to move the
wp
bin tousr/bin
instead. No effect here either. - Out of desperation I even tried to give
wp
777 permissions andapache
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.