- 利用现有 Web 服务器:如果你已经有一个运行中的 Apache 服务器,可以很方便地集成。
- 基于 HTTPS:天然支持 SSL/TLS 加密,传输安全,且配置简单。
- 权限管理灵活:可以和 Apache 的用户认证(
.htaccess)或 LDAP/AD 集成。 - 防火墙友好:通常只开放 80 (HTTP) 和 443 (HTTPS) 端口,便于企业网络管理。
下面我将为你提供一个详细的、分步的搭建指南。

整体架构
我们的目标是实现以下流程:
客户端 <--(HTTPS + Git 协议)--> Apache 服务器 <--(调用)--> Git 后端
客户端通过 https:// 协议克隆和推送代码,Apache 服务器接收到请求后,会将其反向代理给 Git 的后端服务(通常是 git-http-backend CGI 程序)来处理。
环境准备
- 操作系统:以 CentOS 7 / RHEL 7 或 Ubuntu 20.04 / 22.04 为例。
- 服务器 IP:假设为
168.1.100。 - 域名:假设为
git.yourdomain.com(可选,但推荐使用)。 - 客户端:一台装有 Git 的电脑。
第一步:安装必要软件
在服务器上,我们需要安装 Apache、Git 以及 Git 的 Apache 扩展。
对于 CentOS / RHEL 系统
# 更新软件包 sudo yum update -y # 安装 Apache, Git, 以及 Git 的 Apache CGI 支持 sudo yum install -y httpd git git-daemon.x86_64
对于 Ubuntu / Debian 系统
# 更新软件包列表 sudo apt update # 安装 Apache, Git sudo apt install -y apache2 git
安装完成后,确保 Apache 服务正在运行:

# CentOS sudo systemctl start httpd sudo systemctl enable httpd # Ubuntu sudo systemctl start apache2 sudo systemctl enable apache2
第二步:创建 Git 仓库和用户
为了安全,我们不建议使用 root 用户来管理 Git 仓库,这里我们创建一个专用的 git 用户。
# 创建 git 用户,并设置其家目录为 /home/git sudo useradd -m -s /bin/bash git # 为 git 用户设置密码 sudo passwd git
切换到 git 用户,创建一个裸仓库,裸仓库没有工作目录,非常适合作为服务器端仓库。
# 切换到 git 用户 sudo su - git # 创建一个名为 myproject.git 的裸仓库 git init --bare /home/git/myproject.git # 退出 git 用户 exit
为了让 Apache 可以读写这个仓库,我们需要修改仓库的所有者和权限。
# 将仓库的所有者改为 apache 用户 (CentOS) 或 www-data (Ubuntu) # CentOS sudo chown -R apache:apache /home/git/myproject.git # Ubuntu sudo chown -R www-data:www-data /home/git/myproject.git # 设置正确的权限 # 770 表示 git 用户和 apache 用户组可读写执行,其他用户无权限 sudo chmod -R 770 /home/git/myproject.git
第三步:配置 Apache
这是最关键的一步,我们需要配置 Apache 来处理 Git 的 HTTP 请求。

启用 Apache CGI 和 SSL 模块
# CentOS sudo yum install -y mod_ssl mod_cgi sudo systemctl restart httpd # Ubuntu sudo a2enmod ssl sudo a2enmod cgi sudo systemctl restart apache2
创建 Apache 配置文件
我们将为 Git 创建一个独立的虚拟主机配置文件。
# CentOS sudo vim /etc/httpd/conf.d/git.conf # Ubuntu sudo vim /etc/apache2/sites-available/git.yourdomain.com.conf
粘贴到文件中。请务必修改 ServerName 和 SSLCertificateFile/SSLCertificateKeyFile 的路径为你自己的信息。
# 虚拟主机配置
<VirtualHost *:443>
ServerName git.yourdomain.com # 替换成你的域名或IP
ServerAdmin admin@yourdomain.com
DocumentRoot /var/www/html # 默认网站根目录,可以留空
# SSL 配置 (必须)
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/localhost.crt # 替换成你的证书文件
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key # 替换成你的私钥文件
# Git 项目配置
SetEnv GIT_PROJECT_ROOT /home/git
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
# 认证配置 (可选,但强烈推荐)
# AuthType Basic
# AuthName "Git Access"
# AuthUserFile /etc/httpd/git_htpasswd # 存储用户密码的文件
# Require valid-user # 或者指定用户 Require user alice bob
<Directory "/usr/libexec/git-core/git-http-backend/">
Options +ExecCGI +SymLinksIfOwnerMatch
AllowOverride None
Order allow,deny
Allow from all
# 如果启用了认证,需要添加以下两行
# AuthType Basic
# AuthUserFile /etc/httpd/git_htpasswd
# Require valid-user
</Directory>
# 日志配置
ErrorLog /var/log/httpd/git_error.log
CustomLog /var/log/httpd/git_access.log combined
</VirtualHost>
配置说明:
ServerName:你的 Git 服务器域名或 IP 地址。SSLCertificateFile/SSLCertificateKeyFile:SSL 证书和私钥的路径,如果你没有证书,可以先生成一个自签名的用于测试。- 生成自签名证书 (仅用于测试):
# CentOS sudo openssl req -new -x509 -days 365 -nodes -out /etc/pki/tls/certs/localhost.crt -keyout /etc/pki/tls/private/localhost.key # Ubuntu sudo openssl req -new -x509 -days 365 -nodes -out /etc/ssl/certs/ssl-cert-snakeoil.pem -keyout /etc/ssl/private/ssl-cert-snakeoil.key
- 生成自签名证书 (仅用于测试):
SetEnv GIT_PROJECT_ROOT /home/git:告诉 Git 后端,所有仓库都在/home/git目录下。ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/:将所有/git/开头的请求都交给git-http-backend程序处理。AuthUserFile /etc/httpd/git_htpasswd:定义一个密码文件,我们先不启用它,等测试成功后再配置。
启用配置并重启 Apache
# CentOS sudo systemctl restart httpd # Ubuntu # 启用站点 sudo a2ensite git.yourdomain.com.conf # 禁用默认站点 (可选) sudo a2dissite 000-default.conf # 重启 Apache sudo systemctl restart apache2
第四步:创建密码文件并启用认证 (可选但推荐)
为了安全,我们必须为用户设置密码。
# 创建密码文件,-c 表示创建文件(第一次时使用) sudo htpasswd -c /etc/httpd/git_htpasswd your_username # 系统会提示你输入两次密码 # 如果要添加更多用户,去掉 -c 参数 sudo htpasswd /etc/httpd/git_htpasswd another_username
编辑你的 Apache 配置文件 (/etc/httpd/conf.d/git.conf 或 /etc/apache2/sites-available/...),取消认证部分的注释。
# ...
# 认证配置
AuthType Basic
AuthName "Git Access"
AuthUserFile /etc/httpd/git_htpasswd
Require valid-user
# ...
保存文件后,重启 Apache。
# CentOS sudo systemctl restart httpd # Ubuntu sudo systemctl restart apache2
第五步:客户端测试
服务器端配置完成,我们可以在客户端进行测试。
克隆仓库
打开你的 Git 客户端(Bash, PowerShell, Git Bash 等),执行以下命令:
git clone https://git.yourdomain.com/git/myproject.git # 如果启用了认证,系统会弹出窗口要求输入用户名和密码
如果一切顺利,你就可以成功克隆到本地的 myproject 文件夹了。
推送更改
# 进入克隆下来的项目目录 cd myproject # 创建一个新文件并提交 echo "# My First Project" > README.md git add . git commit -m "Initial commit" # 推送到服务器 git push origin master # 同样,如果启用了认证,需要输入用户名和密码
如果推送成功,说明你的 Git 服务器已经搭建完毕!
故障排除
如果遇到问题,请检查以下几点:
- 防火墙:确保服务器的 443 (HTTPS) 端口是开放的。
sudo firewall-cmd --permanent --add-service=httpssudo firewall-cmd --reload
- SELinux:在 CentOS 系统上,SELinux 可能会阻止 Apache 访问仓库文件,可以临时关闭测试:
sudo setenforce 0- 如果问题解决,则永久配置 SELinux 策略。
- Apache 错误日志:这是排查问题的首选!
tail -f /var/log/httpd/error_log(CentOS)tail -f /var/log/apache2/error.log(Ubuntu)- 日志中会明确告诉你权限问题、文件找不到问题等。
- 权限问题:确保
/home/git/myproject.git的所有者是apache(或www-data),并且权限是770。 - URL 路径:确保克隆的 URL 中的路径是
/git/...,而不是 。
通过以上步骤,你就拥有了一个功能完善、安全可靠的基于 Apache 的 Git 服务器,它不仅可以通过 HTTPS 加密传输,还能利用 Apache 强大的用户认证和权限管理功能,非常适合团队协作和个人项目托管。
