Git服务器配置
1.部署环境:
(1)系统版本:CentOS 5.5
(2)服务端 IP:172.16.34.235 Hostname:CentOS5-A
客户端1 IP:172.16.34.236 Hostname:CentOS5-B
客户端2 IP:172.16.34.237 Hostname:CentOS5-C
(3)所需包下载地址:
wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tar.bz2
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
2. python 安装
----------------
查看python版本
python -V
显示结果:
Python 2.4.3
升级到Python 2.7.1
cd /data/softs
tar xvjf Python-2.7.1.tar.bz2
cd Python-2.7.1/
./configure --enable-shared
make
make install
ldconfig
3.setuptools 安装 为安装gitosis做准备
cd /data/softs
tar -zxvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py build
python setup.py install
4.Git 安装
----------------
首先需要安装git的依赖包
yum install curl
yum install curl-devel
yum install zlib-devel
yum install openssl-devel
yum install perl
yum install cpio
yum install expat-devel
yum install gettext-devel
cd /data/softs
tar -zxvf git-latest.tar.gz
cd git-2012-11-13
autoconf
./configure --with-python=/usr/local/bin/python2.7
make
make install
git --version
5.添加用户git
useradd -r -s /bin/sh -c 'git version control' -d /home/git git
passwd git
mkdir -p /home/git
chown git:git /home/git
注:步骤2-5 三台机器上均需要安装
6.安装gitosis(服务端)
cd /data/softs
git clone git://github.com/res0nat0r/gitosis.git
Cloning into 'gitosis'...
remote: Counting objects: 727, done.
remote: Compressing objects: 100% (217/217), done.
remote: Total 727 (delta 510), reused 690 (delta 494)
Receiving objects: 100% (727/727), 110.70 KiB | 53 KiB/s, done.
Resolving deltas: 100% (510/510), done.
cd gitosis
python setup.py install
7.生成本机密钥,上传密钥到服务器临时目录
(1)切换到客户端1
# su git
sh-3.2$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/git/.ssh/id_rsa):
Created directory '/home/git/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/git/.ssh/id_rsa.
Your public key has been saved in /home/git/.ssh/id_rsa.pub.
The key fingerprint is:
50:93:2a:dd:50:b2:dd:c2:aa:a6:f9:a6:9f:55:82:b9 git@CentOS5-B
sh-3.2$ scp /home/git/.ssh/id_rsa.pub 172.16.34.235:/tmp/id_rsa_client1.pub
The authenticity of host '172.16.34.235 (172.16.34.235)' can't be established.
RSA key fingerprint is 0f:92:64:29:ab:02:4a:33:b2:8b:39:b4:8f:0d:88:40.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.34.235' (RSA) to the list of known hosts.
git@172.16.34.235's password:
id_rsa.pub 100% 395 0.4KB/s 00:00
sh-3.2$
(2)客户端2作同样操作
8.初使化gitosis,生成管理库
sh-3.2$ gitosis-init </tmp/id_rsa_client1.pub
看到如下提示说明安装成功
Initialized empty Git repository in /home/git/repositories/gitosis-admin.git/
Reinitialized existing Git repository in /home/git/repositories/gitosis-admin.git/
执行初始化命令会生成两个文件夹如下:
sh-3.2$ls /home/git/
gitosis keys repositories
gitosis作用在于,它通过一个git仓库来管理配置文件,仓库就放在了/home/git/repositorie/gitosis-admin.git
为文件加上可执行权限:
sh-3.2$chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
使用git之前必须要设置2个全局变量
git config --global user.email "hanxiaoqi@xqhan.com"
git config --global user.name "hanxiaoqi"
这个将设置在你的home目录下,默认是在~/.gitconfig里
9.测试
为了测试添加新用户:
客户端1 添加用户useradd song,生成key密钥(/home/song/.ssh/id_rsa.pub)
客户端2 添加用户useradd hanxiaoqi,生成key密钥(/home/hanxiaoqi/.ssh/id_rsa.pub)
在客户端1上执行:
#su git
sh-3.2$ cd /home/git
sh-3.2$ git clone git@172.16.34.235:gitosis-admin.git
Cloning into 'gitosis-admin'...
remote: Counting objects: 17, done.
remote: Compressing objects: 100% (16/16), done.
Receiving objects: 100% (17/17), done.
Resolving deltas: 100% (1/1), done.
remote: Total 17 (delta 1), reused 5 (delta 0)
sh-3.2$ ls
gitosis-admin
sh-3.2$ cd gitosis-admin/keydir/
现在把生成的ssh公钥文件都复制到/home/git/gitosis-admin/keydir,按名字命名一下,比如hanxiaoqi.pub, song.pub等
sh-3.2$ ls
git@CentOS5-B.pub git@CentOS5-C.pub hanxiaoqi.pub song.pub
sh-3.2$ cd ..
sh-3.2$ vi gitosis.conf
------------------
[gitosis]
[group gitosis-admin]
members = git@CentOS5-B git@CentOS5-C hanxiaoqi song
writable = gitosis-admin
[group project]
writable = project
members = git@CentOS5-B git@CentOS5-C hanxiaoqi
[group project_ro]
readonly = project
members = song
-------------------------
这个配置文件表达了如下含义:gitosis-admin组成员有git@CentOS5-B git@CentOS5-C hanxiaoqi song,该组对gitosis-admin仓库有读写权限;
project组有git@CentOS5-B git@CentOS5-C hanxiaoqi三个成员,该组对project仓库有读写权限;
project_ro组有song一个成员,对project仓库有只读权限。
当然目前这些配置文件的修改只是在你的本地,你必须推送到远程的git server上才能真正生效。
加入新文件、提交并push到git服务器:
sh-3.2$ git add .
sh-3.2$ git commit -am "user add"
[master db3fe31] user add
5 files changed, 4 insertions(+), 2 deletions(-)
delete mode 160000 gitosis-admin2
create mode 100644 keydir/git@CentOS5-C.pub
create mode 100644 keydir/hanxiaoqi.pub
create mode 100644 keydir/song.pub
sh-3.2$ git push origin master
Counting objects: 10, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 1.50 KiB, done.
Total 7 (delta 0), reused 0 (delta 0)
To git@172.16.34.235:gitosis-admin.git
13e345b..db3fe31 master -> master
sh-3.2$
回到服务端:
cat /home/git/.ssh/authorized_keys
sh-3.2$ cat authorized_keys
### autogenerated by gitosis, DO NOT EDIT
command="gitosis-serve hanxiaoqi",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA02vPejh7OfzHtFc3dlA+k55X9kttJ/8WgK2YiXObAElXJ+CAzZKayciUfbY4A3Xl55kVVPnYhvfxdndBWXAj2za8iSCW535XMgFBXH/gtD2+Ecql2oUu0TOoAldgZ6jJ03hon+B0T4znWZTmfNKY4GauWgj65Z+ex+VZoO357089mTGMnoOZx1sblC+7vr9TnFoSh1hOqdYlaBjvB8BtcXrU4OVbZphPE1BxmT6OSDWx677M157uWsyZFxH5ocr8hqZ88+zpVZe1i5VEZpnXETHlXb6Ix/4mEutscJ5WGWjZEJgUNGv+jYTSvtDS+7z/A9ot93kze46RxPFuRB6Cyw== hanxiaoqi@CentOS5-C
command="gitosis-serve song",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAqhgzC2oi8JrYNZcyZ//ojddJCqalKJ0mBtMCfq9VEHPh0YjaZqdNd4HeqHrtFoRe3C+6zQwqAesd+q01gMyOTAyAI76d5b4j81MNCyvGbzbhzc/xOOxCpnT9H5ZKoChzoh88Tah1pjsZDW6spBHW/HRtaLRKB3k6fAGyctE5heJJUqGOknnDfzw+ZyzfFxynNiYvgDgWW5MfALFz/ltzo41QwL30Lfj6ZuHF2kV4Olc7jK1vHzm0/riU3a6BeYf5NAmATNlP94QbThUBdq/MpKJ7OFDXCNXuFBBJ9RANtwA3m8pFb48cZ4ebuMfd8YQ0I2Rb+OvVxVDT0XGYU/uNAw== song@CentOS5-B
command="gitosis-serve git@CentOS5-C",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0pO/1EcLPBZOL8zPASs85srQGBz4QHv3aV0SR/zJw/ViuUwY8toMtRp0OsQ8VaqqkP3RIh6J49ZgtmDnbrrcLIkpnTgHEqU+s7WoHmZWnttMEYs8aMoSvFT5pxR9k54baHKaA2TmUtTmCJnVCqPx2uSP5tb+Cww18dj9+LzdJ/fCllcwuGfg9z0G8seedgq/Iv22XIMH045ciUNC7P02kSq2tjCctJtDtP8Cb2MYhurySOVBqjAx+GISrXJXUqRI1Gsr1CjlxazsoRwnHUz217btE+xnpwkQ7lxPMNyPS2fMbR+chU/Pg7Yfx43xxhePhsPqCmYFLaX87gyBRV/GVw== git@CentOS5-C
command="gitosis-serve git@CentOS5-B",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1o68X0/pVW3HM3CZTZhSASOhjj3lI1s0Txzt8Yg1NoEVY/NAW7oFCENxu0Wg2Cxrb93w0HfSTHb/2dXtzhzb4Oq7XaxDYpy+i5M3y1xiQkZoqL30qh7n++RWjpMwObgs00PzcV4kiZ+UCU2gnDlJl+t8rc6CwlEcphoN9IPexiGUJIeJeRWA0c+BGJH0ds3I/D+TGX3Jr92R7k2zsn3pmD6i9px6EtDK/QKFzPnnBQ9psCRiHJk3qu33sghJuUlp7vQU6zKylxMZ9ypevo9nJnA/gUf0PLQKSgvj39/k5PIGVrLOt4o3xu/Z8PeM/dd8cpDEd3hHhFmw+Y0Ev+CaXw== git@CentOS5-B
到此客户端用户公钥已经添加到/home/git/.ssh/authorized_keys文件中。
10.创建空仓库
在服务端创建空仓库,并初始化。
sh-3.2$ cd /home/git/repositories/
sh-3.2$ mkdir project.git
sh-3.2$ cd project.git/
sh-3.2$ git init --bare
Initialized empty Git repository in /home/git/repositories/project.git/
sh-3.2$ ls
branches config description HEAD hooks info objects refs
sh-3.2$
在客户端直接clone该仓库就可以了
1.客户端1:
切换到git用户
sh-3.2$ cd /home/git
sh-3.2$ git clone git@172.16.34.235:project.git
Cloning into 'project'...
warning: You appear to have cloned an empty repository.
sh-3.2$ ls
gitosis-admin project
sh-3.2$ cd project/
sh-3.2$ vi hello
编辑文件
-----------------
hello
--------------------
sh-3.2$ git add .
sh-3.2$ git commit -am "add hello file"
[master (root-commit) 8c6f80f] add hello file
1 file changed, 1 insertion(+)
create mode 100644 hello
sh-3.2$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@172.16.34.235:project.git
* [new branch] master -> master
sh-3.2$
2.客户端2:
切换到hanxiaoqi用户
[root@CentOS5-C home]# su hanxiaoqi
[hanxiaoqi@CentOS5-C home]$ pwd
/home
[hanxiaoqi@CentOS5-C home]$ cd hanxiaoqi/
[hanxiaoqi@CentOS5-C ~]$ ls
[hanxiaoqi@CentOS5-C ~]$ pwd
/home/hanxiaoqi
[hanxiaoqi@CentOS5-C ~]$ git clone git@172.16.34.235:project.git
正克隆到 'project'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收对象中: 100% (3/3), done.
[hanxiaoqi@CentOS5-C ~]$ ls
project
[hanxiaoqi@CentOS5-C ~]$ cd project/
[hanxiaoqi@CentOS5-C project]$ ls
hello
[hanxiaoqi@CentOS5-C project]$ vi hello2
hello2
~
~
"hello2" [新] 1L, 7C 已写入
[hanxiaoqi@CentOS5-C project]$ ls
hello hello2
[hanxiaoqi@CentOS5-C project]$ git add .
[hanxiaoqi@CentOS5-C project]$ git commit -am "add hello2 file by hanxiaoqi"
*** 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: empty ident name (for <hanxiaoqi@CentOS5-C.(none)>) not allowed
[hanxiaoqi@CentOS5-C project]$ git config --global user.email "hanxiaoqi@xqhan.com"
[hanxiaoqi@CentOS5-C project]$ git config --global user.name "hanxiaoqi"
[hanxiaoqi@CentOS5-C project]$ git commit -am "add hello2 file by hanxiaoqi"
[master fd9c5fa] add hello2 file by hanxiaoqi
1 file changed, 1 insertion(+)
create mode 100644 hello2
[hanxiaoqi@CentOS5-C project]$ git push origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@172.16.34.235:project.git
8c6f80f..fd9c5fa master -> master
[hanxiaoqi@CentOS5-C project]$
3.客户端1:
切换到song用户
[root@CentOS5-B .ssh]# su song
[song@CentOS5-B .ssh]$ cd /home/song
[song@CentOS5-B ~]$ ls
[song@CentOS5-B ~]$ git clone git@172.16.34.235:project.git
正克隆到 'project'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
接收对象中: 100% (6/6), done.
[song@CentOS5-B ~]$ ls
project
[song@CentOS5-B ~]$ cd project/
[song@CentOS5-B project]$ ls
hello hello2
[song@CentOS5-B project]$ vi hello3
hello3
~
"hello3" [新] 1L, 7C 已写入
[song@CentOS5-B project]$ git add .
[song@CentOS5-B project]$ git commit -am "add hello3 file by song"
*** 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: empty ident name (for <song@CentOS5-B.(none)>) not allowed
[song@CentOS5-B project]$ git config --global user.email "hanxiaoqi@xqhan.com"
[song@CentOS5-B project]$ git config --global user.name "hanxiaoqi"
[song@CentOS5-B project]$ git commit -am "add hello3 file by song"
[master f645290] add hello3 file by song
1 file changed, 1 insertion(+)
create mode 100644 hello3
[song@CentOS5-B project]$ git push origin master
ERROR:gitosis.serve.main:Repository write access denied
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
[song@CentOS5-B project]$
4.客户端2
切换到git用户
[root@CentOS5-C home]# su git
sh-3.2$ cd /home/git/
sh-3.2$ ls
gitosis-admin
sh-3.2$ cd gitosis-admin/
sh-3.2$ ls
gitosis.conf keydir
sh-3.2$ vi gitosis.conf 开通song用户写权限
[gitosis]
[group gitosis-admin]
members = git@CentOS5-B git@CentOS5-C hanxiaoqi song
writable = gitosis-admin
[group project]
writable = project
members = git@CentOS5-B git@CentOS5-C hanxiaoqi song
#[group project_ro]
#readonly = project
#members = song
~
"gitosis.conf" 11L, 256C written
sh-3.2$ git add .
sh-3.2$ git commit -am "user song writer"
[master 1c80b8d] user song writer
1 file changed, 4 insertions(+), 4 deletions(-)
sh-3.2$ git push origin master
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 361 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@172.16.34.235:/gitosis-admin.git
e1503c4..1c80b8d master -> master
sh-3.2$
5.回到客户端1到song用户
再次执行:
[song@CentOS5-B project]$ git push origin master
ERROR:gitosis.serve.main:Repository write access denied
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
[song@CentOS5-B project]$ git push origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 304 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@172.16.34.235:project.git
fd9c5fa..f645290 master -> master
[song@CentOS5-B project]$
11.viewgit
viewGit 是一个 Git 版本控制系统的 Web 接口,用来查看资源库中的信息,viewGit 安装和升级都非常简单。
首先安装nginx+php,因为viewgit是php环境开发
viewGit安装
[root@CentOS5-A repositories]# git clone git://repo.or.cz/viewgit.git
Cloning into 'viewgit'...
remote: Counting objects: 1675, done.
remote: Compressing objects: 100% (503/503), done.
remote: Total 1675 (delta 1114), reused 1675 (delta 1114)
Receiving objects: 100% (1675/1675), 1.30 MiB | 5 KiB/s, done.
Resolving deltas: 100% (1114/1114), done.
[root@CentOS5-A repositories]# cd viewgit/
[root@CentOS5-A viewgit]# ls
default.css favicon.png inc light.css templates
doc p_w_picpaths index.php plugins
将viewgit文件夹所有内容复制到网站根目录(/data/htdocs/www)
[root@CentOS5-A viewgit]# cd /data/htdocs/www
[root@CentOS5-A www]# ls
default.css favicon.png inc index.php plugins viewgit
doc p_w_picpaths index.html light.css templates viewgit.tag.gz
[root@CentOS5-A www]# cd inc
[root@CentOS5-A inc]# ls
auth_drupal_user.php config.php functions.php
auth_simple.php example-localconfig.php plugins.php
[root@CentOS5-A inc]# cp config.php localconfig.php
[root@CentOS5-A inc]# vi localconfig.php
查找到以下内容:
$conf['projects'] = array(
// 'name' => array('repo' => '/path/to/repo/.git'), // path to GIT_DIR
);
更改为:
$conf['projects'] = array();
$git_repos_path = '/home/git/repositories';
$git_repos_folders = scandir($git_repos_path);
foreach ($git_repos_folders as $folder) {
if (preg_match('/^.*\.git$/', $folder) === 1) {
$conf['projects'][$folder] = array('repo' => $git_repos_path . '/' . $folder);
}
}
创建仓库描述文件
[root@CentOS5-A www]# cd /home/git/
[root@CentOS5-A git]# ls
gitosis id_rsa.pub keys repositories
[root@CentOS5-A git]# cd repositories/
[root@CentOS5-A repositories]# ls
gitosis-admin.git project.git viewgit
[root@CentOS5-A repositories]# cd project.git/
[root@CentOS5-A project.git]# ls
branches config description HEAD hooks info objects refs
[root@CentOS5-A project.git]# cat description
project 1.0 by hanxiaoqi
[root@CentOS5-A project.git]#
测试:
在浏览器里访问http://172.16.34.235
显示如下如果:
Project Description Last Commit Last Change Actions
gitosis-admin.git gitosis-admin manage all project's user permissions. add git237.pub 2012-11-15 06:26 tree tar/gz zip
project.git project 1.0 by hanxiaoqi new file hello3 2012-11-16 04:34 tree tar/gz zip
ViewGit