環境:CentOS 7 *2 192.168.11.{29,30}
Git 版本1.8.3
Httpd 版本2.4.5
1、服務端centos7_30配置
[root@CentOS7_30 ~]# getenforce #查看selinux Enforcing [root@CentOS7_30 ~]# setenforce 0 #設定爲0寬容模式 [root@CentOS7_30 ~]# vim /etc/selinux/config #修改配置文件,改爲disabled [root@CentOS7_30 ~]# iptables -F #清楚防火牆配置 [root@CentOS7_30 ~]# yum install git-daemon -y #安裝git-daemon守護進程 [root@CentOS7_30 ~]# git --version #查看版本 git version 1.8.3.1 [root@CentOS7_30 ~]# rpm -ql git-daemon #查看安裝信息 /usr/lib/systemd/system/git.socket /usr/lib/systemd/system/[email protected] /usr/libexec/git-core/git-daemon ... [root@CentOS7_30 ~]# cat /usr/lib/systemd/system/[email protected] #查看倉庫的定義,--base-path爲倉庫,也可以修改它自定義指定倉庫 [Unit] Description=Git Repositories Server Daemon Documentation=man:git-daemon(1) [Service] User=nobody ExecStart=-/usr/libexec/git-core/git-daemon --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose StandardInput=socket [root@CentOS7_30 git]# git init --bare project.git #創建裸倉庫 Initialized empty Git repository in /var/lib/git/project.git/ [root@CentOS7_30 git]# ll #查看是否創建成功 total 4 drwxr-xr-x. 7 root root 4096 Oct 11 21:24 project.git [root@CentOS7_30 git]# systemctl start git.socket #啓動git-daemon [root@CentOS7_30 git]# ss -tnl | grep 9418 #查看是否啓動成功 LISTEN 0 128 :::9418 :::*
2、centos7_29客戶端clone
[root@CentOS7_29 ~]# yum install git -y #安裝git [root@CentOS7_29 ~]# git --version #查看版本,centos6.x都是1.7.1 ,使用http密碼驗證會有問題 git version 1.8.3.1 [root@CentOS7_29 ~]# git clone git://192.168.11.30/project.git #clone Cloning into 'project'... warning: You appear to have cloned an empty repository. [root@CentOS7_29 ~]# ls #查看是否成功 anaconda-ks.cfg project [root@CentOS7_29 ~]# cd project/ #進入倉庫 [root@CentOS7_29 project]# ls -a #查看是否有問題, . .. .git [root@CentOS7_29 project]# echo "print 'sunshine'" > code.py #在倉庫新建文件 [root@CentOS7_29 project]# git status #查案狀態 # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # code.py nothing added to commit but untracked files present (use "git add" to track) [root@CentOS7_29 project]# git add code.py #加入暫存區 [root@CentOS7_29 project]# git commit -m "v0.1" #commit並說明,第一次會提示輸入用戶更郵箱 *** Please tell me who you are. Run git config --global user.email "[email protected]" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address (got 'root@CentOS7_29.(none)') [root@CentOS7_29 project]# git config --global user.email "[email protected]" #設置郵箱 [root@CentOS7_29 project]# git config --global user.name sunshine #設置用戶 [root@CentOS7_29 project]# git config --global color.ui auto #設置顏色 [root@CentOS7_29 project]# git commit -m "v0.1" #再次提交 [master (root-commit) 4d0d265] v0.1 1 file changed, 1 insertion(+) create mode 100644 code.py [root@CentOS7_29 project]# git log commit 4d0d265d8511f338abb8b4e0652c3a1a6656a892 Author: sunshine <[email protected]> Date: Tue Oct 11 21:33:37 2016 +0800 v0.1 [root@CentOS7_29 project]# git log --oneline #查看log 4d0d265 v0.1 [root@CentOS7_29 project]# git push origin master #push失敗,因爲git不止寫操作 fatal: remote error: access denied or repository not exported: /project.git
3、在centos7_30服務器配置使用htto協議
[root@CentOS7_30 project.git]# pwd #進入項目倉庫 /var/lib/git/project.git [root@CentOS7_30 project.git]# git config http.receivepack true #配置git config即可 [root@CentOS7_30 ~]# yum install httpd -y #安裝httpd [root@CentOS7_30 ~]# httpd -M | grep -Ei "\<(alias|cgi|env)" #必須要有這三個模塊 AH00557: httpd: apr_sockaddr_info_get() failed for CentOS7_30 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message alias_module (shared) env_module (shared) cgi_module (shared) [root@CentOS7_30 ~]# vim /etc/httpd/conf/httpd.conf #編輯httpd文件,註銷中心主機 註銷DocumentRoot中心主機 [root@CentOS7_30 ~]# vim /etc/httpd/conf.d/git.conf #新增git.conf定義虛擬主機 <VirtualHost *:80> #定義端口 ServerName git.sunshine.com #定義域名 SetEnv GIT_PROJECT_ROOT /var/lib/git #定義項目路徑 SetEnv GIT_HTTP_EXPORT_ALL #使用HTTP導出 ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/ #千萬注意如果git後面沒有/那麼git-http-backend後面也一定不能有/ <Directory "/usr/libexec/git-core/"> #該目錄下好像都是一些prel腳本 Options ExecCGI Indexes Require all granted </Directory> <LocationMatch "^/git/.*/git-receive-pack$"> #匹配我們定義的 AuthType Basic #使用標準認證 AuthName "sunshine test project." #描述 AuthUserFile /etc/httpd/conf/.htpasswd #存放賬戶密碼的地方 Require valid-user </LocationMatch> </VirtualHost> [root@CentOS7_30 ~]# chown -R apache.apache /var/lib/git/ #設定用戶及用戶組,不設定那麼你是沒權限的 [root@CentOS7_30 git-core]# htpasswd -c -m /etc/httpd/conf/.htpasswd tom #創建賬戶密碼第一次使用-c New password: Re-type new password: Adding password for user tom [root@CentOS7_30 git-core]# htpasswd -m /etc/httpd/conf/.htpasswd jerry #創建賬戶密碼第二次不要使用-c,不然你就一直就是一個用戶 New password: Re-type new password: Adding password for user jerry [root@CentOS7_30 git-core]# systemctl start httpd.service #啓動httpd服務 :::* [root@CentOS7_30 git-core]# ss -tnl | grep 80 #查看是否啓動成功 LISTEN 0 128 :::80 :::*
4、在centos7_29客戶端pull以及push
[root@CentOS7_29 ~]# cat /etc/hosts #編輯hosts增加域名host 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.11.30 git.sunshine.com [root@CentOS7_29 ~]# git clone http://git.sunshine.com/git/project.git #使用http協議clone Cloning into 'project'... warning: You appear to have cloned an empty repository. [root@CentOS7_29 ~]# cd project/ #進入project倉庫 [root@CentOS7_29 project]# ls #查看倉庫 [root@CentOS7_29 project]# !echo #創建python腳本 echo "print 'sunshine'" > code.py #該爲完整命令,上面 的使用記錄! [root@CentOS7_29 project]# git add code.py #條件腳本git add [root@CentOS7_29 project]# git commit -m "v0.1" #commit並說明 [master (root-commit) d847298] v0.1 1 file changed, 1 insertion(+) create mode 100644 code.py [root@CentOS7_29 project]# git push origin master #push至遠程倉庫 Counting objects: 3, done. Writing objects: 100% (3/3), 215 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) Username for 'http://git.sunshine.com': tom #提示輸入賬號,輸入創建的tom Password for 'http://[email protected]': #輸入密碼,就可以提交成功 To http://git.sunshine.com/git/project.git * [new branch] master -> master