Gitweb搭建

一、安装Gitweb

shell> yum -y install gitweb

二、安装、配置 Nginx ( 使其支持 CGI )

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

shell > yum -y install fcgi-devel

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

shell > 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="www"
FCGI_GROUP="www"
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 跟 fcgiwrap 脚本路径及 FCGI_GROUP 跟 FCGI_GROUP
# 脚本启动了 5 个 cgi 进程,按需调整

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

shell > chkconfig --level 35 fcgiwrap on

shell > /etc/init.d/fcgiwrap start

三、配置 Nginx vhost

shell > vim /usr/local/nginx/conf/vhost/xxx.uckendo.com.conf

server {
    listen      80;
    server_name xxx.uckendo.com;
    root /var/www/git;

    client_max_body_size 100m;

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

    location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {
        root /home/git;
    }    
   
    location ~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
        root          /home/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  /home/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/gitweb.conf;
        fastcgi_param SCRIPT_FILENAME  /var/www/git/gitweb.cgi;
        fastcgi_param PATH_INFO        $uri;
        include fastcgi_params;
    }
}

四、安装http-tool,并设置密码

shell > htpasswd -c pass.db uckendo  # 添加用户时执行 htpasswd pass.db username

五、Gitweb 在线代码预览

shell > /var/www/git  # Gitweb 路径

shell > ls /var/www/git  # gitweb.cgi 脚本、static 这是个目录,里面是静态文件 css 、js 、logo 等
gitweb.cgi  static

shell > vim /etc/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";

# 注意 Git 仓库路径、静态文件路径

shell > /var/www/git/gitweb.cgi  # 手动执行开是否报错,Status: 200 OK 正常,以下为报错及解决方法
Status: 200 OK
Content-Type: text/html; charset=utf-8

六、配置 Gitweb-theme 主题

# 如果觉得 gitweb 默认样式不好看,可以拿该样式替换

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

@摘自 他人Blog,并根据自身情况进行了修改。

Leave Comment

电子邮件地址不会被公开。 必填项已用*标注