';提交';使用PHP将Web服务器更改为Github repo无效
我正在尝试编写一个小PHP脚本,可以在我的web服务器上发现对本地git repo的所有更改,并将它们推送到我的(私有)Github repo。使用Atom从Github repo推送和拉送工作非常完美,使用webhook将更改推送到web服务器工作非常完美,通过命令行在web服务器上推送和拉送更新工作非常完美,我的问题是尝试使用PHP将web服务器上的更新提交和推送到Github repo。你是怎么做到的 如果我必须手动更改、添加甚至删除服务器上的整个模板,我可以提交这些更改,并使用以下命令行将其推送到Github:没问题:';提交';使用PHP将Web服务器更改为Github repo无效,php,git,github,Php,Git,Github,我正在尝试编写一个小PHP脚本,可以在我的web服务器上发现对本地git repo的所有更改,并将它们推送到我的(私有)Github repo。使用Atom从Github repo推送和拉送工作非常完美,使用webhook将更改推送到web服务器工作非常完美,通过命令行在web服务器上推送和拉送更新工作非常完美,我的问题是尝试使用PHP将web服务器上的更新提交和推送到Github repo。你是怎么做到的 如果我必须手动更改、添加甚至删除服务器上的整个模板,我可以提交这些更改,并使用以下命令行
git add --all
git commit -m "from server"
git push -u origin master
但是,当我尝试使用PHP脚本执行此操作时,它永远不会工作,并且我没有收到任何错误消息(我甚至尝试暂停):
如果我运行像“git--version”、“git-config--list”或“git-status”这样的简单脚本,它就可以从这些脚本中完美地运行,所以我很茫然。使用php运行脚本时,它是由用户www data(默认情况下)运行的。当您连接到git存储库时,需要进行身份验证。很可能是使用ssh密钥完成的。因此,您需要使用ssh密钥授权用户www数据,以允许他访问远程存储库 因此,这些步骤很简单
此外,还有一种方法可以做这些事情,您也可以检查它问题在于身份验证。第一个解决办法是照罗伯特说的做。但我认为没有必要重新发明轮子,试着看看这个包:
一切都已经准备好了。实际上,你只是在重复字符串,而不是运行它们 您可以使用
exec()
,shell\u exec()
,而不是echo()
:
以下是允许您在服务器上执行程序的PHP命令:
你说:
但是,当我尝试使用PHP脚本来实现这一点时,它从来都不起作用,我得到了
没有错误消息(我甚至尝试暂停):
如果我运行像“git--version”、“git config--list”或
“git status”它在这些脚本中工作得非常完美,所以我不知所措
看来你在这里没有写权限
您可以轻松地运行以下命令来检查git repo、登录用户和web服务器所有者的权限
运行命令whoami
或id
识别登录用户
$ 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)
显示进程状态以检查运行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用户\u www
编写。所以当您通过web服务器通过php脚本运行git操作时。没有写入权限,它无法执行git操作(添加/提交)
(与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'
让我们通过向正确的用户授予写入权限来修复它
之后,您可以向发送请求以添加文件,通过消息“fromserver”提交,然后将它们推送到GitHub
警告:
此方法在没有身份验证的情况下存在一些安全问题。
exec$output代码>?请提供重现问题的分步说明。上面没有显示足够的信息,甚至没有完整的PHP文件!考虑到A。我有一个类似的问题,并且只能通过创建简单的shell脚本来解决,这些命令必须提交给Git拉/推/提交。例如,对于git pull origin master
我实际上有一个脚本gitpull.sh
,作为可执行文件,带有#/bin/bash
和/usr/bin/git将原始主机拉入其中。我没有深入调查为什么一个有效,另一个无效。对于这两种情况,您必须为web用户设置所有用户--config
、密钥、权限等(www-data、nobody、nginx--取决于您的操作系统)
$ 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
$ 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
$ 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'
chown -R _www /path/to/repo