git原生服务器的构建

两种形式:

       1.使用git-daemon程序,监听9418端口,用git协议下载;

       2.使用http|https协议,在http程序中下载;

 

1.使用git-daemon构建git原生服务器(git协议只能下载,不能上传):

 

① 用yum安装git-daemon程序;

[root@www ~]# yum install git-daemon
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * epel: mirrors.ustc.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 git-daemon.x86_64.0.1.8.3.1-20.el7 将被 安装
--> 正在处理依赖关系 git = 1.8.3.1-20.el7,它被软件包 git-daemon-1.8.3.1-20.el7.x86_64 需要
--> 正在检查事务
---> 软件包 git.x86_64.0.1.8.3.1-13.el7 将被 升级
--> 正在处理依赖关系 git = 1.8.3.1-13.el7,它被软件包 perl-Git-1.8.3.1-13.el7.noarch 需要
---> 软件包 git.x86_64.0.1.8.3.1-20.el7 将被 更新
--> 正在检查事务
---> 软件包 perl-Git.noarch.0.1.8.3.1-13.el7 将被 升级
---> 软件包 perl-Git.noarch.0.1.8.3.1-20.el7 将被 更新
--> 解决依赖关系完成
 
依赖关系解决
 
===================================================================================================================
 Package                    架构                   版本                              源                       大小
===================================================================================================================
正在安装:
 git-daemon                 x86_64                 1.8.3.1-20.el7                    updates                 403 k
为依赖而更新:
 git                        x86_64                 1.8.3.1-20.el7                    updates                 4.4 M
 perl-Git                   noarch                 1.8.3.1-20.el7                    updates                  55 k
 
事务概要
===================================================================================================================
安装  1 软件包
升级           ( 2 依赖软件包)
 
总下载量:4.8 M
Is this ok [y/d/N]: y
Downloading packages:
updates/7/x86_64/prestodelta                                                                | 173 kB  00:00:00    
Delta RPMs reduced 4.4 M of updates to 2.7 M (38% saved)
(1/3): perl-Git-1.8.3.1-13.el7_1.8.3.1-20.el7.noarch.drpm                                   |  28 kB  00:00:00    
(2/3): git-daemon-1.8.3.1-20.el7.x86_64.rpm                                                 | 403 kB  00:00:00    
(3/3): git-1.8.3.1-13.el7_1.8.3.1-20.el7.x86_64.drpm                                        | 2.7 MB  00:00:03    
Finishing delta rebuilds of 1 package(s) (4.4 M)
-------------------------------------------------------------------------------------------------------------------
总计                                                                               282 kB/s | 3.1 MB  00:00:11    
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在更新    : git-1.8.3.1-20.el7.x86_64                                                                      1/5
  正在更新    : perl-Git-1.8.3.1-20.el7.noarch                                                                 2/5
  正在安装    : git-daemon-1.8.3.1-20.el7.x86_64                                                               3/5
  清理        : git-1.8.3.1-13.el7.x86_64                                                                      4/5
  清理        : perl-Git-1.8.3.1-13.el7.noarch                                                                 5/5
  验证中      : git-daemon-1.8.3.1-20.el7.x86_64                                                               1/5
  验证中      : perl-Git-1.8.3.1-20.el7.noarch                                                                 2/5
  验证中      : git-1.8.3.1-20.el7.x86_64                                                                      3/5
  验证中      : git-1.8.3.1-13.el7.x86_64                                                                      4/5
  验证中      : perl-Git-1.8.3.1-13.el7.noarch                                                                 5/5
 
已安装:
  git-daemon.x86_64 0:1.8.3.1-20.el7                                                                              
 
作为依赖被升级:
  git.x86_64 0:1.8.3.1-20.el7                           perl-Git.noarch 0:1.8.3.1-20.el7                         
 
完毕!

 

② 在/var/lib/git目录下创建空git仓库;/var/lib/git默认是git-daemon程序的工作目录;

[root@www ~]# rpm -ql git-daemon
/usr/lib/systemd/system/git.socket
/usr/lib/systemd/system/git@.service
/usr/libexec/git-core/git-daemon
/usr/share/doc/git-daemon-1.8.3.1
/usr/share/doc/git-daemon-1.8.3.1/git-credential-cache--daemon.html
/usr/share/doc/git-daemon-1.8.3.1/git-credential-cache--daemon.txt
/usr/share/doc/git-daemon-1.8.3.1/git-daemon.html
/usr/share/doc/git-daemon-1.8.3.1/git-daemon.txt
/usr/share/man/man1/git-credential-cache--daemon.1.gz
/usr/share/man/man1/git-daemon.1.gz
/var/lib/git
[root@www ~]# cd /var/lib/git/
[root@www git]# ls
[root@www git]# git init --bare
初始化空的 Git 版本库于 /var/lib/git/
[root@www git]# ls
branches  config  description  HEAD  hooks  info  objects  refs
[root@www git]#

 

③ 启动git-daemon程序,查看是否监听9418端口,启动需要使用git.socket文件启动;

[root@www ~]# systemctl start git.socket
[root@www ~]# ss -tnl
State       Recv-Q Send-Q            Local Address:Port                           Peer Address:Port             
LISTEN      0      128                           *:111                                       *:*                 
LISTEN      0      5                 192.168.122.1:53                                        *:*                 
LISTEN      0      128                           *:22                                        *:*                 
LISTEN      0      128                   127.0.0.1:631                                       *:*                 
LISTEN      0      100                   127.0.0.1:25                                        *:*                 
LISTEN      0      128                   127.0.0.1:6010                                      *:*                 
LISTEN      0      128                          :::9418                                     :::*                 
LISTEN      0      128                          :::111                                      :::*                 
LISTEN      0      128                          :::22                                       :::*                  
LISTEN      0      128                         ::1:631                                      :::*                 
LISTEN      0      100                         ::1:25                                       :::*                 
LISTEN      0      128                         ::1:6010                                     :::*

 

④ 在另一台主机上,使用git协议下载此空库;

[root@slave1 ~]# git clone git://172.16.75.3/ gitdir
正克隆到 'gitdir'...
warning: 您似乎克隆了一个空版本库。
[root@slave1 ~]# tree gitdir/ -a
gitdir/
└── .git
    ├── branches
    ├── config
    ├── description
    ├── HEAD
    ├── hooks
    │   ├── applypatch-msg.sample
    │   ├── commit-msg.sample
    │   ├── post-update.sample
    │   ├── pre-applypatch.sample
    │   ├── pre-commit.sample
    │   ├── prepare-commit-msg.sample
    │   ├── pre-push.sample
    │   ├── pre-rebase.sample
    │   └── update.sample
    ├── info
    │   └── exclude
    ├── objects
    │   ├── info
    │   └── pack
    └── refs
        ├── heads
        └── tags
 
10 directories, 13 files

 

2.使用httpd程序构建原生git服务器;

①首先安装httpd程序,假如httpd程序已安装,需要把里边我们创建的虚拟主机配置文件先改名,使其不能被httpd程序识别,否则我们在使用http协议下载git库的时候,会一直报错;

②创建空库对应的目录,并创建原生git服务器的配置文件,并对此目录和所有文件进行apache用户授权,因为在http协议下载时,是httpd程序apache用户在访问;

[root@www ~]# mkdir /var/www/git
[root@www ~]# cd /var/www/git
[root@www git]# git init --bare test.git/
重新初始化现存的 Git 版本库于 /var/www/git/test.git/
[root@www git]# cd
[root@www ~]#
[root@www ~]# chown -R apache. /var/www/git/
[root@www ~]#
[root@www git]# ll /var/www/git/
总用量 0
drwxr-xr-x 7 apache apache 119 12月  6 09:09 test.git

③服务器端设置http.receivepack参数为true,从而可以使服务器接收用户传上的git仓库;

[root@www test.git]# git config http.receivepack true
[root@www test.git]# git config -l
core.repositoryformatversion=0
core.filemode=true
core.bare=true
http.receivepack=true

④创建git虚拟主机配置文件(如果要使用https协议,则把下列内容写至ssl.conf即可,但是客户端需要把http.sslVerify参数设置为false,否则自签证书会出错);

[root@www ~]# cd /etc/httpd/conf.d/
[root@www conf.d]# touch git.conf
[root@www conf.d]# cat git.conf
<VirtualHost *:80>
       ServerName git.ljy.com
       SetEnv GIT_PROJECT_ROOT /var/www/git
       SetEnv GIT_HTTP_EXPORT_ALL
       ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
                                         
       <Directory "/usr/libexec/git-core/">
              Options Indexes ExecCGI
              Require all granted
       </Directory>
                                         
       <LocationMatch "^/git/.*/git-receive-pack$">
              AuthType Basic
              AuthName "Private Git Repository"
              AuthUserFile /etc/httpd/conf/.htpasswd
              Require valid-user
       </LocationMatch>
 
</VirtualHost>

 

ssl自签证书的问题

[root@slave1 ~]# git clone https://172.16.75.3/git/ gitdir
正克隆到 'gitdir'...
fatal: unable to access 'https://172.16.75.3/git/': Peer's Certificate issuer is not recognized.
[root@slave1 ~]# git config --global http.sslVerify false
[root@slave1 ~]#
[root@slave1 ~]# git clone https://172.16.75.3/git/gitdir.git
正克隆到 'gitdir'...
warning: 您似乎克隆了一个空版本库。

 

⑤服务器端添加一个可以识别的用户名和密码供git上传验证,保证安全性;

[root@www conf.d]# htpasswd -c -m /etc/httpd/conf/.htpasswd  tom
New password:
Re-type new password:
Adding password for user tom


⑥客户端可以下载此git仓库;

[root@slave1 ~]# git clone http://172.16.75.3/git/test.git
正克隆到 'test'...
warning: 您似乎克隆了一个空版本库。
[root@slave1 ~]# cd test


 

⑦客户端在此git仓库进行编辑,提交之后,上传至git服务器;

[root@slave1 test]# echo 123 > README
[root@slave1 test]# git add .
[root@slave1 test]# git add README
[root@slave1 test]#
[root@slave1 test]#
[root@slave1 test]# git commit -m "v1.0"
[master(根提交) 34993c8] v1.0
 1 file changed, 1 insertion(+)
 create mode 100644 README
[root@slave1 test]#
[root@slave1 test]#
[root@slave1 test]# tree .git/
.git/
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── index
├── info
│   └── exclude
├── logs
│   ├── HEAD
│   └── refs
│       └── heads
│           └── master
├── objects
│   ├── 19
│   │   └── 0a18037c64c43e6b11489df4bf0b9eb6d2c9bf
│   ├── 34
│   │   └── 993c840c283c1f2672f254f037a009fbcc8780
│   ├── 48
│   │   └── 60c7c0842823dd156d84c0e0c5d4dbf6dd5759
│   ├── info
│   └── pack
└── refs
    ├── heads
    │   └── master
    └── tags
 
15 directories, 21 files
[root@slave1 test]# cat .git/config
[core]
       repositoryformatversion = 0
       filemode = true
       bare = false
       logallrefupdates = true
[remote "origin"]
       url = http://172.16.75.3/git/test.git
       fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
       remote = origin
       merge = refs/heads/master
[root@slave1 test]# git push origin master
fatal: unable to access 'http://172.16.75.3/git/test.git/': The requested URL returned error: 403
[root@slave1 test]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 198 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
Username for 'http://172.16.75.3': tom
Password for 'http://tom@172.16.75.3':
To http://172.16.75.3/git/test.git
 * [new branch]      master -> master

 

⑧查看服务器端是否有此上传的文件,通过配置文件和对应提交的hash码比较即可;

[root@www test.git]# git log
commit 34993c840c283c1f2672f254f037a009fbcc8780
Author: ljy <ljy@123.com>
Date:   Thu Dec 6 09:02:39 2018 +0800
 
v1.0