简介:

自己搭建 Git 仓库,实现 SSH 协议、配合 Nginx 实现 HTTP 协议拉取、推送代码。

利用 Nginx 实现 Gitweb 在线浏览代码,使用 Gitweb-theme 更新默认 Gitweb 样式。

一、安装 Git

shell > yum -y install git

shell > git --version  # yum 安装的 git 版本比较低,所以我选择源码编译的方式安装
git version 1.7. shell > yum -y remove git shell > yum -y install perl cpio autoconf tk zlib-devel libcurl-devel openssl-devel expat-devel gettext-devel perl-ExtUtils-MakeMaker shell > cd /usr/local/src; wget https://www.kernel.org/pub/software/scm/git/git-2.10.0.tar.gz shell > tar zxf git-2.10..tar.gz && cd git-2.10. shell > autoconf && ./configure && make && make install shell > git --version
git version 2.10.

二、Git SSH 协议

1、创建 Git 仓库

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

shell > git init --bare sample.git

2、客户端 clone 仓库

shell > git config --global color.ui true
shell > git config --global user.name 'wang'
shell > git config --global user.email 'wangxiaoqiang@bftv.com'

# 初始化 git 客户端

shell > git clone root@192.168.1.22:/data/git/sample.git  # 输入 1.22 的 root 密码
root@192.168.1.22's password: shell > cd sample
shell > cp /etc/passwd .
shell > git add passwd
shell > git commit -m 'add passwd'
shell > git push -u origin master shell > git status
# On branch master
nothing to commit (working directory clean)

# 已经将新增的文件 passwd 提交到了远程 git 仓库的 master 分支

shell > rm -rf sample
shell > git clone root@192.168.1.22:/data/git/sample.git
root@192.168.1.22's password: shell > ls sample
passwd

# 这就实现了 SSH 本地用户授权访问 git 仓库
# 如果 SSH 非标准端口时,需要这样访问:git clone ssh://root@192.168.1.22:16543/data/git/sample.git
# 现在你想把 git 仓库 sample.git 授权别人访问,怎么办?
# 首先你得给每个人创建用户,他们才能 clone 代码,但是不能写入。
# 其次你要把这些用户加入到一个组,然后把 sample.git 属组改为这个组,并且给这个组写入权限。
# 或者创建一个公共用户,修改 git 仓库 sample.git 属主为这个公共用户,然后大家都使用这个用户访问代码库。

三、Git HTTP 协议

1、创建、配置 Git 仓库

shell > useradd -r -s /sbin/nologin www-data  # 创建运行用户

shell > mkdir -p /data/git && chown www-data.www-data /data/git && cd /data/git

shell > git init --bare sample.git && chown -R www-data.www-data sample.git

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

shell > git update-server-info

2、安装、配置 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:
# Default-Stop:
# 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-data"
FCGI_GROUP="www-data"
FORK_NUM=
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
fi $SPAWN_FCGI -u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -P $PIDFILE -F $FORK_NUM -f $DAEMON if [ "$?" != ]; then
echo " failed"
exit
else
echo " done"
fi
;; stop)
echo -n "Stoping $NAME... " PID=`pidof $NAME`
if [ ! -z "$PID" ]; then
kill `pidof $NAME`
if [ "$?" != ]; then
echo " failed. re-quit"
exit
else
rm -f $pid
echo " done"
fi
else
echo "$NAME is not running."
exit
fi
;; status)
PID=`pidof $NAME`
if [ ! -z "$PID" ]; then
echo "$NAME (pid $PID) is running..."
else
echo "$NAME is stopped"
exit
fi
;; restart)
$SCRIPTNAME stop
sleep
$SCRIPTNAME start
;; *)
echo "Usage: $SCRIPTNAME {start|stop|restart|status}"
exit
;;
esac

# 注意 spawn-fcgi 跟 fcgiwrap 脚本路径及 FCGI_GROUP 跟 FCGI_GROUP
# 脚本启动了 5 个 cgi 进程,按需调整

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

shell > chkconfig --level  fcgiwrap on

shell > /etc/init.d/fcgiwrap start

shell > sh auto.sh install nginx  # 安装 nginx

#!/bin/bash

install_nginx(){
yum -y install gcc gcc-c++ wget make pcre-devel zlib-devel openssl-devel id www-data > /dev/null >& || useradd -r -s /sbin/nologin www-data cd /usr/local/src; wget -qc http://nginx.org/download/nginx-1.10.2.tar.gz || exit 9 tar zxf nginx-1.10..tar.gz; cd nginx-1.10.
./configure --prefix=/usr/local/nginx-1.10. \
--with-http_dav_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_degradation_module && make && make install
mkdir /usr/local/nginx-1.10./conf/vhost; mkdir -p /data/logs/nginx
echo "/usr/local/nginx-1.10.2/sbin/nginx" >> /etc/rc.local
} [ $# -lt ] && exit if [ $ == 'install' ];then
case $ in
nginx)
install_nginx ;;
*)
echo 'NULL' ;;
esac
fi

# --with-http_dav_module  不添加该模块无法 git push,请查找 Nginx WebDAV 模块

shell > vim /usr/local/nginx-1.10./conf/vhost/git.server.conf

server {
listen ;
server_name git.server.com;

client_max_body_size 100m;
auth_basic "Git User Authentication";
auth_basic_user_file /usr/local/nginx-1.10./conf/pass.db; location ~ ^.*\.git/objects/([-9a-f]+/[-9a-f]+|pack/pack-[-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;
}
}

# 自己按需修改 nginx.conf,user www-data www-data; 不要忘记加入 include vhost/*.conf;
# 注意 认证文件 pass.db 路径
# 注意 git-http-backend 路径
# 第一个 location 用于静态文件直接读取
# 第二个 location 用于将指定动作转给 cgi 执行
# 根目录指向 git 仓库目录

shell > /usr/local/nginx-1.10./sbin/nginx

shell > yum -y install httpd-tools  # 安装 htpasswd 命令

shell > cd /usr/local/nginx-1.10./conf

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

3、客户端 clone

shell > git config --global color.ui true
shell > git config --global user.name 'wnag'
shell > git config --global user.email 'wangxiaoqiang@bftv.com'

# 初始化 git 客户端

shell > git clone http://git.server.com/sample.git  # 输入用户名、密码即可
正克隆到 'sample'...
Username for 'http://git.server.com': wang
Password for 'http://wang@git.server.com':
warning: 您似乎克隆了一个空仓库。 shell > cd sample.git
shell > cp /etc/passwd .
shell > git add passwd
shell > git commit -m 'add passwd'
shell > git push
Username for 'http://git.server.com': wang
Password for 'http://wang@git.server.com':
对象计数中: , 完成.
Delta compression using up to threads.
压缩对象中: % (/), 完成.
写入对象中: % (/), bytes | bytes/s, 完成.
Total (delta ), reused (delta )
To http://git.server.com/sample.git
* [new branch] master -> master

# htpasswd 创建新用户,就可以协作了。

四、Gitweb 在线代码预览

# 按照我的方法安装 Git ,Gitweb 已经集成了,我们要做的就是找到并配置 Gitweb 。

shell > /usr/local/share/gitweb  # Gitweb 路径

shell > ls /usr/local/share/gitweb  # 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 > /usr/local/share/gitweb/gitweb.cgi # 手动执行开是否报错,Status: OK 正常,以下为报错及解决方法
Status: OK
Content-Type: text/html; charset=utf-
.....

报错 1:

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.

解决方法:

shell > yum -y install perl-CPAN

报错 2:

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.

解决方法:

shell > yum -y install perl-CGI

报错 3:

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.

解决方法:

shell > yum -y install perl-Time-HiRes
shell > vim /usr/local/nginx-1.10./conf/vhost/git.server.conf  # 添加 Gitweb 配置

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

client_max_body_size 100m;
auth_basic "Git User Authentication";
auth_basic_user_file /usr/local/nginx-1.10./conf/pass.db; location ~ ^.*\.git/objects/([-9a-f]+/[-9a-f]+|pack/pack-[-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/gitweb.conf;
fastcgi_param SCRIPT_FILENAME /usr/local/share/gitweb/gitweb.cgi;
fastcgi_param PATH_INFO $uri;
include fastcgi_params;
}
}

# 全局中指定了 gitweb 根目录,最后配置区域加入了 gitweb 的相关配置。
# 现在浏览器访问 git.server.com 就可以预览 Git 代码库及其中的代码了。

五、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 /usr/local/share/gitweb --install # -t 指定 gitweb 根目录,一路 y 即可

# 这时刷新浏览器,就会发现界面的变化

2016-12-14 git push 报错:

error: RPC failed; HTTP  curl  The requested URL returned error:  Request Entity Too Large
fatal: The remote end hung up unexpectedly
fatal: The remote end hung up unexpectedly 解决方法: shell > vim /usr/local/nginx-1.10./conf/vhost/git.server.conf # 或者 nginx.conf 也可以 client_max_body_size 100m;

Git 仓库 SSH、HTTP、Gitweb (Nginx) 乱炖的更多相关文章

  1. Git中如何利用生成SSH个人公钥访问git仓库

    Git中如何利用生成SSH个人公钥访问git仓库方法(这里以coding平台为例): 1. 获取 SSH 协议地址 在项目的代码页面点击 SSH 切换到 SSH 协议, 获得访问地址, 请使用这个地址 ...

  2. Ubuntu16.04 下搭建git服务器及gitweb+nginx配置

    本文转自:http://blog.csdn.net/water_horse/article/details/68958140 1.安装所需软件 fengjk@water:~$ sudo apt-get ...

  3. 解决git仓库从http转为ssh所要处理的问题

    https://www.cnblogs.com/lusecond/p/7607198.html 为了方便,一般我们克隆仓库的时候会选择http或者https协议 git clone https://g ...

  4. 建立多人协作git仓库/git 仓库权限控制(SSH)

    转载文章请保留出处  http://blog.csdn.net/defeattroy/article/details/13775499 git仓库是多人协作使用的,可以基于很多种协议,例如http.g ...

  5. [原创]SSH密钥访问Git仓库配置

    SSH密钥并非为了解决拉取git仓库代码时,需要频繁输入密码的问题. SSH是一种比较安全的协议,可以用来免去远程登录Linux等服务器时需要输入密码的繁琐过程. 命令: ssh user@serve ...

  6. 关于.ssh出错,无法从远程git仓库拉代码

    背景:从windows开发环境改到ubuntu,将windows下生成的.ssh文件拷贝到ubuntu下,以clone远程git仓库代码 错误:sign_and_send_pubkey: signin ...

  7. 使用TortoiseGit,设置ssh方式连接git仓库。

    开始设置之前的准备:建立项目文件夹,初始化git仓库(右键 git  init),右键打开 git bash ,git pull “仓库地址”, 把网站上的仓库代码拉取下来. TortoiseGit使 ...

  8. 服务器上搭建使用SSH账户登录的Git仓库

    1.安装git yum install -y git 2.创建git仓库保存的目录 mkdir /data/git_repo 3.初始化空仓库 cd /data/git_repogit init -- ...

  9. CENTOS下搭建git代码仓库 ssh协议

    centos服务器下搭建git仓库,使用ssh协议管理仓库代码权限    git官网(http://git-scm.com/) 使用ssh协议: 一.安装git,使用yum install git 或 ...

随机推荐

  1. Spring学习10- bean的生命周期(阿里面试题目两次面试均提到)

    找工作的时候有些人会被问道Spring中Bean的生命周期,其实也就是考察一下对Spring是否熟悉,工作中很少用到其中的内容,那我们简单看一下. 在说明前可以思考一下Servlet的生命周期:实例化 ...

  2. linux 命令-case

    case 命令作用: case语句使用于需要进行多重分支的应用情况 case 命令使用场景 在shell中的case结构与C/C++中的switch结构是相同的. 它允许通过判断来选择代码块中多条路径 ...

  3. Xilinx SDK使用教程

    本文参考 Xilinx SDK软件内置的教程,打开方法:打开SDK->Help->Cheet Sheets...->Xilinx SDK Tutorials,这里有6篇文档.本文详细 ...

  4. 免费获取 Kaspersky Small Office Security 90 天授权

    Kaspersky Small Office Security 是卡巴斯基出品的企业版杀毒软件,目前美国官网上官方有赠送活动,能够免费获取 90 天的授权,但必须要使用美国代理. 获取地址:http: ...

  5. 判断是否是iPhone5

    #define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ...

  6. 代码控制 textarea 控件是否为KindEditor 编辑框

    <script charset="utf-8" src="<%:Url.Content("~/UI/Scripts/KindEditor/kinde ...

  7. 使用 某款基于Socks5协议的代理软件 一段时间后 被封锁掉IP的一些技术思考

    由于关键词比较敏感为了不被删除帖子所以文中某软件(上图所示软件)不用全称表示. 去年9月末在 在某国外网站 上弄了一个vpn,在上面安装了某软件,使用起来还是蛮不错的,平时查查英文论文,看看美剧还是比 ...

  8. L3-011 直捣黄龙 (30 分)

    本题是一部战争大片 —— 你需要从己方大本营出发,一路攻城略地杀到敌方大本营.首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营.当这样的路径不唯一时,要求选择可以沿途解放最多城镇 ...

  9. 51nod 算法马拉松4 B递归(YY)

    递归   基准时间限制:1 秒 空间限制:131072 KB 分值: 80 函数f(n,m) { 若n=1或m=1返回a[n][m]; 返回f(n-1,m)异或f(n,m-1); } 读入2<= ...

  10. (原创)AP6212蓝牙模块在am335x控制板上的应用

    主控板wifi模块调通后接着调试蓝牙,经过两周的摸索,终于把蓝牙应用基本建立起来,下面记录下大概流程. 1.硬件管脚设置 static void uart4_init(int evm_id, int ...