centos7基於nginx的http協議的git服務器

使用Gitlab有點配置太麻煩,用github或gitee,領導要求內網或私有服務器,也麻煩.因此乾脆本身動手吧. 相對來講比使用gitlab部署要簡單(非docker模式),將就看吧javascript

nginx的安裝就略過啦.
要注意的是編譯安裝nginx,必定要加上--with-http_dav_module不添加該模塊沒法 git push
咱們從git安裝開始.css

安裝git

centos默認帶的是1.7x的,也能夠用.我不當心把它刪了html

yum -y remove git

而後下載了最新的,如今最新的已經到了2.14.2了...java

yum -y install perl cpio autoconf tk zlib-devel libcurl-devel openssl-devel expat-devel gettext-devel perl-ExtUtils-MakeMaker

cd /usr/local/src; wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz

tar zxf git-2.9.5.tar.gz && cd git-2.9.5

autoconf && ./configure

make prefix=/usr/local/git all
make prefix=/usr/local/git install

添加到環境變量nginx

vim /etc/profile

添加這一條git

export PATH="/usr/local/git/bin:$PATH"

當即生效github

source /etc/profile

查看git版本web

git --version

git version 2.9.5docker

好了,咱們作個小動做
將git設置爲默認路徑,否則後面克隆時會報錯shell

ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack 
ln -s /usr/local/git/bin/git-receive-pack /usr/bin/git-receive-pack

Git SSH協議

1.建立 Git 倉庫

mkdir -p /gitServer && cd /gitServer

git init --bare lxm.git

2.本地使用git客戶端鏈接

git clone git@xxxx.xxxx.xxxx:gitServer/lxm.git

輸入git用戶密碼,你應該就能克隆一個空的倉庫下來了
這就實現了SSH本地用戶受權訪問git倉庫
固然,你也能夠提交數據.可是每次都用root,好2B是否是,並且還用的是ssh協議,寫起來就頭疼.咱們要http或https...

繼續...

配置http協議的倉庫

1.修改lxm倉庫配置

cd memory.git && 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

安裝

yum -y install fcgi-devel

安裝fcgi的管理工具

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

配置啓動腳本

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-fcgifcgiwrap腳本路徑及FCGI_GROUPFCGI_GROUP
腳本啓動了5個cgi進程,按需調整
用戶組我這兒直接使用的是nginx的www用戶組,若是你的nginx用的別的組跑的,這兒改改

讓它可執行

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

chkconfig --level 35 fcgiwrap on

/etc/init.d/fcgiwrap start

3.安裝httpd工具包

yum -y install httpd-tools

主要是方便使用htpasswd
若是你不肯安裝它就用如下的方法建立密碼也能夠

vi ~/httppasswd.pl

//寫入下面四行代碼
#!/usr/bin/perl
use strict;
my $pw=$ARGV[0] ;
print crypt($pw,$pw)."\n";

//添加可執行
chmod +x ~/httppasswd.pl

//使用方法
~/httppasswd.pl admin            
adpexzg3FUZAkvim  //密碼admin轉換

echo "user:adpexzg3FUZAkvim" > /www/gitpass.db

好了,咱們繼續剛纔的htpasswd

htpasswd -c /www/gitpass.db user

建立一個帳號並要求你輸入密碼,我這兒就memory,密碼就123456
而後建立一個nginx站點

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 /gitServer;
    }    
    
    location ~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
        root          /gitServer;
        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;
        fastcgi_param PATH_INFO         $uri;
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param GIT_PROJECT_ROOT  /gitServer;
        fastcgi_param REMOTE_USER $remote_user;
        include fastcgi_params;
    }
}

這樣簡單的配置後,你應該就可使用 git clone http://xxxx.xxxx.xxx.xxx/lxm.git 來克隆倉庫了
注意認證文件gitpass.db路徑
注意git-http-backend路徑 , 到git的安裝目錄下去找
第一個location用於靜態文件直接讀取
第二個location用於將指定動做轉給cgi執行
根目錄指向git倉庫目錄

配置http在線瀏覽 Gitweb

這個Gitweb其實咱們在編譯安裝git的時候他也給咱們安裝了,在git的安裝目錄下有個share,裏面有驚喜
不廢話了,直接繼續

vim /etc/gitweb.conf

寫入

# path to git projects (<project>.git)
$projectroot = "/gitServer";

# directory to use for temp files
$git_temp = "/tmp";

# target of the home link on top of all pages
$home_link = $my_uri || "/";

# html text to include at home page
$home_text = "indextext.html";

# file with project list; by default, simply scan the projectroot dir.
$projects_list = $projectroot;

# javascript code for gitweb
$javascript = "static/gitweb.js";

# stylesheet to use
$stylesheet = "static/gitweb.css";

# logo to use
$logo = "static/git-logo.png";

# the 'favicon'
$favicon = "static/git-favicon.png";

手動執行一下看報錯不

/usr/local/git/share/gitweb/gitweb.cgi

固然報錯啦...是否是,由於你有一堆東西沒裝.

yum -y install perl-CPAN perl-CGI perl-Time-HiRes

這三貨安裝了應該就不會報錯了
而後咱們把剛纔的nginx站點的配置再改改

server {
    listen      80;
    server_name xxxx.xxxx.xxxx.xxxx;
    root        /usr/local/git/share/gitweb;

    client_max_body_size 100m;

    auth_basic "Git User Authentication";
    auth_basic_user_file /www/gitpass.db;
location ~ \.(cgi|pl).*$ {  

            gzip off;  

            fastcgi_pass unix:/var/run/fcgiwrap.socket;  

            fastcgi_param  SCRIPT_FILENAME    /var/www/git/gitweb.cgi;    

            fastcgi_param  SCRIPT_NAME        gitweb.cgi;    

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  

            include fastcgi_params;  

      }  

    location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {
        root /www/git;
    }

    location ~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
        root          /www/git;
        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;
        fastcgi_param PATH_INFO           $uri;
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param GIT_PROJECT_ROOT    /gitServer;
        fastcgi_param REMOTE_USER $remote_user;
        include fastcgi_params;
    }

    try_files $uri @gitweb;

    location @gitweb {
        fastcgi_pass  unix:/var/run/fcgiwrap.socket;
        fastcgi_param GITWEB_CONFIG    /etc/gitweb.conf;
        fastcgi_param SCRIPT_FILENAME  /var/www/git/gitweb.cgi;
        fastcgi_param PATH_INFO        $uri;
        include fastcgi_params;
    }
}

好了,記得nginx reload一下呀.而後你就能夠用瀏覽器瀏覽代碼倉庫了
大概樣子應該以下

收工. 別問我爲何瀏覽器打開提示你輸入帳號密碼,你說咧..你說咧 nginx的這貨你當它是吃的呀auth_basic_user_file

Gitweb-theme樣式

若是以爲 gitweb 默認樣式很差看,能夠拿該樣式替換

cd /usr/local/src
git clone https://github.com/kogakure/gitweb-theme.git
cd gitweb-theme
./setup -vi -t /var/www/git  --install  # -t 指定 gitweb 根目錄,一路 y 便可

清空下緩存吧

備註一些git的常規配置

配置git的最低速度和最低速度時間:
git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999
git config --global http.postBuffer 5242880000

快速添加倉庫的shell腳本

#!/bin/bash
read -p "請輸入倉庫名稱:" name
if [ ! -n "$name" ];then
  echo "不能爲空"
else
  cd /www/git
  git init --bare $name.git
  chown -Rf www:www $name.git
  cd $name.git && mv hooks/post-update.sample hooks/post-update
  echo $name > description
  git update-server-info

  echo "git remote add origin http://xx.xx.xx/$name.git"
  echo "git push -u origin master"
fi

通常性錯誤

錯誤1.push502
push的時候如何提示502,可是網站確正常訪問.請檢查你的git目錄是否是git權限

報錯 2:

Can't locate CPAN.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendorperl /usr/share/perl5/vendorperl /usr/lib/perl5 /usr/share/perl5 .) BEGIN failed--compilation aborted.

解決方法:

yum -y install perl-CPAN

報錯 3:

Can't locate CGI.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendorperl /usr/share/perl5/vendorperl /usr/lib/perl5 /usr/share/perl5 .) BEGIN failed--compilation aborted.

解決方法:

yum -y install perl-CGI

報錯 4:

Can't locate Time/HiRes.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/share/gitweb/gitweb.cgi line 20.

解決方法:

yum -y install perl-Time-HiRes

報錯 5:

error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large

解決方法:nginx站點配置文件中添加或修改client_max_body_size的值

client_max_body_size 100m;
相關文章
相關標籤/搜索