Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.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
如何通过http设置git?_Git_Apache_Http_Git Http Backend - Fatal编程技术网

如何通过http设置git?

如何通过http设置git?,git,apache,http,git-http-backend,Git,Apache,Http,Git Http Backend,我需要使用git over http(智能http)设置git服务器,但是在线可用的资源非常混乱,混合在其他apache配置中,缺少详细信息或不够明确 我自己回答这个问题是基于我在可用资源中发现的不足。首先,有必要了解git在http上有两个组件:git和apache。这两者通过一个名为git http后端的脚本连接。挑战在于配置这两个组件之间的接口,以便apache转发到git的http请求 注意:安全性不在本指南的范围内 首先,使用发行版的包管理器安装git和apache2 添加apache

我需要使用git over http(智能http)设置git服务器,但是在线可用的资源非常混乱,混合在其他apache配置中,缺少详细信息或不够明确


我自己回答这个问题是基于我在可用资源中发现的不足。

首先,有必要了解git在http上有两个组件:git和apache。这两者通过一个名为git http后端的脚本连接。挑战在于配置这两个组件之间的接口,以便apache转发到git的http请求

注意:安全性不在本指南的范围内

  • 首先,使用发行版的包管理器安装git和apache2

  • 添加apache启用git over http所需的模块。它们是cgi、alias和env

  • $a2enmod cgi别名env
    
  • 将以下内容复制到
    /etc/apache2/httpd.conf
    (不删除其中包含的任何其他内容)
  • 
    SetEnv GIT_项目_根/data/GIT
    SetEnv GIT_HTTP_EXPORT_ALL
    SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER
    脚本别名匹配\
    “(?x)^/(.*/(总目)\
    信息/参考文献|\
    对象/(信息/[^/]+|\
    [0-9a-f]{2}/[0-9a-f]{38}|\
    pack/pack-[0-9a-f]{40}\(pack | idx))|\
    git-(上传|接收)-打包)$”\
    “/usr/lib/git/git http backend/$1”
    别名/git/data/git
    选项+执行CGI-多视图+符号链接所有者匹配
    不允许超限
    命令允许,拒绝
    通融
    
  • 现在,将出现的两个
    /data/git
    替换为服务器上git repo的父目录(如果您还没有任何repo,请不要担心,只使用您打算放置它/它们的目录)
  • 还可以将
    /usr/lib/git/git-http-backend
    替换为系统上git-http-backend的位置,可以使用
    $find/-name-git-http-backend

    可能是在您的系统上,重定向远程用户实际上覆盖了有效的远程用户。如果此设置完成后不起作用,请尝试删除该行

    根据消息来源,可能需要将目录标记中的最后两行替换为Apache2.4及以上版本的
    Require-all-grated

  • 重新启动apache服务器:
    $apache2ctl-k
  • 现在apache服务器已经设置好了,但是我们还没有完成,设置repo的一些重要部分将影响这个设置是否有效

  • 建立回购协议:
  • $mkdir myrepo.git
    $cd myrepo.git
    $git init--bare--shared
    $cp hooks/post-update.sample hooks/post-update
    $git更新服务器信息
    $chown-R wwwrun:www
    
    在这里,重要的是要理解最后一行将回购的所有者更改为apache2用户。此用户在您的系统上可能不同。要查找apache用户,请执行
    $ps aux | egrep'(apache | httpd)
    。然后,要查找用户的组名,请执行
    $id user name
    。在我的系统上,用户是wwwrun,组是www。相应地更换

  • 使用回购协议
  • 为了使用回购协议,您需要知道url。对于此设置,url为

    注意:https将不起作用

    从客户端访问回购协议时,只需将其添加为远程:

    $git远程添加源http://server.domain/myrepo.git
    

    然后,您可以像其他任何git回购一样与它进行交互。

    使用不同的访问权限将多个项目进行交互。

    有白云母,但很复杂。。。 一个简单的解决方案是在apache2 conf中创建宏,如下所示:

    ## Git root
    
    SetEnv GIT_PROJECT_ROOT /opt/gitroot
    SetEnv GIT_HTTP_EXPORT_ALL
    SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER
    SetEnv GITWEB_CONFIG /etc/gitweb.conf
    
    ##  SMART Http
    
    ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
    
    <Directory "/usr/libexec/git-core*">
       Options +ExecCGI +Indexes
       Order allow,deny
       Allow from all
       Require all granted
    </Directory>
    
    <Macro Project $repository $developers $users>
        <LocationMatch "^/git/$repository.*$">
            AuthType Basic
            AuthName "Git Access"
            AuthUserFile /opt/basic_auth
            Require $developers $users 
        </LocationMatch>
        <LocationMatch "^/git/$repository/git-receive-pack$">
            AuthType Basic
            AuthName "Git Access"
            AuthUserFile /opt/basic_auth
            Require  $developers
        </LocationMatch>
     </Macro>
    
     IncludeOptional /opt/git_access.conf
    
    因此,git项目test1将具有admin和john的读/写访问权限,以及mike的只读访问权限

    项目test2将具有所有经过身份验证的用户的读取权限

    例如,git_access.conf可以由您的工具从项目和用户的数据库中生成,并通过“服务httpd reload”重新加载

    对于gitweb读取访问的相同配置,请在/etc/gitweb.conf中添加:

    $export_auth_hook = sub {
            my $repo = shift;
            my $user = $cgi->remote_user;
            if($repo =~ s/\/opt\/gitroot\///) {
               open FILE, '/opt/git_access'; 
               while(<FILE>) {
                   if ($_ =~ m/Use Project $repo \"(.*)\" \"(.*)\"/)
                    {
                        my $users = $1 . ' ' . $2;
                        $users =~ s/all granted/$user/;
                        $users =~ s/user//;
                        if ( $users =~ m/$user/ ) {
                            return 1;
                        }
                    }              
                }
            }
            return 0;
    };
    
    $export\u auth\u hook=sub{
    我的$repo=移位;
    my$user=$cgi->远程用户;
    如果($repo=~s/\/opt\/gitroot\//){
    打开文件“/opt/git_access”;
    while(){
    如果($\=~m/使用项目$repo\”(.*)\“\”(.*)\”/)
    {
    my$users=$1.'.$2;
    $users=~s/all-grated/$user/;
    $users=~s/user/;
    如果($users=~m/$user/){
    返回1;
    }
    }              
    }
    }
    返回0;
    };
    
    这个小gitweb钩子隐藏(返回0)当前用户没有读取权限的git存储库

    在apache2配置中,经典的gitweb配置:

    ## Gitweb  
    
    Alias /gitweb /var/www/git
    
    <Directory /var/www/git>
        AddHandler cgi-script .cgi
        DirectoryIndex gitweb.cgi
        Options +ExecCGI +Indexes +FollowSymlinks 
        AllowOverride None    
        AuthType Basic
        AuthName "Git Access"
        AuthUserFile /opt/basic_auth
        Require valid-user
    </Directory>
    
    ##Gitweb
    别名/gitweb/var/www/git
    AddHandler cgi script.cgi
    目录索引gitweb.cgi
    选项+ExecCGI+索引+FollowSymlinks
    不允许超限
    AuthType Basic
    AuthName“Git访问”
    AuthUserFile/opt/basic\u auth
    需要有效用户
    

    这是我的配置。

    非常感谢!在我的Ubuntu14.04和Apache2.4.7中,它对我起了作用!一些详细信息,我的apache用户是www数据,在步骤6中,命令“chown…”末尾缺少“.”。git位于我的ubuntu文件夹“/usr/lib/git-core”中,我尝试了同样的方法,但似乎存在一些权限问题。当我尝试ClonewWorks时,我得到403(无法访问),但它不会提示任何身份验证。你只是在Apache中为它设置了基本身份验证吗?我很久以前就这样做了,结果得到了一个付费的解决方案,所以我无法回答你的问题。如果你在CentOS做这件事,而且似乎没有任何进展,希望你能找到答案
    ## Gitweb  
    
    Alias /gitweb /var/www/git
    
    <Directory /var/www/git>
        AddHandler cgi-script .cgi
        DirectoryIndex gitweb.cgi
        Options +ExecCGI +Indexes +FollowSymlinks 
        AllowOverride None    
        AuthType Basic
        AuthName "Git Access"
        AuthUserFile /opt/basic_auth
        Require valid-user
    </Directory>