git原生服務器的構建html
兩種形式:git
1.使用git-daemon程序,監聽9418端口,用git協議下載;apache
2.使用http|https協議,在http程序中下載;安全
1.使用git-daemon構建git原生服務器(git協議只能下載,不能上傳):bash
① 用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文件啓動;app
[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協議下載此空庫;socket
[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服務器;ide
①首先安裝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