本文详细介绍了在 windows 系统上如何配置 apache git 服务器,并使用 active directory (ad) 进行用户认证。
软件环境
安装 Apache
从 Apache Httpd 的官方网站下载最新版的 Windows 版本,我下载的是 2.2.22 版本。按照提示完成安装,我的安装目录为 C:\Apache2.2,接下来的配置都基于这个目录。
安装 Git
下载并安装 msysgit,建议使用便携版(Portable)。下载后解压到 C:\Git 目录下。然后创建 C:\GitRepos 目录,作为代码库的根目录,接下来的配置都基于这两个目录。
配置 Apache 使用 AD 认证
首先,停止 Apache 服务器。打开 C:\Apache2.2\conf\httpd.conf 文件,搜索 <directory></directory>,修改根目录配置,允许所有位置访问,如下:
<directory></directory>
Options FollowSymLinks
AllowOverride None
Order deny,allow
# 默认是 Deny from all,修改为 Allow from all
Allow from all在 httpd.conf 文件中搜索 ldap,确保 authnz_ldap_module 和 ldap_module 都已被加载(行首没有#)。
新建一个 git.conf 文件,与 httpd.conf 保存在同一目录。然后在 httpd.conf 的末尾添加一行 Include conf/git.conf 以包含这个文件。
保存 httpd.conf,打开 git.conf,添加以下内容:
Alias /git "C:/GitRepos"
将 C:\Repos 映射为 /git,接下来设置这个目录的认证:
<directory></directory>
# 设置认证名称、类型
AuthName "Git Access"
AuthType Basic
AuthBasicProvider ldap
# 设置 LDAP 搜索的目录,使用 sAmAccountName 登录,位于这个 AD 目录下的所有用户都可以登录
AuthLDAPURL "ldap://company.local:389/O=MyOrg,DC=company,DC=local?sAMAccountName?sub?(objectClass=*)"
AuthzLDAPAuthoritative on
# 设置 Apache 搜索 AD 时使用的凭据
AuthLDAPBindDN "username@company.local"
AuthLDAPBindPassword userpassword
# 设置使用 AD 组过滤时搜索的条件
AuthLDAPGroupAttributeIsDN on
AuthLDAPGroupAttribute member
# 通过认证的用户都可以访问
Require valid-user保存 httpd.conf 和 git.conf,启动 Apache HTTP 服务。如果一切顺利,访问 https://www.php.cn/link/46ec3ee8c6ef686a945d04daf92927f3 会显示 "It works!",表示 HTTP 服务正常运行。访问 https://www.php.cn/link/46ec3ee8c6ef686a945d04daf92927f3/git 会弹出登录框,输入用户名和密码后可以顺利访问。
配置 Git Smart Http
首先运行 C:\Git\libexec\git-core\git-http-backend.exe。如果出现下面的错误提示:

则需要将 C:\Git\bin 和 C:\Git\cmd 添加到 Path 环境变量中,然后再运行 git-http-backend.exe,不应再出现错误提示。
接下来修改 git.conf,让 Apache 启动 git-http-backend.exe。打开 git.conf,将原来的
Alias /git "C:/GitRepos"
注释掉,改为:
# 设置 Git 代码库的根目录 SetEnv GIT_PROJECT_ROOT C:/GitRepos # 默认通过 HTTP 导出所有的 Git 代码库 SetEnv GIT_HTTP_EXPORT_ALL # 将 git-http-backend.exe 映射为 cgi 程序,请求 /git/ 下的所有请求都由 # git-http-backend.exe 处理 ScriptAlias /git/ C:/Git/libexec/git-core/git-http-backend.exe/
将原来配置的 <directory></directory> 修改为 <location></location>,如下所示:
<location></location>
# 设置认证名称、类型
AuthName "Git Access"
AuthType Basic
AuthBasicProvider ldap
# 设置 LDAP 搜索的目录,使用 sAmAccountName 登录,位于这个 AD 目录下的所有用户都可以登录
AuthLDAPURL "ldap://company.local:389/O=MyOrg,DC=company,DC=local?sAMAccountName?sub?(objectClass=*)"
AuthzLDAPAuthoritative on
# 设置 Apache 搜索 AD 时使用的凭据
AuthLDAPBindDN "username@company.local"
AuthLDAPBindPassword userpassword
# 设置使用 AD 组过滤时搜索的条件
AuthLDAPGroupAttributeIsDN on
AuthLDAPGroupAttribute member
# 通过认证的用户都可以访问
Require valid-user保存 git.conf,重启 Apache httpd 服务。现在,可以在 C:\GitRepos 目录下使用 git 新建一个测试库,打开命令行窗口,输入以下命令:
git init --bare Test.git
然后在新的命令行窗口中输入以下命令进行测试:
git clone https://www.php.cn/link/46ec3ee8c6ef686a945d04daf92927f3/git/Test.git
接下来会提示输入用户名和密码,最后看到以下提示表示成功:
C:\temp>git clone https://www.php.cn/link/46ec3ee8c6ef686a945d04daf92927f3/git/Test.git Cloning into 'Test'... Username for 'https://www.php.cn/link/46ec3ee8c6ef686a945d04daf92927f3': zhangzhimin Password for 'https://zhangzhimin@localhost': warning: You appear to have cloned an empty repository.
如果出现错误,可以查看 C:\Apache2.2\logs\error.log。如果错误原因是 “Repository not exported”,需要在 Test.git 目录下创建一个名为 git-daemon-export-ok 的空文件。如果提示关于 git-http-backend.exe 的问题,则可能是 git-http-backend.exe 无法运行造成的。
设置代码库权限
现在,Windows 上的 Git 服务器已经可以运行了,通常情况下,需要为每个库配置权限。例如,Test.git 只允许特定用户或特定用户组访问,则需要在 git.conf 文件中添加以下配置:
<location></location>
AuthName "Private Git Access"
# 允许特定用户访问
Require ldap-user zhangzhimin
# 允许用户组访问,把下面一行行首的#去掉即可
#Require ldap-group CN=Developers,OU=GitUsers,O=MyOrg,DC=company,DC=local改完后保存 git.conf 并重启 Apache httpd 服务。
注意问题
整个配置过程比较复杂,在 Windows 上配置的资料也比较少,很容易出错,因此在配置过程中,给出以下建议:
以上就是在 Windows 系统上配置 Apache Git 服务器的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号