centos7基于nginx的http协议的git服务器

一、安装git

sudo yum install -y git

二、安装nginx

sudo yum install -y nginx

启动

systemctl start nginx

三、创建http协议的仓库

1.创建git仓库

mkdir -p /srv/depot/test.git
cd /srv/depot/test.git
git init --bare
mv hooks/post-update.sample hooks/post-update
git update-server-info

2.安装nginx的扩展,使它支持cgi

cd /usr/local/src
git clone https://github.com/lighttpd/spawn-fcgi.git
cd spawn-fcgi && ./autogen.sh && ./configure && make && make install

3.安装fcgi-devel

yum -y install fcgi-devel

如果安装fcgi-devel失败,要安装源

rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

4.安装fcgi的管理工具

cd /usr/local/src
git clone https://github.com/gnosek/fcgiwrap.git
cd fcgiwrap && autoreconf -i && ./configure && make && make install

5.配置启动脚本

vim /etc/init.d/fcgiwrap

脚本代码如下

#! /bin/bash
### BEGIN INIT INFO
# Provides:          fcgiwrap
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: FastCGI wrapper
# Description:       Simple server for running CGI applications over FastCGI
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SPAWN_FCGI="/usr/local/bin/spawn-fcgi"
DAEMON="/usr/local/sbin/fcgiwrap"
NAME="fcgiwrap"

PIDFILE="/var/run/$NAME.pid"

FCGI_SOCKET="/var/run/$NAME.socket"
FCGI_USER="git"
FCGI_GROUP="git"
FORK_NUM=5
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
    start)
        echo -n "Starting $NAME... "

        PID=`pidof $NAME`
        if [ ! -z "$PID" ]; then
            echo " $NAME already running"
            exit 1
        fi

        $SPAWN_FCGI -u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -P $PIDFILE -F $FORK_NUM -f $DAEMON

        if [ "$?" != 0 ]; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    stop)
        echo -n "Stoping $NAME... "

        PID=`pidof $NAME`
        if [ ! -z "$PID" ]; then
            kill `pidof $NAME`
            if [ "$?" != 0 ]; then
                echo " failed. re-quit"
                exit 1
            else
                rm -f $pid
                echo " done"
            fi
        else
            echo "$NAME is not running."
            exit 1
        fi
    ;;

    status)
        PID=`pidof $NAME`
        if [ ! -z "$PID" ]; then
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped"
            exit 0
        fi
    ;;

    restart)
        $SCRIPTNAME stop
        sleep 1
        $SCRIPTNAME start
    ;;

    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|status}"
        exit 1
    ;;
esac

脚本上的 :
这两个根据自己系统的文件的位置填写:
SPAWN_FCGI=“/usr/local/bin/spawn-fcgi”
DAEMON=“/usr/local/sbin/fcgiwrap”

这两个要跟nginx的启动运行用户相同,或者组相同,否则会发生502报错,权限拒绝
FCGI_USER=“git”
FCGI_GROUP=“git”

接下来给脚本执行权限,并启动脚本

chmod a+x /etc/init.d/fcgiwrap

chkconfig --level 35 fcgiwrap on

/etc/init.d/fcgiwrap start

6.配置Nginx的server服务 让请求转到上面的脚本

server {
    listen      80;
    server_name xxxx.xxxx.xxxx.xxxx;

    client_max_body_size 100m;
    
    auth_basic "Git User Authentication";#开启权限验证功能
    auth_basic_user_file /www/gitpass.db;#验证的用户名密码文件

    location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {
        root /srv/depot;
    }    
    
    location ~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
        root          /srv/depot;
        fastcgi_pass  unix:/var/run/fcgiwrap.socket;
        fastcgi_connect_timeout 24h;
        fastcgi_read_timeout 24h;
        fastcgi_send_timeout 24h;
        fastcgi_param SCRIPT_FILENAME   /usr/local/git/libexec/git-core/git-http-backend;#这个查找服务器上git的所在位置进行替换
        fastcgi_param PATH_INFO         $uri;
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param GIT_PROJECT_ROOT  /srv/depot;#git仓库所在位置
        fastcgi_param REMOTE_USER $remote_user;
        include fastcgi_params;
    }
}

创建验证的用户名 密码,可以用
htpasswd -c /www/gitpass.db user

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值