git 如何通过http设置git?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26734933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:38:10  来源:igfitidea点击:

How to set up git over http?

gitapachehttpgit-http-backend

提问by Marmoy

I need to set up a git server with git-over-http (smart http), but the resources available online are a mess, mixing in other apache configuration, missing details or not being explicit enough.

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

I am answering this question myself based on what I found lacking in the available resources.

我根据我发现可用资源缺乏的内容自己回答这个问题。

回答by Marmoy

First it is necessary to understand that there are 2 components to git-over-http: git and apache. These two are connected through a script with the name of git-http-backend. The challenge is to configure the interface between these two components, so that http requests to git are forwarded by apache.

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

Note:Security is outside the scope of this guide.

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

  1. Start out by installing git and apache2 using the package manager of your distribution.

  2. Add the modules needed by apache to enable git-over-http. These are cgi, alias and env

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

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

$ a2enmod cgi alias env
  1. Copy the following into /etc/apache2/httpd.conf(without removing whatever else it contains)
  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/"
    Alias /git /data/git
    <Directory /usr/lib/git>
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
  1. Now replace the 2 occurrences of /data/gitwith the parent directory of your git repos on the server (don't worry if you don't have any repos yet, just use the directory where you intend to place it/them)
  1. 现在用/data/git服务器上 git 存储库的父目录替换 2 次出现(如果您还没有任何存储库,请不要担心,只需使用您打算放置它/它们的目录)

Also replace /usr/lib/git/git-http-backendwith the location of git-http-backend on your system, which can be found using $ find / -name git-http-backend

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

It may be that on your system REDIRECT_REMOTE_USERactually overwrites a valid REMOTE_USER. If this setup doesn't work when finished, try removing that line.

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

According to thissource, it may be necessary to replace the last two lines within the Directory tag by Require all grantedfor apache 2.4and above.

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

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

Now the apache server is set up, but we're not done yet, there are some important parts of setting up the repos that will affect whether this setup works or not.

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

  1. Set up the repo:
  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

Here it is important to understand that the last line changes the owner of the repo to the apache2 user. This user may be different on your system. To find the apache user, execute $ ps aux | egrep '(apache|httpd)'. Then to find the group name of the user, execute $ id user-name. On my system the user is wwwrunand the group www. Replace accordingly.

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

  1. Use the repo
  1. 使用回购

In order to use the repo, you need to know the url. For this setup the url is http://server.domain/myrepo.git

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

Note: httpswill not work.

注:HTTP小号将无法正常工作。

When accessing the repo from a client, you just add it as a remote:

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

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

Then you can interact with it like any other git repo.

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

回答by tfa

Multiples projects with differents access rigths

具有不同访问权限的多个项目

There is gitolite, but complex... A simple solution is to create macro in apache2 conf as :

有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

And in /opt/git_access.conf

在 /opt/git_access.conf

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

So, the git project test1 will have read/write access by admin and john, and read only access by mike.

因此,git 项目 test1 将具有 admin 和 john 的读/写访问权限,而 Mike 具有只读访问权限。

The project test2 will have read access by all authentified users.

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

The git_access.conf can be produced by your tools from a database of yours projetcs and users for example, and reload by a "service httpd reload".

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

For the same config for gitweb read access, add in /etc/gitweb.conf :

对于 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 =  . ' ' . ;
                    $users =~ s/all granted/$user/;
                    $users =~ s/user//;
                    if ( $users =~ m/$user/ ) {
                        return 1;
                    }
                }              
            }
        }
        return 0;
};

This little gitweb hook hide (return 0) the git repositories with no read access for current user.

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

And into apache2 configuration, the classic gitweb conf :

进入 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>

That's my config.

那是我的配置。