Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t在共享主机上通过php运行bash脚本_Php_Bash_Shell - Fatal编程技术网

Can';t在共享主机上通过php运行bash脚本

Can';t在共享主机上通过php运行bash脚本,php,bash,shell,Php,Bash,Shell,我想要实现的目标: 每当我把一些东西推到我的存储库中时,我想在我的共享主机上更新我的git存储库(它保存着我的laravel应用程序)。为此,我在共享主机上有一个deploy.sh脚本,它可以提取我的存储库并从我的laravel应用程序中删除所有缓存 为了自动运行这个bash脚本,我还得到了一个php脚本,每当我将某个东西推到存储库中时,它就会被githubwebhook触发 我的问题: 我尝试在共享主机上用php脚本运行bash脚本,但这不起作用 我的尝试: 当我通过ssh在共享主机上手动执行

我想要实现的目标:

每当我把一些东西推到我的存储库中时,我想在我的共享主机上更新我的git存储库(它保存着我的laravel应用程序)。为此,我在共享主机上有一个deploy.sh脚本,它可以提取我的存储库并从我的laravel应用程序中删除所有缓存

为了自动运行这个bash脚本,我还得到了一个php脚本,每当我将某个东西推到存储库中时,它就会被githubwebhook触发

我的问题:

我尝试在共享主机上用php脚本运行bash脚本,但这不起作用

我的尝试:

当我通过ssh在共享主机上手动执行bash脚本时,它本身运行得非常好。但是它不会被我的php文件执行

PHP

<?php
function debug_to_console($data) {
    $output = $data;
    if (is_array($output))
        $output = implode(',', $output);

    echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}

$access_token = 'maxi9000';
$client_token = $_GET['token'];

if ($client_token !== $access_token)
{
    echo 'error 403';
}
else{
    
    $old = getcwd();
    echo '</br>'.$old;
    $file = $old.'/mhraschan.com/deploy.sh';
    echo '</br>'.$file;

    if(is_file($file)){
      echo '</br>Is file';
      if(!is_executable($file)) {
        chmod($file, 0755);
        echo '</br>file not executable - run chmod';
        if(is_executable($file)) echo "File is now executable";
      }
    //Try to run bash file
    $return = shell_exec('bash '. $file);
    echo '</br>Shell return: '.$return;
    debug_to_console('Shell return'.$return);
    }
    
}
?>

手动运行php脚本时浏览器的输出:


/data/web/e115665/html/test

/data/web/e115665/html/test/mhraschan.com/deploy.sh

是文件

文件不可执行-运行chmod

壳返回:


因此,我的文件不是可执行文件,并且运行chmod,但在这之后,它似乎仍然不是可执行文件,因为它不再运行该行:
if(is_executable($file))echo“file is now executable”

此外,我没有从shell_exec命令获得任何输出


有什么解决方案吗?

这几乎肯定是权限问题-请记住,您的web服务器不是以
根用户身份运行的;如果您使用的是Apache,那么您可能正在以
www-data
的身份运行—其他web服务器将使用其他用户。因此,您尝试
chmod
该文件失败,因为您没有更改该文件权限的权限。如果文件的权限不包括web服务器用户的“可执行文件”,则脚本将不会运行


检查
chmod
函数的返回值-我想您会发现它是
false
(意思是它失败了,)。

我返回了chmod。它显示
true
I还通过ssh手动更改了权限。这也行得通。仍然是不可执行的:(还有另一种可能性-你的bash脚本中可能有一个错误阻止它执行。我也可以手动运行我的bash脚本。我认为这不可能是错误