阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

利用Nginx搭建HTTP访问的Git服务器

145次阅读
没有评论

共计 7850 个字符,预计需要花费 20 分钟才能阅读完成。

利用 Nginx 搭建 HTTP 访问的 Git 服务器过程记录。搭建 Git 仓库,实现 SSH 协议、配合 Nginx 实现 HTTP 协议拉取、推送代码。利用 Nginx 实现 Gitweb 在线浏览代码,使用 Gitweb-theme 更新默认 Gitweb 样式。

一. 准备工作:

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 'example@example.com'

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

Git 教程系列文章

GitHub 使用教程图文详解  http://www.linuxidc.com/Linux/2014-09/106230.htm 

Git 使用图文详细教程  http://www.linuxidc.com/Linux/2016-11/136781.htm

Ubuntu Git 安装与使用 http://www.linuxidc.com/Linux/2016-11/136769.htm

Git 标签管理详解 http://www.linuxidc.com/Linux/2014-09/106231.htm 

Git 分支管理详解 http://www.linuxidc.com/Linux/2014-09/106232.htm 

Git 远程仓库详解 http://www.linuxidc.com/Linux/2014-09/106233.htm 

Git 本地仓库(Repository)详解 http://www.linuxidc.com/Linux/2014-09/106234.htm 

Git 服务器搭建与客户端安装  http://www.linuxidc.com/Linux/2014-05/101830.htm 

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm 

分享实用的 GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm 

Git 从入门到学会 http://www.linuxidc.com/Linux/2016-10/135872.htm

Git 基本操作详解 http://www.linuxidc.com/Linux/2016-10/135691.htm

Git 创建远程仓库实例  http://www.linuxidc.com/Linux/2017-02/140737.htm

Git 的详细介绍 :请点这里
Git 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-02/140779.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计7850字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中