1、安装GIT
yum install git
git --version
接下来创建一个git用户组和用户,用来运行git服务:
groupadd git
adduser git -g git
passwd git
2、创建证书登录
收集所有需要登录的用户的公钥,公钥位于id_rsa.pub文件中,把公钥导入/home/git/.ssh/authorized_keys文件里,一行一个。如果没有该文件创建它
git 安装及配置参考
cd /home/git/
mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
3、初始化Git仓库
首先我们选定一个目录作为Git仓库,假定是/usr/local/git/project.git, 在/usr/local/git目录下输入命令:
cd /usr/local
mkdir git
chown git:git git/
cd git
git init --bare project.git
以上命令Git创建一个空仓库,服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git:
chown -R git:git project.git
4、克隆仓库
git clone git@192.168.33.10:/usr/local/git/project.git #可能需要输入git用户密码
git clone root@192.168.33.10:/usr/local/git/project.git #此时输入root密码
192.168.33.10 为 Git 所在服务器 ip
5、禁止git用户shell登录
vi /etc/passwd
修改git的bash 从/bin/bash改为/usr/bin/git-shell
6、基于Nginx配置http(s)
安装相关依赖
如果希望用nginx做webserver,则需要另外的fcgiwrap以及spawn-fcgi。
具体的逻辑则是:nginx若要支持git-http-backend,需要fcgiwarp的帮助。而如果希望fcgiwarp以daemon方式运行,则需要spawn-fcgi
yum install fcgi-devel spawn-fcgi
git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
autoreconf -i
./configure
make && make install
借用github上别人写的启动脚本,按照自己的实际环境,修改好放置/etc/init.d/下
#!/bin/sh
#
# git-fcgi The Git HTTP/FastCGI server
#
# chkconfig: - 80 20
# processname: git-fcgi
# description: Git HTTP/FastCGI server
# pidfile: /var/run/git-fcgi.pid
### BEGIN INIT INFO
# Provides: git-fcgi
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop Git HTTP/FastCGI server
### END INIT INFO
# Source function library.
. /etc/init.d/functions
# Config & Vars
prog=git-fcgi
childs=1
pidfile=/var/run/git-fcgi.pid
lockfile=/var/lock/subsys/git-fcgi
sockfile=/var/run/git-fcgi.sock
sockmode=0700;
sockuser=nginx
sockgroup=nginx
proguser=git
proggroup=git
gitexec=/usr/libexec/git-core/git-http-backend
fcgiexec=/usr/local/sbin/fcgiwrap
spawnexec=/usr/bin/spawn-fcgi
progexec="${spawnexec} -u ${proguser} -g ${proggroup} -U ${sockuser} -G ${sockgroup} -P ${pidfile} -s ${sockfile} -M ${sockmode} -- ${fcgiexec} -f -c ${childs} -p ${gitexec}"
RETVAL=0
# Functions
start() {
echo -n $"Starting ${prog}: "
[ -n "${sockfile}" -a -S "${sockfile}" ] && rm -f ${sockfile}
daemon "${progexec} > /dev/null"
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping ${prog}: "
[ -n "${sockfile}" -a -S "${sockfile}" ] && rm -f ${sockfile}
killproc -p ${pidfile} ${prog}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
return $RETVAL
}
restart() {
stop
start
}
reload() {
restart
}
force_reload() {
restart
}
rh_status() {
status -p ${pidfile} ${prog}
}
# Main
case "$1" in
start)
rh_status > /dev/null 2>&1 && exit 0
start
;;
stop)
stop
;;
status)
rh_status
RETVAL=$?
;;
restart)
restart
;;
reload)
reload
;;
force-reload)
force_reload
;;
condrestart|try-restart)
if rh_status > /dev/null 2>&1; then
restart
fi
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|force_reload|condrestart|try-restart|status|help}"
RETVAL=2
esac
exit $RETVAL
<p></p>
chmod u+x /etc/init.d/git-fcgi
service git-fcgi start
chkconfig git-fcgi on
#将git仓库的所属权限修改为$proguser
chown -R git.git yougit_dir
7、配置nginx
#Git nginx conf
server {
listen 80;
server_name yougit.git.com;
#return 301 https://$server_name$request_uri;
root /data/git/;
error_log /var/log/nginx/git.error.log warn;
access_log /var/log/nginx/git.access.log main;
location ~ ^/([^/]+\.git)(/.*|$) {
include fastcgi_params;
fastcgi_param PATH_INFO $uri;
fastcgi_param REMOTE_USER $remote_user;
fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
fastcgi_param GIT_PROJECT_ROOT $document_root;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_pass unix:/var/run/git-fcgi.sock;
}
}
现在,你可以使用git clone http://xxx.xxx/**.git 的方式操作了。
尾巴
若需要使用验证,可使用htpasswd生成用户名以及密码,再配置到nginx的配置文件中。若仓库接收git push时报错403,可在仓库中设置
git config http.receivepack true
参考资料
https://www.yvanz.com/2016/06/01/Git-server-with-http(s)-protocol.html