Git 仓库 SSH、HTTP、Gitweb (Nginx) 乱炖
简介:
自己搭建 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) 乱炖的更多相关文章
- Git中如何利用生成SSH个人公钥访问git仓库
Git中如何利用生成SSH个人公钥访问git仓库方法(这里以coding平台为例): 1. 获取 SSH 协议地址 在项目的代码页面点击 SSH 切换到 SSH 协议, 获得访问地址, 请使用这个地址 ...
- Ubuntu16.04 下搭建git服务器及gitweb+nginx配置
本文转自:http://blog.csdn.net/water_horse/article/details/68958140 1.安装所需软件 fengjk@water:~$ sudo apt-get ...
- 解决git仓库从http转为ssh所要处理的问题
https://www.cnblogs.com/lusecond/p/7607198.html 为了方便,一般我们克隆仓库的时候会选择http或者https协议 git clone https://g ...
- 建立多人协作git仓库/git 仓库权限控制(SSH)
转载文章请保留出处 http://blog.csdn.net/defeattroy/article/details/13775499 git仓库是多人协作使用的,可以基于很多种协议,例如http.g ...
- [原创]SSH密钥访问Git仓库配置
SSH密钥并非为了解决拉取git仓库代码时,需要频繁输入密码的问题. SSH是一种比较安全的协议,可以用来免去远程登录Linux等服务器时需要输入密码的繁琐过程. 命令: ssh user@serve ...
- 关于.ssh出错,无法从远程git仓库拉代码
背景:从windows开发环境改到ubuntu,将windows下生成的.ssh文件拷贝到ubuntu下,以clone远程git仓库代码 错误:sign_and_send_pubkey: signin ...
- 使用TortoiseGit,设置ssh方式连接git仓库。
开始设置之前的准备:建立项目文件夹,初始化git仓库(右键 git init),右键打开 git bash ,git pull “仓库地址”, 把网站上的仓库代码拉取下来. TortoiseGit使 ...
- 服务器上搭建使用SSH账户登录的Git仓库
1.安装git yum install -y git 2.创建git仓库保存的目录 mkdir /data/git_repo 3.初始化空仓库 cd /data/git_repogit init -- ...
- CENTOS下搭建git代码仓库 ssh协议
centos服务器下搭建git仓库,使用ssh协议管理仓库代码权限 git官网(http://git-scm.com/) 使用ssh协议: 一.安装git,使用yum install git 或 ...
随机推荐
- 在laravel视图中直接使用{{ csrf_token() }}被翻译成英文显示的处理方法
在表单中加一个input框在放入{{ csrf_token() }}就可以了: 方法如下: <input type="hidden" name="_token&qu ...
- 终于知道为什么我的mysql总是卸载的不干净以及老是找不到my.ini文件
感谢博主: http://blog.sina.com.cn/s/blog_6fc5bfa90100qmr9.html 如果你的电脑里装过MySQL,想再重新安装MySQL的时候可能就会因为前一版本卸载 ...
- jsr303 参考表
下面是主要的验证注解及说明: 注解 适用的数据类型 说明 @AssertFalse Boolean, boolean 验证注解的元素值是false @AssertTrue Boolean, boole ...
- html和js实现滚动条效果
HTML部分 <!-- 遮罩层 --> <div id="zzc" style="z-index:-1;height:100%;width: 100%; ...
- linux 网络测试命令 长期更新
一.网络测试命令 1.测试 网络连接 发送两包后停发 [oracle@hadoop ~]$ PING www.a.shifen.com (() bytes of data. bytes from tt ...
- mac下安装apache tomcat
目录 一. 默认版: 二. 自定义下载配置版: ———————————————————————正文—————————————————————————— 一. 默认版: ##一.mac 自带了apach ...
- iOS笔记之AutoresizingMask
在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. enum { UIViewAutoresi ...
- linux 下mongodb 3.2.5单机版安装
mongodb3.0.x的安装教程网上很多,这里主要介绍3.2.5的安装 linux iso 在\\10.10.10.1\ShareDoc\User\yipengzhi\ISO\Centos7.0 ...
- ballerina 学习二十五 项目docker 部署&& 运行
ballerina 官方提供了docker 的runtime,还是比较方便的 基本项目创建 使用cli创建项目 按照提示操作就行 ballerina init -i 项目结构 添加了dockerfil ...
- [CLPR] 卷积神经网络的结构
本文翻译自: http://www.codeproject.com/Articles/16650/Neural-Network-for-Recognition-of-Handwritten-Digi ...