搭建一个Git HTTP(S)服务器
你可以将你的仓库放在一个HTTP请求目录下,然后直接使用git clone对应的URL路径即可。
Git提供了一个叫git-http-backend的程序,它可以帮助你实现推送。具体步骤如下:
- 安装Git和Apache/Nginx。
- 配置Apache/Nginx以便它可以处理Git HTTP请求。
- 配置Git以便它可以使用Apache/Nginx作为HTTP服务器。
- 创建一个裸仓库。
- 启用Git HTTP后端。
- 配置访问控制。
下面的指南,可以帮助你完成每个步骤:
1. 安装Git和Apache/nginx。
Git:你可以从Git官方网站下载Git的安装程序,然后按照安装向导的指示进行安装。
Apache/Nginx:你可以从Apache/Nginx官方网站下载Apache/Nginx的安装程序,然后按照安装向导的指示进行安装。
2. 配置Apache/Nginx以便它可以处理Git HTTP请求。
在Apache的配置文件中添加以下内容:
SetEnv GIT_PROJECT_ROOT /path/to/git/repositories
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
如果使用ngnix,则需要在配置文件中加入一下内容:
location ~ /git(/.*) {
client_max_body_size 0;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /path/to/git/repositories;
fastcgi_param PATH_INFO $1;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
上述的内容的解释如下:
- location ~ /git(/.*) {:匹配 URL 以 /git 开头的请求。
- client_max_body_size 0;:允许客户端上传任意大小的文件。
- include fastcgi_params;:包含 FastCGI 的参数。
- fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;:指定 Git HTTP 后端的位置。
- fastcgi_param GIT_HTTP_EXPORT_ALL “”;:禁用 Git HTTP 后端的导出功能。
- fastcgi_param GIT_PROJECT_ROOT /path/to/git/repositories;:指定 Git 仓库的根目录。
- fastcgi_param PATH_INFO $1;:将 URL 中的第一个参数传递给 Git HTTP 后端。
- fastcgi_pass unix:/var/run/fcgiwrap.socket;:将请求转发到 FastCGI 进程。
3. 配置Git以便它可以使用Apache/Nginx作为HTTP服务器。
在Git的配置文件中添加以下内容:
[http]
receivepack = true
uploadpack = true
sslVerify = false
sslCert = /path/to/cert.pem
sslKey = /path/to/key.pem
sslCAInfo = /path/to/ca.pem
sslCertPasswordProtected = true
4. 创建一个裸仓库。
使用以下命令创建一个裸仓库:
git init --bare /path/to/repo.git
5. 启用Git HTTP后端。
在Apache的配置文件中添加以下内容:
<LocationMatch "^/git/.*/git-receive-pack$">
AuthType Basic
AuthName "Git Access"
AuthUserFile /path/to/users.htpasswd
Require valid-user
Options +ExecCGI
SetEnv GIT_HTTP_BACKEND "/usr/libexec/git-core/git-http-backend"
</LocationMatch>
如果使用ngnix,则需要在配置文件中添加以下内容
location ~ /git(/.*) {
client_max_body_size 0;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /path/to/git/repositories;
fastcgi_param PATH_INFO $1;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
}
6. 配置访问控制。
创建一个用户列表文件:
user1:password1
user2:password2
使用以下命令创建一个密码文件:
htpasswd -c /path/to/users.htpasswd user1
htpasswd /path/to/users.htpasswd user2
另,
可以通过spawn-fcgi的-p指令指定fcgiwrap监听某端口,这是不同于套接字的设置。
spawn-fcgi -f /usr/sbin/fcgiwrap -p 7099
相应的http配置如下:
location ~ /git(/.*)$ {
client_max_body_size 0;
fastcgi_pass 127.0.0.1:7099;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param GIT_PROJECT_ROOT /git/repositories;
fastcgi_param PATH_INFO $1;
include /etc/nginx/fastcgi_params;
}
仓库的创建
通过git init创建服务器端仓库
git init --bare xxxx.git
创建仓库后,需要确认该仓库支持http
确认仓库路径下的.git/config文件中,需要有下列的内容
[http]
receivepack = true
uploadpack = true
sslVerify = false
过期
替代
当前的git版本中,已经不支持git-http-backend了。
需要通过gitweb提供http协议。
安装gitweb的方法如下(alpine)
apk add git-gitweb --repository=http://dl-cdn.alpinelinux.org/alpine/edge/main
配置方法
gitweb是一个perl的cgi程序,代码在下面的目录中
/usr/share/gitweb/gitweb.cgi
Docker
在hub.docker.com中,有一个名为fraoustin/gitweb的nginx&gitweb镜像,镜像地址
下面是镜像的主页文档:
Docker Image for gitweb
generate a nginx server with git server and gitweb for ihm on http://127.0.0.1/
load when start image load file in
- /usr/share/gitweb/docker-entrypoint.pre
- /usr/share/gitweb/docker-entrypoint.post
Parameter - SET_CONTAINER_TIMEZONE (false or true) manage time of container
- CONTAINER_TIMEZONE timezone of container
- GITPROJECT
- GITUSER (default gituser)
- GITPASSWORD (default gitpassword)
- IHM (default “”)
Volume - /var/lib/git
Port - 80 for gitweb
Command - addrepos: add repository
- addauth : add user for git
- rmrepos : remove repository
- rmauth : remove user
Usage direct
run image fraoustin/gitweb
docker run -d -e "CONTAINER_TIMEZONE=Europe/Paris" -e "GITPROJECT=test" -v <localpath>:/var/lib/git --name test -p 80:80 fraoustin/gitweb
user default is gituser and password default is gitpassword
you use http://localhost/ for access gitweb
you can clone project
git clone http://gituser:gitpassword@localhost/test.git
You can change user and password by variable environment
Usage by Dockerfile
Sample of Dockerfile
FROM fraoustin/gitweb
COPY ./00_init.sh /usr/share/gitweb/docker-entrypoint.pre/00_init.sh
RUN chmod +x -R /usr/share/gitweb/docker-entrypoint.pre
File 00_init.sh
#!/bin/bash
REPOS='/var/lib/git/test.git'
if [ ! -d $REPOS ]; then
addrepos test
cd $REPOS
chmod -R g+ws .
chgrp -R nginx .
fi
addauth $GITUSER $GITPASSWORD
build image mygit
docker build -t mygit .
run image mygit
docker run -d -e "CONTAINER_TIMEZONE=Europe/Paris" -e "GITUSER=gituser" -e "GITPASSWORD=gitpassword" -v <localpath>:/var/lib/git --name test -p 80:80 mygit
IHM material design
If you want use a new design for ihm, you can use IHM variable
- IHM = mdl
docker run -d -e "CONTAINER_TIMEZONE=Europe/Paris" -e "IHM=mdl" -e "GITPROJECT=test" -v
问题一
如果您在alpine系统中安装完git后,没有git-http-backend,可能是因为git-http-backend二进制文件在最新的alpine linux包(3.7及以上)中丢失。可以通过下面的命令单独安装:
apk add git-daemon
问题二
如果fcgi程序没有权限创建文件,您可以尝试以下方法来提升权限:
检查文件夹的权限。确保您的fcgi程序有足够的权限来访问该文件夹。您可以使用chmod命令更改文件夹的权限。例如,如果您想将文件夹的所有者和组的权限设置为读、写和执行,则可以使用以下命令:
chmod u+rwx,g+rwx 文件夹名
检查nginx用户的权限。确保nginx用户有足够的权限来访问该文件夹。您可以使用chown命令更改文件夹的所有者和组。例如,如果您想将文件夹的所有者设置为nginx用户,则可以使用以下命令:
chown nginx 文件夹名
参考资料
GitWeb学习笔记
GitWeb服务器搭建