概述
前不久在公司内网搭建了一个GitLab CE,随之产生了一个疑问:GitLab是如何创建git服务器的?可以发现GitLab支持http和ssh两种请求方式,作为一个喜欢动手动脚的五好青年,那必须得捣鼓一番,本次将在Mac上使用Http的方式搭建git服务器。
参考文档
https://www.nginx.com/resources/wiki/start/topics/examples/fcgiwrap/
https://www.centos.bz/2017/07/ubuntu-16-04-nginx-http-git/
准备工作
Mac OS
下载并安装Git
测试是否安装完成:
git --version
git version 2.24.0
安装FCGI Wrap
不要问为什么安装FCGI,问就是百度。
brew install fcgiwrap
安装完成后看一下最后几行信息,它会告诉你fcgiwrap的安装路径,比如Mac下:
/usr/local/Cellar/fcgiwrap/1.1.0/sbin/fcgiwrap
这个路径后面会用到。
设置开机启动项
创建一个文件fcgiwrap
,随便你放在哪里,比如:/Users/kyg/bin/fcgiwrap
#!/usr/bin/perl
use strict;
use warnings FATAL => qw( all );
use IO::Socket::UNIX;
my $bin_path = '/usr/local/bin/fcgiwrap';
my $socket_path = $ARGV[0] || '/tmp/cgi.sock';
my $num_children = $ARGV[1] || 1;
close STDIN;
unlink $socket_path;
my $socket = IO::Socket::UNIX->new(
Local => $socket_path,
Listen => 100,
);
die "Cannot create socket at $socket_path: $!\n" unless $socket;
for (1 .. $num_children) {
my $pid = fork;
die "Cannot fork: $!" unless defined $pid;
next if $pid;
exec $bin_path;
die "Failed to exec $bin_path: $!\n";
}
这玩意可不是我写出来的,说实话我不知道它在干啥,这都是nginx文档上写的。
注意,你需要把$bin_path
换成你自己的fcgiwrap的安装路径。
再把fcgiwrap变成可执行文件:chmod +x fcgiwrap
最后把脚本添加到启动项:
配置nginx
在nginx用户配置目录下创建文件/usr/local/etc/nginx/servers/gitlab.conf
:
# GitLab ME Conf
server {
# gitlab监听的端口
listen 80 default_server;
location ~ (/.*) {
auth_basic "Git Login";
# 存放用户名和密码
auth_basic_user_file "/usr/local/etc/nginx/pass.db";
include /usr/local/etc/nginx/fastcgi_params;
# 在git的安装目录可以找到git-http-backend
fastcgi_param SCRIPT_FILENAME /usr/local/Cellar/git/2.24.0/libexec/git-core/git-http-backend;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
# 全部仓库的父目录
fastcgi_param GIT_PROJECT_ROOT /Users/kyg/code/repo;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_param PATH_INFO $1;
# /Users/kyg/bin/fcgiwrap文件中提到的cgi.sock
fastcgi_pass unix:/tmp/cgi.sock;
}
}
创建用户和密码
创建用户root,密码也是root
# 会提示输入两次密码
htpasswd -c /usr/local/etc/nginx/pass.db root
会看到pass.db中多了一行:
root:$apr1$NKxLGs3r$1wzqc6ZQ46L678Kqx1FxN0
创建git仓库
cd /Users/kyg/code/repo
mkdir demo.git
git init --bare demo.git
测试克隆
测试前请先重启,让开机启动项运行起来,可以通过ps查看有没有运行:
kyg@kangyonggan % ps -ef | grep fcgiwrap
501 550 1 0 11:00AM ?? 0:00.01 /usr/local/Cellar/fcgiwrap/1.1.0/sbin/fcgiwrap
501 1663 725 0 12:32PM ttys000 0:00.00 grep fcgiwrap
克隆项目:
git clone http://localhost/demo.git
然后会要求输入用户名和密码。
如果不幸报错,请查看nginx的日志access.log和error.log自行百度解决。
其他Linux
在Linux上的操作和Mac类似,也就是安装命令、安装路径、开机启动项设置不一样。