1. 程式人生 > 其它 >git伺服器搭建

git伺服器搭建

1、 首先檢查電腦是否已安裝git遠端服務,如沒有Git服務請執行第三步

rpm -qa git

2、 如果執行完第一步發現系統安裝了git 服務,請執行此步驟

[root@iZbp159egvvktszc82dr5xZ ~]# yum remove git
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package git.x86_64 0:1.8.3.1-23.el7_8 will be erased
--> Processing Dependency: git = 1.8.3.1-23.el7_8 for package: perl-Git-1.8.3.1-23.el7_8.noarch
--> Running transaction check
---> Package perl-Git.noarch 0:1.8.3.1-23.el7_8 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================
 Package               Arch                Version                         Repository          Size
====================================================================================================
Removing:
 git                   x86_64              1.8.3.1-23.el7_8                @base               22 M
Removing for dependencies:
 perl-Git              noarch              1.8.3.1-23.el7_8                @base               57 k

Transaction Summary
====================================================================================================
Remove  1 Package (+1 Dependent package)

Installed size: 22 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : perl-Git-1.8.3.1-23.el7_8.noarch                                                 1/2
  Erasing    : git-1.8.3.1-23.el7_8.x86_64                                                      2/2
  Verifying  : git-1.8.3.1-23.el7_8.x86_64                                                      1/2
  Verifying  : perl-Git-1.8.3.1-23.el7_8.noarch                                                 2/2

Removed:
  git.x86_64 0:1.8.3.1-23.el7_8

Dependency Removed:
  perl-Git.noarch 0:1.8.3.1-23.el7_8

Complete!

3、 建立Git安裝目錄

mkdir -v /usr/local/git

4、 下載Git原始碼安裝包

最新版本地址:https://github.com/git-for-windows/git/releases/

wget https://codeload.github.com/git/git/tar.gz/v2.17.1

5、 解壓Git原始碼包

tar -zxvf git-2.17.1.tar.gz
[root@iZbp159egvvktszc82dr5xZ git]# ls
git-2.17.1  git-2.17.1.tar.gz

6、 進入解壓後的目錄開始進行編譯操作,並指定Git安裝路徑

[root@iZbp159egvvktszc82dr5xZ git]# cd git-2.17.1
[root@iZbp159egvvktszc82dr5xZ git-2.17.1]# make prefix=/usr/local/git all

重新編譯的過程可能遇到的錯誤

在使用命令 make 重新編譯 Git 的某些原始檔時,你可能會遇到以下這些錯誤:

錯誤 1 - FATAL ERROR: OPENSSL/SSL.H: NO SUCH FILE OR DIRECTORY
GIT_VERSION = 2.27.0
make: curl-config: Command not found
    * new build flags
    CC fuzz-commit-graph.o
In file included from commit-graph.h:4:0,
                 from fuzz-commit-graph.c:1:
git-compat-util.h:297:25: fatal error: openssl/ssl.h: No such file or directory
 #include <openssl/ssl.h>
                         ^
compilation terminated.
make: *** [fuzz-commit-graph.o] Error 1
1234567891011

使用包管理器檢視與 openssl 相關的資訊:

# rpm --query --all | grep openssl
openssl-libs-1.0.2k-19.el7.x86_64
openssl-1.0.2k-19.el7.x86_64
xmlsec1-openssl-1.2.20-7.el7_4.x86_64
1234

這是缺少 openssl-devel 導致的,執行命令 yum install --assumeyes openssl-devel 安裝即可。

錯誤 2 - FATAL ERROR: CURL/CURL.H: NO SUCH FILE OR DIRECTORY
... 省略部分資訊
In file included from http.c:2:0:
http.h:6:23: fatal error: curl/curl.h: No such file or directory
 #include <curl/curl.h>
                       ^
compilation terminated.
make: *** [http.o] Error 1
1234567

使用包管理器檢視與 curl 相關的資訊:

# rpm --query --all | grep curl
curl-7.29.0-54.el7.x86_64
libcurl-7.29.0-54.el7.x86_64
python-pycurl-7.19.0-19.el7.x86_64
1234

這是缺少 curl-devel 導致的,執行命令 yum install --assumeyes curl-devel 安裝即可。

錯誤 3 - FATAL ERROR: EXPAT.H: NO SUCH FILE OR DIRECTORY
... 省略部分資訊
http-push.c:22:19: fatal error: expat.h: No such file or directory
 #include <expat.h>
                   ^
compilation terminated.
make: *** [http-push.o] Error 1
123456

使用包管理器檢視與 expat 相關的資訊:

# rpm --query --all | grep expat
expat-2.1.0-10.el7_3.x86_64
12

這是缺少 expat-devel 導致的,執行命令 yum install --assumeyes expat-devel 安裝即可。

7、當Git原始碼編譯操作完成後,執行安裝操作

[root@iZbp159egvvktszc82dr5xZ git-2.17.1]# make prefix=/usr/local/git install

8、安裝完Git 服務後,配置環境變數

vim /etc/profile

新增如下欄位:

export GIT_HOME=/usr/local/git
export PATH=$PATH:${GIT_HOME}/bin

配置完成後,過載配置檔案:

source /etc/profile

9、驗證Git 版本號

[root@iZbp159egvvktszc82dr5xZ git-2.17.1]# git --version
git version 2.17.1

10、將git設定為預設路徑,不然後面克隆時會報錯

[root@iZbp159egvvktszc82dr5xZ git-2.17.1]# ln -s /usr/local/git/bin/git-upload-pack /usr/bin/git-upload-pack
[root@iZbp159egvvktszc82dr5xZ git-2.17.1]# ln -s /usr/local/git/bin/git-recive-pack /usr/bin/git-recive-pack

至此,Git就安裝完成。

11、建立一個git使用者組和使用者,用來執行git服務

# groupadd git
# useradd git -g git
# passwd git  #引數是使用者名稱
# su - git  //切換git使用者

【注】最好切換到git使用者 不然後面新建的git倉庫都要改許可權

11、建立證書登入

在Git伺服器上首先需要將/etc/ssh/sshd_config中將RSA認證開啟

[root@iZbp159egvvktszc82dr5xZ git-2.17.1]# vim /etc/ssh/sshd_config

這裡我們可以看到公鑰存放在 .ssh/authorized_keys 檔案中。

所以我們在/home/git下建立.ssh目錄,然後建立authorized_keys檔案,並將剛生成的公鑰匯入進去。

# cd /home/git/
# mkdir .ssh #新建資料夾
# chmod 700 .ssh 
# touch .ssh/authorized_keys  #新建檔案
# chmod 600 .ssh/authorized_keys
[root@iZbp159egvvktszc82dr5xZ git]# ls -al
total 36
drwx------  4 git  git  4096 Sep  6 14:50 .
drwxr-xr-x. 4 root root 4096 Sep  6 13:46 ..
-rw-------  1 git  git   611 Sep  7 08:56 .bash_history
-rw-r--r--  1 git  git    18 Apr  1  2020 .bash_logout
-rw-r--r--  1 git  git   193 Apr  1  2020 .bash_profile
-rw-r--r--  1 git  git   231 Apr  1  2020 .bashrc
drwxrwxr-x  4 git  git  4096 Sep  6 14:30 data
drwx------  2 git  git  4096 Sep  6 14:50 .ssh
-rw-------  1 git  git   889 Sep  6 14:50 .viminfo

12、將客戶端公鑰寫入到authorized_keys檔案中

12.1生成公鑰

12.2寫入伺服器

windows預設生成在C:\Users\Administrator.ssh資料夾中,將id_rsa.pub檔案內容vim authorized_keys,儲存退出

此時,使用windows終端,即可用SSH直連伺服器

13、建立裸倉庫

完成以上操作後,在服務端建立git倉庫的目錄:

[root@localhost ~]# mkdir /data/gitroot
[root@localhost ~]# cd /data/gitroot
[root@localhost /data/gitroot]#
[root@localhost /data/gitroot]# git init --bare sample.git
Initialized empty Git repository in /data/gitroot/sample.git/
##初始化空的 Git 版本庫於 /data/gitroot/sample.git/
[root@localhost /data/gitroot]# ls
sample.git
[root@localhost /data/gitroot]# chown -R git.git sample.git