简介:

自己搭建 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. Cookie是什么?从哪来?存在哪?往哪去?

    什么是cookie? cookie最简单的介绍就是服务器返回的一个字符串信息,只不过我们每次请求都需要把它发送给服务器.以AFN和android-async-http为例子,默认都会把cookie自动 ...

  2. 051——VUE中自定义指令:directive

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. hdu 3696 10 福州 现场 G - Farm Game DP+拓扑排序 or spfa+超级源 难度:0

    Description “Farm Game” is one of the most popular games in online community. In the community each ...

  4. 201621123006 《Java程序设计》第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 List中指定元素的删除(题集题目) 1.1 实验总结.并回答:列举至少2种 ...

  5. 第一个openGL程序

    一.工具的安装 因为要写C++程序,为了便捷,这里我安装的Visual Studio: 免费使用90天!安装好了之后,我们要为我们的开发安装相应的工具集: 因为我是要在windows上跑的,所以选择如 ...

  6. Mysql基本操作(远程登陆,启动,停止,重启,授权)

    1.查看mysql版本 方法一:status; 方法二:select version(); 2.Mysql启动.停止.重启常用命令 a.启动方式 1.使用 service 启动: [root@loca ...

  7. threejs精灵平面Sprite(类似tip效果)

    效果图:   let center = this.cube.position.clone(), size = this.cube.geometry.boundingBox.getSize(), sca ...

  8. Convex 一道阿姆斯特朗回旋好题

    2001年5月8日,阿姆斯特朗(Armstrong, 1929-2013) 教授发明了一种名为“阿姆斯特朗回旋加速喷气式阿姆斯特朗加密”的加密算法,算法从未公开,直至2013阿姆斯特朗教授逝世后,其生 ...

  9. sourcetree回退到历史节点

    1. 原理 原理,我们都知道Git是基于Git树进行管理的,要想要回滚必须做到如下2点: 本地头节点与远端头节点一样(Git提交代码的前提条件):于本地头节点获取某次历史节点的更改.说的有点抽象,以图 ...

  10. 在.NET中实现Actor模型的不同方式

    上周,<实现领域驱动设计>(Implementing Domain-Driven Design)一书的作者Vaughn Vernon,发布了Dotsero,这是一个使用C#编写的.基于.N ...