歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> 利用Nginx搭建Http訪問的Git服務器,nginx搭建git服務器

利用Nginx搭建Http訪問的Git服務器,nginx搭建git服務器

日期:2017/3/3 17:31:04   编辑:學習Linux

利用Nginx搭建Http訪問的Git服務器,nginx搭建git服務器


熱度1 評論 246 www.BkJia.Com 網友分享於: 2017-02-18 04:02:41 浏覽數5286次

利用Nginx搭建Http訪問的Git服務器,nginx搭建git服務器


本文參考了http://www.cnblogs.com/wangxiaoqiangs/p/6179610.html, 自己操作中收獲一些錯誤心得.記下以備用.

一. 准備工作:

  1. 下載nginx並安裝

    推薦到nginx官方網站下載並安裝,有很詳細的教程. 參考資料: http://nginx.org/en/linux_packages.html

    (1). 編輯repo文件,這裡以64位的CentOS 7為示例:

      

> vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1

    (2). 保存退出後,使用yum安裝.可以選擇把動態模塊也安裝上,具體參見上述站點

> yum install nginx -y

  2. 下載git並安裝

    (1).不推薦使用yum安裝的git版本,過低了.到github下載最新的git源碼. 下載地址: https://github.com/git/git/releases

> yum -y remove git

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

> cd /usr/local/src; wget https://github.com/git/git/archive/v2.11.1.tar.gz

> tar zxf v2.11.1.tar.gz && cd git-2.11.1

> autoconf && ./configure && make && make install

> git --version

    (2). 這個時候,git安裝好了,可以選擇較高的穩定版本.我用的時候2.11.1

  3. 下載spawn-fcgi, fcgi-devel, fcgiwrap並安裝

    (1). 安裝spawn-fcgi.github地址: https://github.com/lighttpd/spawn-fcgi

      這裡需要注意的是,如果你沒有安裝前面的automake和gcc,請這裡一定要把這些依賴安裝好.

> cd /usr/local/src;

> git clone https://github.com/lighttpd/spawn-fcgi.git

> cd spawn-fcgi && ./autogen.sh && ./configure && make && make install

    (2). 安裝fcgi-devel.

      安裝前,需要先安裝epel源,不然安裝不了fcgi-devel

> yum -y install epel-release

> yum -y install fcgi-devel

    (3). 安裝fcgiwrap. GitHub地址: https://github.com/gnosek/fcgiwrap

> cd /usr/local/src

> git clone https://github.com/gnosek/fcgiwrap.git

> cd fcgiwrap && autoreconf -i && ./configure && make && make install

      

二. 配置

  1. 添加Git的運行用戶, Git倉庫初始化

> useradd -r -s /sbin/nologin git

> mkdir -p /data/git && cd /data/git

> git init --bare repo.git && chown -R git.git /data/git

> cd repo.git && mv hooks/post-update.sample hooks/post-update

> git update-server-info

  2. 編寫fcgiwrap啟動腳本

> vi /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=15
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

   注意其中"FCGI_USER"和"FCGI_GROUP"以及"FORK_NUM",分別為fastcgi運行的用戶,組以及進程數(進程數按需調整).需要與之後配置的nginx的worker用戶一樣.

   記得修改一下讀寫權限以及設置腳本為開機啟動.然後我們啟動fastcgi

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

> chkconfig --level 35 fcgiwrap on

> /etc/init.d/fcgiwrap start

  3. nginx配置. yum安裝的nginx已經默認配置了WebDAV模塊,所以不用麻煩了.如果發現沒有WebDAV模塊的功能,可以參考nginx的官方文檔中Dynamic Modules的說明: http://nginx.org/en/docs/ngx_core_module.html#load_module

    (1). 創建授權文件夾以及git的nginx設置文件

> mkdir -p /usr/local/nginx/config

> vi /etc/nginx/conf.d/git.conf

      內容如下:

server {
    listen      80;
    server_name gitServer;
    root /usr/local/share/gitweb;

    client_max_body_size 100m;

    auth_basic "Git User Authentication";
    auth_basic_user_file /usr/local/nginx/config/pass.db;

    location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {
        root /data/git;
    }    
    
    location ~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
        root          /data/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/libexec/git-core/git-http-backend;
        fastcgi_param PATH_INFO         $uri;
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param GIT_PROJECT_ROOT  /data/git;
        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/git/gitweb.conf;
        fastcgi_param SCRIPT_FILENAME  /usr/local/share/gitweb/gitweb.cgi;
        fastcgi_param PATH_INFO        $uri;
        include fastcgi_params;
    }
}

    (2). 修改/etc/nginx/nginx.conf中的worker進程所有者.

# 將此處的nginx用戶修改為git用戶,以保證能調用到fastcgi(需要和fcgiwrap腳本中的FCGI_USER保持一致)
user git; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }

  4. 安裝http-tools並添加認證用戶

> yum -y install httpd-tools

> cd /usr/local/nginx/config

> htpasswd -c pass.db guestUser

> git config --global color.ui true

> git config --global user.name 'git'

> git config --global user.email '[email protected]'

  5. 配置gitweb,首先要確定默認安裝的gitweb(采用源碼安裝git才會有)是否存在

> find /usr/local/share -name gitweb.cgi

> cd /usr/local/share/gitweb && ll /usr/local/share/gitweb

> vi /etc/git/gitweb.conf

    gitweb.conf的配置內容如下:

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

# 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";

三. 啟動nginx,fastcgi

> nginx -t

> systemctl start nginx

> /etc/init.d/fcgiwrap start

四. 問題收集:

  1. 訪問http://hostname/repo.git出現502錯誤,nginx錯誤日志中出現:connect() to unix:/var/run/fcgiwrap.socket failed (13: Permission denied) while connecting to upstream

    解決方法: 檢查selinux是否開啟,如果開啟,請關閉或者配置策略使其能被訪問.

  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

五. Gitweb-theme 樣式

  如果覺得 gitweb 默認樣式不好看,可以拿該樣式替換

  

> cd /usr/local/src

> git clone https://github.com/kogakure/gitweb-theme.git

> cd gitweb-theme
# -t 指定 gitweb 根目錄,一路 y 即可
> ./setup -vi -t /usr/local/share/gitweb --install

http://www.bkjia.com/Linuxjc/1193931.html TechArticle

Copyright © Linux教程網 All Rights Reserved