使用PHP无法将Web服务器上的``提交''更改提交给Github存储库

时间:2018-10-06 15:09:21

标签: php git github

我正在尝试编写一个PHP小脚本,该脚本可以在Web服务器上发现对本地git repo的所有更改,并将其推送到我的(私有)Github repo。使用Atom从Github存储库中推送和拉取效果很好,使用Webhook将更改推到Web服务器效果很好,通过命令行在Web服务器上推和拉更新效果也很好,我的问题是试图在Web服务器到使用PHP的Github存储库。你怎么做到的?

如果必须更改,请手动在服务器上添加甚至删除整个模板,我可以提交这些更改,并使用命令行将其推送到Github,就像这样:

git add --all
git commit -m "from server"
git push -u origin master

但是,当我尝试使用PHP脚本执行此操作时,它永远不会起作用,并且我也不会收到错误消息(我什至尝试暂停):

$output = `git add --all`;
echo $output;
sleep(1);

$output = `git commit -m "from server"`;
echo $output;
sleep(3);

$output = `git push -u origin master`;
echo $output;
sleep(3);

如果我运行诸如' git --version ',' git config --list '或' git status '之类的简单程序,这些脚本可以很好地工作,所以我很茫然。

4 个答案:

答案 0 :(得分:3)

您说:

  

但是当我尝试使用PHP脚本执行此操作时,它永远无法工作,我得到了   没有错误消息(我什至尝试暂停):

$output = `git add --all`;
echo $output;
sleep(1);

$output = `git commit -m "from server"`;
echo $output;
sleep(3);

$output = `git push -u origin master`;
echo $output;
sleep(3);
     

如果我运行类似'git --version','git config --list'或   'git status'可以在这些脚本中完美运行,所以我很茫然。

似乎您在这里没有写权限。

您可以轻松地运行以下命令来检查git repo的权限,登录用户和Web服务器的所有者。

运行命令whoamiid来标识登录用户。

$ whoami
gasolwu
$ id
uid=501(gasolwu) gid=20(staff) groups=20(staff),501(access_bpf),12(everyone),61(localaccounts),79(_appserverusr),80(admin),81(_appserveradm),98(_lpadmin),33(_appstore),100(_lpoperator),204(_developer),250(_analyticsusers),395(com.apple.access_ftp),398(com.apple.access_screensharing),399(com.apple.access_ssh),701(com.apple.sharepoint.group.1)

列出目录以检查给定路径(git repo)的所有者和权限

$ ls -al /path/to/repo
total 16
drwxr-xr-x   5 gasolwu  staff  160 Oct 15 21:50 ./
drwxr-xr-x   5 gasolwu  staff  160 Oct 15 21:48 ../
drwxr-xr-x  13 gasolwu  staff  416 Oct 15 21:53 .git/
-rw-r--r--   1 gasolwu  staff  196 Oct 15 21:50 git.php*
-rw-r--r--   1 gasolwu  staff   79 Oct 15 21:49 git.sh*

显示进程状态以检查运行Web服务器的用户

$ ps auxwww | grep 'httpd\|nginx\|apache'
_www              1139   0.0  0.0  4342760   3692   ??  S     9:51PM   0:00.01 /usr/sbin/httpd -D FOREGROUND
_www              1138   0.0  0.0  4351976   3692   ??  S     9:51PM   0:00.02 /usr/sbin/httpd -D FOREGROUND
_www              1137   0.0  0.0  4317160   1988   ??  S     9:51PM   0:00.01 /usr/sbin/httpd -D FOREGROUND
_www              1129   0.0  0.0  4334568   2300   ??  S     9:51PM   0:00.01 /usr/sbin/httpd -D FOREGROUND
root              1119   0.0  0.1  4316156  11772   ??  Ss    9:51PM   0:00.51 /usr/sbin/httpd -D FOREGROUND
gasolwu           1465   0.0  0.0  4268036    824 s000  S+   10:19PM   0:00.00 grep --color=auto -d skip httpd\|nginx\|apache

或如果您使用PHP-FPM运行Web服务器,请检查php-fpm的用户

$ ps auxwww | grep php-fpm
gasolwu           1761   0.0  0.0  4268036    812 s000  S+   10:33PM   0:00.00 grep --color=auto -d skip php-fpm
nobody            1737   0.0  0.0  4323216    724   ??  S    10:33PM   0:00.00 php-fpm
nobody            1736   0.0  0.0  4323216    732   ??  S    10:33PM   0:00.00 php-fpm
root              1735   0.0  0.0  4323216    920   ??  Ss   10:33PM   0:00.00 php-fpm

如您所见,这里也有权限问题,.git目录只能由用户gasolwu写入,而不能由Web用户_www写入。因此,当您通过Web服务器通过php脚本运行git操作时。没有写权限就无法进行git操作(添加/提交)。

shell_exec(与backtick运算符相同)函数仅返回stdout,发生错误时此处为空,stderr将根据您的环境重定向到错误日志, 您将在Apache或PHP中获得类似的错误消息日志。

$ cat /var/log/apache2/error_log
Mon Oct 15 21:51:06.734474 2018] [mpm_prefork:notice] [pid 1119] AH00163: Apache/2.4.34 (Unix) PHP/7.1.19 configured -- resuming normal operations
[Mon Oct 15 21:51:06.734572 2018] [core:notice] [pid 1119] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
fatal: Unable to create '/path/to/repo/.git/index.lock': Permission denied
fatal: Unable to create '/path/to/repo/.git/index.lock': Permission denied
error: could not lock config file .git/config: Permission denied
error: Unable to write upstream branch configuration
hint:
hint: After fixing the error cause you may try to fix up
hint: the remote tracking information by invoking
hint: "git branch --set-upstream-to=origin/master".
error: update_ref failed for ref 'refs/remotes/origin/master': cannot lock ref 'refs/remotes/origin/master': Unable to create '/path/to/repo/.git/refs/remotes/origin/master.lock': Permission denied
Everything up-to-date
error: remote unpack failed: unable to create temporary object directory
To /tmp/git
 ! [remote rejected] master -> master (unpacker error)
error: failed to push some refs to '/tmp/git'

让我们通过向正确的用户(此处为_www)授予写权限来解决此问题。

chown -R _www /path/to/repo

此后,您可以向http://example.com/git.php发送请求以添加文件,并提交“来自服务器”消息,然后将其推送到GitHub。

注意: 没有身份验证的这种方法存在一些安全问题。

答案 1 :(得分:2)

使用php运行脚本时,该脚本由用户www-data运行(默认情况下)。当您连接到git仓库时,您需要进行身份验证。最有可能使用ssh键完成此操作。因此,您需要使用ssh密钥授权用户www-data,以允许他访问远程存储库。

步骤如此。

  1. 生成密钥
  2. 将密钥添加到远程存储库
  3. 为用户www-data将密钥添加到本地ssh代理
  4. 检查运行命令的环境
  5. 享受

有用的链接:https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/

还有一个选项,可通过https使用身份验证并跳过放置凭据。您可以看到更多here

此外,还有library会执行此类操作,您也可以对其进行检查

答案 2 :(得分:0)

问题出在认证上。第一个解决方案是像罗伯特所说的那样做。但我认为无需重新发明轮子,请尝试查看此软件包:

https://github.com/kbjr/Git.php

一切都已经在那里。

答案 3 :(得分:0)

您实际上只是在回显字符串,而不是在运行它们。

您可以使用echo()exec()代替shell_exec()

http://php.net/manual/en/function.exec.php

http://php.net/manual/en/function.shell-exec.php

以下是允许您在服务器上执行程序的PHP命令:

http://php.net/manual/en/ref.exec.php

相关问题