Apache作为一款成熟稳定的Web服务器软件,除了提供HTTP服务外,还能通过配置模块实现Git代码托管服务,相较于专门的Git服务器解决方案(如GitLab、Gitea),Apache搭建Git服务器具有轻量、灵活、与现有Web服务集成的优势,适合中小型团队或个人开发者使用,本文将从环境准备、安装配置、安全加固及日常运维四个方面,详细阐述基于Apache搭建Git服务器的完整流程。

环境准备与基础安装
在开始搭建前,需确保系统已安装必要的软件环境,以CentOS 7系统为例,首先更新系统并安装Apache、Git及依赖包:
sudo yum update -y sudo yum install -y httpd git openssl openssl-devel
安装完成后,启动Apache服务并设置开机自启:
sudo systemctl start httpd sudo systemctl enable httpd
创建Git用户及仓库存储目录,为后续操作做准备:
sudo useradd -r -s /bin/git-shell git sudo mkdir -p /var/git/repo sudo chown -R git:git /var/git/repo
Git仓库初始化与Apache配置
创建裸仓库
Git仓库需以“裸仓库”形式存在,避免工作目录与服务器代码冲突,初始化一个示例仓库:
sudo su - git cd /var/git/repo git init --bare sample_project.git exit
配置Apache虚拟主机
编辑Apache配置文件/etc/httpd/conf.d/git.conf,添加以下内容:

<VirtualHost *:80>
ServerName git.example.com
DocumentRoot /var/www/html
<Directory /var/git/repo>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<LocationMatch "^/.*/git-receive-pack$">
AuthType Basic
AuthName "Git Repository"
AuthUserFile /etc/httpd/conf.d/git.htpasswd
Require valid-user
</LocationMatch>
</VirtualHost>创建认证用户
使用htpasswd工具创建认证用户并设置密码:
sudo htpasswd -c /etc/httpd/conf.d/git.htpasswd admin
启用CGI支持
Git over HTTP需要CGI模块支持,启用相关模块并修改配置:
sudo yum install -y mod_fcgid echo "ScriptAlias /git/ /usr/libexec/git-core/" >> /etc/httpd/conf.d/git.conf echo "<Files "git-http-backend">" >> /etc/httpd/conf.d/git.conf echo " Require all granted" >> /etc/httpd/conf.d/git.conf echo "</Files>" >> /etc/httpd/conf.d/git.conf
安全加固与SSL配置
为保障传输安全,建议为Git服务启用HTTPS,以下是使用Let’s Encrypt免费证书的配置步骤:
安装Certbot
sudo yum install -y certbot python2-certbot-apache
获取并配置SSL证书
sudo certbot --apache -d git.example.com
根据提示完成域名验证后,Certbot会自动修改Apache配置文件,添加443端口监听及SSL相关指令,修改后的虚拟主机配置如下:
<VirtualHost *:443>
ServerName git.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/git.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/git.example.com/privkey.pem
<Directory /var/git/repo>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<LocationMatch "^/.*/git-receive-pack$">
AuthType Basic
AuthName "Git Repository"
AuthUserFile /etc/httpd/conf.d/git.htpasswd
Require valid-user
</LocationMatch>
</VirtualHost>防火墙与SELinux配置
开放必要端口并调整SELinux策略:

sudo firewall-cmd --permanent --add-service={http,https}
sudo firewall-cmd --reload
sudo setsebool -P httpd_can_connect_git 1
sudo setsebool -P httpd_execmem 1日常运维与故障排查
仓库权限管理
| 操作命令 | 功能说明 |
|---|---|
sudo chmod 750 /var/git/repo/project.git | 设置仓库目录权限 |
sudo git update-server-info | 更新服务器信息文件 |
sudo git --git-dir=/path/to/repo.git config http.receivepack true | 启用接收包 |
常见问题解决
- 权限拒绝问题:检查仓库目录所有者是否为
git用户,SELinux上下文是否正确(chcon -R -t httpd_sys_content_t /var/git/repo)。 - 推送失败:确认
git-receive-pack已启用,认证用户是否有写入权限。 - SSL证书续期:设置定时任务自动续期证书:
echo "0 0 * * * /usr/bin/certbot renew --quiet" | sudo crontab -
备份策略
定期备份仓库数据及Apache配置文件:
sudo tar -czf /backup/git_repo_$(date +%Y%m%d).tar.gz /var/git/repo sudo cp /etc/httpd/conf.d/git.conf /backup/git_config_$(date +%Y%m%d).conf
通过以上步骤,即可完成基于Apache的Git服务器搭建,该方案在保证功能完整性的同时,充分利用了现有Web服务器的资源,适合对资源要求不高的场景,如需扩展更多功能(如Web界面、项目管理等),可结合Gitosis或Gitolite等工具进一步增强服务器管理能力。
图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/25937.html




