【问题标题】:error: cannot open .git/FETCH_HEAD: Permission denied when using php script for git pull错误:无法打开 .git/FETCH_HEAD:使用 php 脚本进行 git pull 时权限被拒绝
【发布时间】:2016-11-15 21:38:29
【问题描述】:

我为 git pull 疯狂的简单 php 脚本

<?php
$output = shell_exec("cd /var/www/domain.tld/html/test/ && git pull 2>&1");
echo "<pre>$output</pre>";

将它命名为 git.php 并给它 755

输出是

error: cannot open .git/FETCH_HEAD: Permission denied

但是当从终端尝试时

$ git pull
Already up-to-date.

我检查了目录所有权,我的用户很“精明”

drwxrwsr-x  4 savvy savvy     4096 Jul 13 05:40 test

和里面的文件

$ ls -la test/
total 20
drwxrwsr-x 4 savvy savvy 4096 Jul 13 05:40 .
drwxr-xr-x 7 savvy savvy 4096 Jul 13 05:38 ..
drwxrwsr-x 8 savvy savvy 4096 Jul 13 05:41 .git
-rw-rwSr-- 1 savvy savvy   19 Jul 13 05:39 test.php

.git

$ ls -la .git/
total 60
drwxrwsr-x 8 savvy savvy 4096 Jul 13 05:41 .
drwxrwsr-x 4 savvy savvy 4096 Jul 13 05:40 ..
drwxrwsr-x 2 savvy savvy 4096 Jul 13 05:39 branches
-rw-rwSr-- 1 savvy savvy  268 Jul 13 05:39 config
-rw-rwSr-- 1 savvy savvy   73 Jul 13 05:39 description
-rw-rwSr-- 1 savvy savvy   97 Jul 13 05:41 FETCH_HEAD
-rw-rwSr-- 1 savvy savvy   23 Jul 13 05:39 HEAD
drwxrwsr-x 2 savvy savvy 4096 Jul 13 05:39 hooks
-rw-rwSr-- 1 savvy savvy  104 Jul 13 05:41 index
drwxrwsr-x 2 savvy savvy 4096 Jul 13 05:39 info
drwxrwsr-x 3 savvy savvy 4096 Jul 13 05:39 logs
drwxrwsr-x 4 savvy savvy 4096 Jul 13 05:39 objects
-rw-rwSr-- 1 savvy savvy   41 Jul 13 05:40 ORIG_HEAD
-rw-rwSr-- 1 savvy savvy  107 Jul 13 05:39 packed-refs
drwxrwsr-x 5 savvy savvy 4096 Jul 13 05:39 refs

~/.ssh

drwx------ 2 savvy savvy 4096 Jul 13 04:56 .ssh

里面

$ ls -la .ssh/
total 36
drwx------ 2 savvy savvy 4096 Jul 13 04:56 .
drwxr-xr-x 4 savvy savvy 4096 Jul 12 08:34 ..
-rw-r--r-- 1 savvy savvy  398 Jul 12 05:46 authorized_keys
-rw-rw-r-- 1 savvy savvy   59 Jul 11 10:10 config
-rw------- 1 savvy savvy 1679 Jul 11 07:44 id_rsa
-rw-rw-r-- 1 savvy savvy  398 Jul 12 05:21 id_rsa.pub
-rw-r--r-- 1 savvy savvy 1768 Jul 13 05:08 known_hosts

【问题讨论】:

  • 你是如何运行你的 php 脚本的?作为哪个用户?
  • 从 url 调用它:domain.tld/git.php 并用精明的用户创建 git.php
  • 该网址由网络服务器提供服务,该服务器以哪个用户身份运行?您可能需要将网络服务器的用户添加到您的组中。
  • $ ps aux|grep nginx|grep -v grep root 12433 0.0 0.2 85892 1400 ? Ss Jul12 0:00 nginx: master process /usr/sbin/nginx www-data 12435 0.0 0.3 86188 1620 ? S Jul12 0:09 nginx: worker process www-data 12436 0.0 0.4 86556 2104 ? S Jul12 0:01 nginx: worker process www-data 12437 0.0 0.3 86188 1620 ? S Jul12 0:09 nginx: worker process www-data 12438 0.0 0.3 86532 1868 ? S Jul12 0:08 nginx: worker process
  • 我将 www-data 用户添加到 savvy 组,结果相同

标签: php linux git nginx server


【解决方案1】:

我通过以下方式解决了这个问题

1- 复制 /home/savvy/.ssh 到 /var/www/

# cp -R /home/savvy/.ssh /var/www/

2- 将 /var/www 目录提供给 www-data 用户

# chown -R www-data:www-data /var/www/

3- 以 www-data 用户身份登录

$ su
Password: 
# su -s /bin/bash www-data

4- 将 repo 克隆到我的目录 /var/www/domain.tld/html/test/

5- 调用我已经制作的 git.php 文件 domain.tld/git.php
结果

From bitbucket.org:repo/test
8e9f67c..185cd9e  master     -> origin/master
Updating 8e9f67c..185cd9e
Fast-forward
test.php | 1 +
1 file changed, 1 insertion(+)

【讨论】:

  • 在我的设置中,我有一个具有组写访问权限的 git 存储库,解决方案是将 www-data 添加到该组:sudo gpasswd -a www-data mygitgroup。在systemctl 中重新启动apache2 后,调用PHP 脚本现在将成功从Git 获取。
猜你喜欢
  • 2015-11-29
  • 2012-10-23
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
  • 2019-10-23
  • 2020-12-30
  • 1970-01-01
  • 2021-06-12
相关资源
最近更新 更多