【问题标题】:How to set up git over http?如何通过 http 设置 git?
【发布时间】:2014-12-31 07:56:06
【问题描述】:

我需要用 git-over-http(智能 http)搭建一个 git 服务器,但是网上可用的资源很乱,混入了其他 apache 配置,缺少细节或者不够明确。

我根据我发现可用资源中的不足来回答这个问题。

【问题讨论】:

    标签: git apache http git-http-backend


    【解决方案1】:

    首先有必要了解 git-over-http 有 2 个组件:git 和 apache。这两者是通过一个名为 git-http-backend 的脚本连接起来的。挑战在于配置这两个组件之间的接口,让对git的http请求由apache转发。

    注意:安全性超出了本指南的范围。

    1. 首先使用发行版的包管理器安装 git 和 apache2。

    2. 添加 apache 启用 git-over-http 所需的模块。这些是 cgi、别名和 env

    $ a2enmod cgi alias env
    
    1. 将以下内容复制到/etc/apache2/httpd.conf(不删除其中包含的任何其他内容)
    <VirtualHost *:80>
        SetEnv GIT_PROJECT_ROOT /data/git
        SetEnv GIT_HTTP_EXPORT_ALL
        SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER
        ScriptAliasMatch \
            "(?x)^/(.*/(HEAD | \
            info/refs | \
            objects/(info/[^/]+ | \
            [0-9a-f]{2}/[0-9a-f]{38} | \
            pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
            git-(upload|receive)-pack))$" \
            "/usr/lib/git/git-http-backend/$1"
        Alias /git /data/git
        <Directory /usr/lib/git>
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            AllowOverride None
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    
    1. 现在将出现的 2 次 /data/git 替换为服务器上 git 存储库的父目录(如果您还没有任何存储库,请不要担心,只需使用您打算放置它/它们的目录即可)

    还将/usr/lib/git/git-http-backend 替换为您系统上的git-http-backend 位置,可以使用$ find / -name git-http-backend 找到

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

    根据this 来源,可能需要将目录标记中的最后两行替换为Require all granted 对于 apache 2.4 及更高版本。

    1. 重启apache服务器:$ apache2ctl -k graceful

    现在 apache 服务器已设置完毕,但我们还没有完成,设置 repos 的一些重要部分会影响此设置是否有效。

    1. 设置存储库:
    $ mkdir myrepo.git
    $ cd myrepo.git
    $ git init --bare --shared
    $ cp hooks/post-update.sample hooks/post-update
    $ git update-server-info
    $ chown -R wwwrun:www
    

    这里重要的是要了解最后一行将 repo 的所有者更改为 apache2 用户。此用户在您的系统上可能不同。要查找 apache 用户,请执行 $ ps aux | egrep '(apache|httpd)'。然后要找到用户的组名,执行$ id user-name。在我的系统上,用户是 wwwrun 和组 www。相应地替换。

    1. 使用回购

    为了使用 repo,您​​需要知道 url。对于此设置,网址为 http://server.domain/myrepo.git

    注意:https 不起作用。

    从客户端访问 repo 时,只需将其添加为远程:

    $ git remote add origin http://server.domain/myrepo.git
    

    然后你可以像任何其他 git repo 一样与它交互。

    【讨论】:

    • 非常感谢!它在我的 ubuntu 14.04 和 apache 2.4.7 中为我工作!一些细节,我的 apache 用户是 www-data,在步骤 6 a '.'最后缺少命令“chown ...”。 git位于我的ubuntu文件夹'/usr/lib/git-core'
    • 我试过同样的事情,但似乎有些权限问题。我尝试克隆时收到 403(无法访问)
    • 效果很好,只是它不提示进行任何身份验证。您是否只是在 Apache 中为此设置了基本身份验证?
    • 我很久以前就这样做了,最后得到了一个付费解决方案,所以我无法回答你的问题。希望你能解决
    • 如果您在 CentOS 中执行此操作并且似乎无法取得任何进展 - 请仔细检查您的 iptables 规则。 CentOS 默认锁定所有内容。此外,如果您遵循 Git 文档,您可能需要修改 .htpasswd 文件的 SELinux 设置。
    【解决方案2】:

    访问权限不同的多个项目

    有gitolite,但复杂... 一个简单的解决方案是在 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
    

    在 /opt/git_access.conf 中

    Use Project test1 "admin john" "mike"
    Use Project test2 "admin emma" "all granted"
    

    因此,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;
    };
    

    这个 gitweb 小钩子隐藏(返回 0)当前用户没有读取权限的 git 存储库。

    并进入apache2配置,经典的gitweb conf:

    ## 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>
    

    这是我的配置。

    【讨论】:

    • 太棒了,非常感谢你。一件事,您需要用户... Require $developer $user Require $developer 只是缺少 user 字。 Require user $developer $user 或者我猜 user 词也可以添加到 git_access.conf 中,这样你就可以使用 Require allgranted still。
    猜你喜欢
    • 2011-12-05
    • 2016-03-27
    • 2015-12-15
    • 1970-01-01
    • 2017-09-14
    • 2015-06-29
    • 1970-01-01
    • 2015-11-14
    • 2011-09-18
    相关资源
    最近更新 更多