【问题标题】:shell_exec and git pullshell_exec 和 git pull
【发布时间】:2011-07-05 20:50:07
【问题描述】:

我希望有人能提供帮助,我有一个 PHP 页面,它使用 shell_exec 压缩目录并运行 git pull 以降低最近的存储库更改。

$op = shell_exec("cd /home/user/git/$repo/$dir/; zip -r /home/user/archives/$dir.$datestamp.zip $dir; cd /home/user/git/$repo/$dir/; git pull");

拉链工作正常。如果我将git pull 更改为例如git loggit status - 在我的shell_exec 中,这也有效,我可以看到日志文件。

只是似乎不喜欢 git pull。

我看到了另一个类似的帖子,但不确定它是如何实现的 >> Shell_exec with git pull?

【问题讨论】:

  • git pull 的输出是什么?您是否尝试过git pull origin master 或您的分支/遥控器的名称?
  • 您好.. 很遗憾,git pull 没有返回任何内容,git pull origin master 也没有返回 - 很奇怪。
  • 您提到的另一个问题的答案是存在权限问题,这似乎是合理的,因为 git loggit status 不需要写入存储库,而 @987654335 @ 会。要对此进行调查,我会将您的git pull 更改为touch /tmp/whatever,然后使用ls -l /tmp/whatever 查找拥有该文件的用户和组——这将告诉您shell_exec 命令正在以哪个用户身份运行。如果您不能以该用户身份写入存储库目录,这将解释为什么 git pull 失败...
  • 啊,它正试图通过apache 用户进行拉取,不知道我该如何改变它,改为使用特定用户......认为这就是问题所在!再次欢呼
  • 尝试从 php 运行 git pull 时的一系列注意事项 ... jondavidjohn.com/b/7m

标签: php git shell-exec git-pull


【解决方案1】:

从您在 cmets 中的描述看来,问题在于您的 apache 用户无法写入存储库,这在您使用 git pull 时显然是必需的。您有两种行动方案:

  1. 设置 Apache 以以其他用户身份运行脚本(例如,使用 suEXECon a VirtualHost or via userdir
  2. 更改存储库的权限,以便apache 用户可以写入它

您应该仔细考虑这两种选择的安全隐患,但第二种选择可能是最简单的。如果您还没有这样的组,您可以使用以下命令创建它:

addgroup gitwriters

...然后将您自己和 Apache 用户添加到该组:

adduser [yourusername] gitwriters
adduser apache gitwriters

然后您可以按照another question 中的说明更改存储库的权限。重申一些细微的变化:

# Recursively, set the group ownership of every file and directory of your repository:
chgrp -R gitwriters /path/to/your/repo

# Recursively, make every file and directory of your repository readable and writable
# by the group:
chmod -R g+rw /path/to/your/repo

# Recursively, set the setgid of every directory in the repository.  The setgid bit
# on directories means that files created in the directory will have the same group
# ownership as the directory.  
find /path/to/your/repo -type d -print0 | xargs -0 chmod g+s

那么希望你的git pull 应该可以工作。

【讨论】:

  • 干杯马克在这方面的帮助。我已经按照你说的做了,目前我有这个:ip-xx-xxx-xx-xxx ~: id apache uid=48(apache) gid=48(apache) groups=48(apache),10(wheel),501(gitwriters) 但是在执行git pull 时,它仍然在 apache 组下作为 apache 运行。有没有办法改变默认组 - 如果这有意义的话!抱歉,对这一切都很陌生,再次欢呼。
  • @williamsowen:是的,如果您遵循方法 2,您不会更改运行 PHP 代码的用户,您只是在创建该用户(以及您通常使用的用户)用户帐户)能够写入存储库。你肯定需要让它以不同的用户身份运行吗?如果是这样,您需要研究方法 1。
猜你喜欢
  • 2011-02-01
  • 2018-08-21
  • 1970-01-01
  • 2016-01-12
  • 2019-12-12
  • 2017-11-23
  • 2012-09-10
  • 1970-01-01
  • 2014-10-15
相关资源
最近更新 更多