【发布时间】:2014-12-31 07:56:06
【问题描述】:
我需要用 git-over-http(智能 http)搭建一个 git 服务器,但是网上可用的资源很乱,混入了其他 apache 配置,缺少细节或者不够明确。
我根据我发现可用资源中的不足来回答这个问题。
【问题讨论】:
标签: git apache http git-http-backend
我需要用 git-over-http(智能 http)搭建一个 git 服务器,但是网上可用的资源很乱,混入了其他 apache 配置,缺少细节或者不够明确。
我根据我发现可用资源中的不足来回答这个问题。
【问题讨论】:
标签: git apache http git-http-backend
首先有必要了解 git-over-http 有 2 个组件:git 和 apache。这两者是通过一个名为 git-http-backend 的脚本连接起来的。挑战在于配置这两个组件之间的接口,让对git的http请求由apache转发。
注意:安全性超出了本指南的范围。
首先使用发行版的包管理器安装 git 和 apache2。
添加 apache 启用 git-over-http 所需的模块。这些是 cgi、别名和 env
$ a2enmod cgi alias env
/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>
/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 及更高版本。
$ apache2ctl -k graceful
现在 apache 服务器已设置完毕,但我们还没有完成,设置 repos 的一些重要部分会影响此设置是否有效。
$ 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。相应地替换。
为了使用 repo,您需要知道 url。对于此设置,网址为 http://server.domain/myrepo.git
注意:https 不起作用。
从客户端访问 repo 时,只需将其添加为远程:
$ git remote add origin http://server.domain/myrepo.git
然后你可以像任何其他 git repo 一样与它交互。
【讨论】:
访问权限不同的多个项目
有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。