简介:

自己搭建 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. 探究JS中对象的深拷贝和浅拷贝

    深拷贝和浅拷贝的区别 在讲深拷贝和浅拷贝的区别之前,回想一下我们平时拷贝一个对象时是怎么操作的?是不是像这样? var testObj1 = {a: 1, b:2}, testObj2=testObj ...

  2. 转载 Linux top命令详解

    TOP命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况. TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止 ...

  3. 利用javascript:void(0)制作假的提交按钮替代button

    在写html页面,我们很自然的在表单提交的地方采用button来作为提交按钮,但是,用<button type=”button”>按钮</button>作为提交代码会有个问题, ...

  4. Alpha阶段第1周Scrum立会报告+燃尽图 05

    作业要求与https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246相同 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 周 ...

  5. Go安装一些第三方库

    原文链接:https://javasgl.github.io/go-get-golang-x-packages/ 侵权联系删除! go在go get 一些 package时候的会由于众所周知的原因而无 ...

  6. 斯特灵(Stirling)数

    http://zh.wikipedia.org/wiki/%E6%96%AF%E7%89%B9%E7%81%B5%E6%95%B0 第一类:n个元素分成k个非空循环排列(环)的方法总数 递推式:s(n ...

  7. CIE-LUV是什么颜色特征

    参考文献:维基百科 a simple-to-compute transformation of the 1931 CIE XYZ color space, but which attempted pe ...

  8. DesignPattern(一)设计模式的六个基本原则

    使用设计模式的根本原因是适应变化,提高代码复用率,使软件更具有可维护性和可扩展性.并且,在进行设计的时候,也需要遵循以下几个原则:单一职责原则.开放封闭原则.里氏代替原则.依赖倒置原则.接口隔离原则. ...

  9. TCP拥塞控制机制

     研究TCP的拥塞机制,不仅仅是想了解TCP如何的精巧,更多的是领悟其设计思想,即在一般情况下,我们该怎样处理问题.   一.拥塞的发生与其不可避免    拥塞发生的主要原因:在于网络能够提供的资源不 ...

  10. 常见web安全攻防总结

    Web 安全的对于 Web 从业人员来说是一个非常重要的课题 , 所以在这里总结一下 Web 相关的安全攻防知识,希望以后不要再踩雷,也希望对看到这篇文章的同学有所帮助.今天这边文章主要的内容就是分析 ...