在 Windows 服务器上使用 Apache 配置 Git

Sky*_*Sky 4 apache git

我需要在 Windows 服务器上设置 Git 存储库。要求是:

  • 使用 Apache 2.2 在 Windows 上运行的 Git(因为这个 Apache 已经存在并且已经用于为 Subversion 提供服务)
  • 允许创建各种 Git 存储库
  • 存储库不是公开的。必须能够定义每个项目的访问权限。有权访问存储库的用户始终具有完全访问权限(拉取和推送)。

我已经完成了标准的 Git 安装并将这些行添加到 Apache 的httpd.conf文件中:

SetEnv GIT_PROJECT_ROOT "D:/srv/git"
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ "C:/Program Files/Git/mingw64/libexec/git-core/git-http-backend/"

<Location "/git/testproject.git">
  AuthType Basic
  require group developers
  AuthName "Git test project"
  AuthUserFile D:/srv/gitauth/auth.txt
  AuthGroupFile D:/srv/gitauth/groups.txt
</Location>
Run Code Online (Sandbox Code Playgroud)

"C:/Program Files/Git/mingw64/libexec/git-core/git-http-backend/"是我git-http-backend在 Windows 上找到可执行文件的地方。auth.txt是一个文件,htpasswd其中包含我的用户的用户名/密码,并groups.txt包含一行定义我的用户在名为developers.

为了测试,我在D:/srv/git/testproject.git.

在我的客户端计算机上,我尝试克隆此存储库并收到此错误:

git clone https://[serverurl]/git/testproject.git
Cloning into 'testproject'...
fatal: unable to access 'https://[serverurl]/git/testproject.git/': The requested URL returned error: 403
Run Code Online (Sandbox Code Playgroud)

Apache 的 error.log 有这个错误信息:

[Wed Aug 23 18:39:10 2017] [error] [client 192.168.130.80] client denied by server configuration: C:/Program Files/Git/mingw64/libexec/git-core/git-http-backend
Run Code Online (Sandbox Code Playgroud)

我没有找到使这项工作的方法。不幸的是,我对 Apache 也不是很熟悉。

这个配置正确吗?我什至不确定SetEnvandScriptAlias命令是否适用,httpd.conf或者它们是否应该放在其他地方。我已经阅读了各种教程和博客文章,其中大多数建议我的 Apache 安装中不存在的不同地方(也许 Windows 环境不同......?)。

任何帮助将不胜感激!

Sky*_*Sky 6

经过几个小时的研究和测试,我终于让它运行起来并设法设置它以满足我的要求。

这是我必须添加到 Apache 的配置中才能使其工作的内容:

# 1. allow access to CGI directory, where git-http-backend.exe is located
<Directory "C:/Progra~1/Git/mingw64/libexec/git-core/">
    Options +ExecCGI
    Allow From All
</Directory>

# 2. Define where GIT projects are located and create /git/ script alias
SetEnv GIT_PROJECT_ROOT "D:/srv/git"
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ "C:/Progra~1/Git/mingw64/libexec/git-core/git-http-backend.exe/"

# 3. Restricting access to /git/ root, otherwise not yet defined projects could be read
#    without restriction.
<Location "/git/">
    AuthType Basic
    AuthName "git"
    AuthUserFile D:/srv/auth_files/htpasswd.txt
    Require all denied
</Location>

# 4. For every project a section like this must be created in order to allow access to it.
<Location "/git/testproject.git/">
   AuthType Basic
   AuthName "git test repository"
   AuthUserFile D:/srv/auth_files/htpasswd.txt
   AuthGroupFile D:/srv/auth_files/git_groups.txt
   Require group developers
</Location>

<Location "/git/other_project.git/">
   AuthType Basic
   AuthName "git test repository"
   AuthUserFile D:/srv/auth_files/htpasswd.txt
   AuthGroupFile D:/srv/auth_files/git_groups.txt
   Require group developers managers
   Require user sky
   AuthzGroupFileAuthoritative Off
</Location>
Run Code Online (Sandbox Code Playgroud)

一些注意事项

  • 第 (1) 部分似乎仅在 Windows 上是必需的。如果没有此指令,Apache 将不会将 git-http-backend.exe 文件作为 CGI 脚本执行。
  • 第 (3) 部分是必要的,因为如果没有它,Apache 将允许访问每个未在后面的配置中明确定义的项目。
  • 第(4)部分项目other_project.git允许两个组和一个用户访问。Apache 2.2 有AuthzGroupFileAuthoritative Off设置,使其正常工作。如果没有这个,只有开发人员或管理员组中的用户天空才能访问。在 Apache 2.4 中,这可以通过<RequireAny>块以更好的方式完成。
  • 克隆这些存储库之一的示例: git clone https://[serverurl]/git/testproject.git