322321 发表于 2016-10-13 10:25:49

Centos 7 Git+httpd 基于密码认证push

环境:CentOS 7 *2192.168.11.{29,30}
   Git 版本1.8.3
   Httpd 版本2.4.5

1、服务端centos7_30配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# getenforce                  #查看selinux
Enforcing
# setenforce 0                  #设定为0宽容模式
# vim /etc/selinux/config       #修改配置文件,改为disabled
# iptables -F                   #清楚防火墙配置
# yum install git-daemon -y   #安装git-daemon守护进程
# git --version               #查看版本
git version 1.8.3.1
# rpm -ql git-daemon            #查看安装信息
/usr/lib/systemd/system/git.socket
/usr/lib/systemd/system/git@.service
/usr/libexec/git-core/git-daemon
...
# cat /usr/lib/systemd/system/git@.service#查看仓库的定义,--base-path为仓库,也可以修改它自定义指定仓库

Description=Git Repositories Server Daemon
Documentation=man:git-daemon(1)


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
# git init --bare project.git                #创建裸仓库
Initialized empty Git repository in /var/lib/git/project.git/      
# ll                                       #查看是否创建成功
total 4
drwxr-xr-x. 7 root root 4096 Oct 11 21:24 project.git
# systemctl start git.socket               #启动git-daemon
# ss -tnl | grep 9418                        #查看是否启动成功
LISTEN   0      128                      :::9418                  :::*




2、centos7_29客户端clone

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# yum install git -y                            #安装git
# git --version                                 #查看版本,centos6.x都是1.7.1 ,使用http密码验证会有问题
git version 1.8.3.1
# git clone git://192.168.11.30/project.git   #clone
Cloning into 'project'...
warning: You appear to have cloned an empty repository.      
# ls                                          #查看是否成功
anaconda-ks.cfgproject
# cd project/                                 #进入仓库
# ls -a                                 #查看是否有问题,
....git
# echo "print 'sunshine'" > code.py       #在仓库新建文件
# 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)
# git add code.py                         #加入暂存区
# git commit -m "v0.1"                  #commit并说明,第一次会提示输入用户更邮箱

*** Please tell me who you are.

Run

git config --global user.email "you@example.com"
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)')
# git config --global user.email "sunshine@sunshine.com"#设置邮箱
# git config --global user.name sunshine                  #设置用户
# git config --global color.ui auto                     #设置颜色
# git commit -m "v0.1"                                    #再次提交
v0.1                  
1 file changed, 1 insertion(+)
create mode 100644 code.py
# git log
commit 4d0d265d8511f338abb8b4e0652c3a1a6656a892
Author: sunshine <sunshine@sunshine.com>
Date:   Tue Oct 11 21:33:37 2016 +0800

    v0.1
# git log --oneline                                       #查看log
4d0d265 v0.1
# git push origin master                                  #push失败,因为git不止写操作
fatal: remote error: access denied or repository not exported: /project.git




3、在centos7_30服务器配置使用htto协议

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# pwd                                                #进入项目仓库
/var/lib/git/project.git
# git config http.receivepack true                   #配置git config即可
# yum install httpd -y                                       #安装httpd
# 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)
# vim /etc/httpd/conf/httpd.conf                              #编辑httpd文件,注销中心主机
注销DocumentRoot中心主机
# vim /etc/httpd/conf.d/git.conf                              #新增git.conf定义虚拟主机
    <VirtualHost *:80>                                                            #定义端口
    ServerName git.sunshine.com                                             #定义域名
    SetEnvGIT_PROJECT_ROOT /var/lib/git                                     #定义项目路径
    SetEnvGIT_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>
# chown -R apache.apache /var/lib/git/    #设定用户及用户组,不设定那么你是没权限的
# htpasswd -c -m /etc/httpd/conf/.htpasswd tom#创建账户密码第一次使用-c
New password:
Re-type new password:
Adding password for user tom
# htpasswd -m /etc/httpd/conf/.htpasswd jerry   #创建账户密码第二次不要使用-c,不然你就一直就是一个用户
New password:
Re-type new password:
Adding password for user jerry
# systemctl start httpd.service    #启动httpd服务                                                                                                                                             :::*   
# ss -tnl | grep 80                #查看是否启动成功
LISTEN   0      128                      :::80                      :::*




4、在centos7_29客户端pull以及push

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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
# git clone http://git.sunshine.com/git/project.git    #使用http协议clone
Cloning into 'project'...
warning: You appear to have cloned an empty repository.
# cd project/                                           #进入project仓库
# ls                                              #查看仓库
# !echo                                           #创建python脚本
echo "print 'sunshine'" > code.py                                          #该为完整命令,上面 的使用记录!
# git add code.py                                 #条件脚本git add
# git commit -m "v0.1"                            #commit并说明
v0.1
1 file changed, 1 insertion(+)
create mode 100644 code.py
# 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://tom@git.sunshine.com':                                 #输入密码,就可以提交成功
To http://git.sunshine.com/git/project.git
*       master -> master






页: [1]
查看完整版本: Centos 7 Git+httpd 基于密码认证push