搭建GIT基于Nginx的私服

2018/07/19

依赖

yum install -y git gitweb spawn-fcgi fcgi-devel fcgi

安装fcgiwrap

https://codeload.github.com/gnosek/fcgiwrap/legacy.tar.gz/master
cd fcgiwrap
autoreconf -i 
Configure
Make
make install

检查以下必要文件是否存在

ll /etc/init.d/spawn-fcgi
ll /usr/bin/spawn-fcgi
ll /var/www/git/gitweb
ll /etc/gitweb.conf
ll /etc/sysconfig/spawn-fcgi

vim

$projectroot = "/home/git/repositories";

@git_base_url_list= ("git://192.168.0.101", "http://192.168.0.101:81");
$git_temp = "/tmp";
$home_text = "indextext.html";
@stylesheets = ("gitweb.css");
$javascript = "gitweb.js";
@diff_opts = ();
$feature{pathinfo}{default} = [1];
$feature{'highlight'}{'default'} = [1];

启动spawn-fcgi

chkconfig --levels 2345 spawn-fcgi on
/etc/init.d/spawn-fcgi start
服务成功启动后会生成/var/run/fcgiwrap.socket文件。

初始化GIT服务

vim /etc/sudoers
sudo -H -u git gitosis-init < ~/tmp/id_rsa.pub
useradd gitpasswd git
su git #切换到git用户
mkdir repositories # 会在/home/git目录下建立repositories目录
#开始建立git的root repositories
ssh-keygen -t rsa
git gitosis-init < ~/.ssh/id_rsa.pub

以上做的事情就是:

  1. 生成的gitosis-admin为Git的用户访问权限管理库,gitosis通过这个git库来管理所有git库的访问权限。
  2. 通过执行初始化,该公钥的拥有者就能修改用于配置gitosis的那个特殊Git仓库了

因为git中的密钥就是通过gitosis这个特殊的repo来管理的。有了它我们就可以管理我们的GIT,包括建子项目什么的了。

建子项目

su git
cd /data/gitrepo
git --bare init hello.git
sudo chmod 777 /data/gitrepo/hello.git –R

测试

git clone git@192.168.56.101:/data/hello.git
如果SSH用的不是默认的22端口,则需要使用以下的命令(假设SSH端口号是7700):
git clone ssh://git@192.168.56.101:7700/data/hello.git
cd hello
echo hdfs > readme
git add . # 代表把当前目录内所有东西进行远程推送
git commit -a -m “init helloworld“ # 代表commit所有之前add过的东东,并且带上commit时的注释
git remote add origin git@192.168.1.101:/home/git/repositories/hello.git # 建立远程GIT连接
git push origin master # 正式推送

配置GITWEB + NGINX

vim /etc/nginx/nginx.conf
  user  nginx;
  worker_processes  1;

  error_log  /var/log/nginx/error.log warn;
  pid        /var/run/nginx.pid;

  events {
    worker_connections  1024;
  }

  http {
    server {
      error_log logs/git.error.log;
      access_log logs/git.access.log;
      listen       81;
      server_name  192.168.20.65;
      index       gitweb.cgi;
      root /data/gitrepo;
    location ~ \.(cgi|pl).*$ {
      gzip off;
      fastcgi_pass unix:/var/run/fcgiwrap.socket;
      fastcgi_param  SCRIPT_FILENAME    /var/www/git/gitweb.cgi;  
      fastcgi_param  SCRIPT_NAME        gitweb.cgi;  
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include /etc/nginx/fastcgi_params;
    }
    location ~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {
      root /data/gitrepo;
    }
    location ~ ^.*\.png {
      root /var/www/git;
    }
    location ~ ^.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
      root /data/gitrepo;
      fastcgi_param QUERY_STRING $query_string;
      fastcgi_param SCRIPT_FILENAME /usr/libexec/git-core/git-http-backend;
      fastcgi_param GIT_HTTP_EXPORT_ALL true;
      fastcgi_param GIT_PROJECT_ROOT /home/git/repositories;
      fastcgi_param PATH_INFO $uri;
      include /etc/nginx/fastcgi_params;
      fastcgi_pass unix:/var/run/fcgiwrap.socket;
    }
    try_files $uri @gitweb;
      location @gitweb {
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
        fastcgi_param SCRIPT_FILENAME /var/www/git/gitweb.cgi;
        fastcgi_param PATH_INFO $uri;
        fastcgi_param GITWEB_CONFIG /etc/gitweb.conf;
        include /etc/nginx/fastcgi_params;
      }
    }
  }

启动nginx

常见问题

问题1

客户端通过ECLIPSE的JGIT连上GIT的远程服务后会发送一个git-upload指令。

同时服务端会回一个git-receive给到客户端做“接收”请求的准备,但是git服务默认对于这个receive的值为false,因此如果你没有做特殊处理后,在eclipse连上git后会发生一个can not find git-receive这样的错误,

解决办法

su git
cd /home/git/repositories/hello.git
git config --file config http.receivepack true

其他问题

git clone https://github.com/tv42/gitosis.git
cd gitosis
python setup.py install

参考网址

https://blog.csdn.net/lifetragedy/article/details/51658266

目录