Ubuntu12.04 Git 服务器详细配置

  Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,学过Linux的都知道,Git的优点我就不再多说了,我也是很喜欢Linux的。今天我们一起学习Git服务器在Ubuntu 12.04上的配置,废话不多说,走起!

1.Git 服务器的搭建过程详细记录,如下:

搭建环境:Ubuntu 12.04 桌面版或服务器版均可 软件要求: git-core, gitosis, openssh-server, openssh-client, Apache2(Gitweb),python编译环境。

2,安装配置git服务器。

  准备工作,为了不改变计算机的环境,我们新增家一个用户:
  User Name:git
  Password:git
  切换用户:su git

  Ubuntu12.04 Git 服务器详细配置

3.安装git和openssh:
  git@lightserver-GREATWALL-PC:~$ sudo apt-get install git-core openssh-server openssh-client

 Ubuntu12.04 Git 服务器详细配置 

4.添加Git用户gitadmin,该用户将作为所有代码仓库和用户权限的管理者:

  git@lightserver-GREATWALL-PC:~$ sudo useradd -m gitadmin

    Ubuntu12.04 Git 服务器详细配置

5.建立一个git仓库的存储点:

  Ubuntu12.04 Git 服务器详细配置

6.更改gitadmin的权限

  git@lightserver-GREATWALL-PC:~/repo$ sudo chown gitadmin:gitadmin /home/git/repo

  Ubuntu12.04 Git 服务器详细配置

  更改仓库的访问权限:

  sudo chmod 700 /home/git/repo

  Ubuntu12.04 Git 服务器详细配置

7.安装配置gitosis

  初始化一下服务器的git用户,这一步其实是为了安装gitosis做准备。在任何一台机器上使用git,第一次必须要初始化一下:初始化用户名和邮箱。
  git@lightserver-GREATWALL-PC:~/repo$ sudo git config --global user.name “tengfei”

  Ubuntu12.04 Git 服务器详细配置

  

  这是什么原因呢?不要着急,检查下啊!哦。。。。原来是 ‘--global’

  Ubuntu12.04 Git 服务器详细配置

  

  OK,没有报错,通过设置!
  初始化git邮箱
  git@lightserver-GREATWALL-PC:~/repo$ sudo git config --global user.email “tengfei@10.255.13.253“

@后是服务器IP。

  Ubuntu12.04 Git 服务器详细配置

8.安装一下python的setup tool, 这个也是为了gitosis做准备:

  Ubuntu12.04 Git 服务器详细配置

  git@lightserver-GREATWALL-PC:~/repo$ sudo apt-get install python-setuptools
  OK,Next!

9.获得gitosis包:切换到/tmp下。

  git@lightserver-GREATWALL-PC:/$ cd /tmp

   Ubuntu12.04 Git 服务器详细配置

   git@lightserver-GREATWALL-PC:/tmp$ git clone https://github.com/res0nat0r/gitosis.git

   Ubuntu12.04 Git 服务器详细配置

    ls一下:多了个gitosis文件夹吧?

    Ubuntu12.04 Git 服务器详细配置

   Perfect,Next,python 安装gitosis

  git@lightserver-GREATWALL-PC:/tmp/gitosis$ sudo python setup.py install

  Ubuntu12.04 Git 服务器详细配置

  到此为止,gitosis安装完成!

10.切换到gitadmin用户下:
  light-server@lightserver-GREATWALL-PC:~$ su gitadmin
  密码:
  $

  Ubuntu12.04 Git 服务器详细配置

  为什么是$ ?多难看。Linux支持bash,shell,sh或许就是这个原因吧?我猜的,试试不就知道了?走起。。   

  Ubuntu12.04 Git 服务器详细配置

  还真是这个原因,这样看着顺眼!

11.默认状态下,gitosis会将git仓库放在 gitadmin用户的home下,所以我们做一个链接到/home/repo
  ln -s /home/git/repo /home/gitadmin/repositories
  返回正常用户:
  $ exit
  light-server@lightserver-GREATWALL-PC:~$

  Ubuntu12.04 Git 服务器详细配置

  查看软链接状态:

  Ubuntu12.04 Git 服务器详细配置

12.如果你将作为git服务器的管理员,那么在你的电 脑上(另一台pc)生成ssh公钥:

   我在我的电脑上,用户为:wuzhang@ubuntu:$
  生成ssh公钥。
  wuzhang@ubuntu~:$ ssh -keygen -t rsa

  Ubuntu12.04 Git 服务器详细配置

  OK,生成公钥成功!
  复制到远程主机上,这就考验我们Linux 命令的基础了。还好大一linux学的还不错!
  Scp 远程复制命令: scp 本地文件 远端主机:存储路径

  例如:scp .ssh/id_rsa.pub light-server@10.255.13.253:/tmp

  Ubuntu12.04 Git 服务器详细配置

  OK,100%这个我喜欢, 说明复制成功了,不信我们可以去主机查看:

  Ubuntu12.04 Git 服务器详细配置

  果然有id_rsa.pub.

  在git服务器上,更改权限:
  git@lightserver-GREATWALL-PC:/tmp/gitosis$ sudo chmod a+r /tmp/id_rsa.pub

  Ubuntu12.04 Git 服务器详细配置

13.让gitosis运行起来:

  执行命令:sudo -H -u git gitosis -init < /tmp/id_rsa.pub

  Ubuntu12.04 Git 服务器详细配置

  Initialized empty Git repository in /home/repo/gitosis-admin.git/

   Reinitialized existing Git repository in /home/repo/gitosis-admin.git/
   说明实例化空的git仓库/home/git/repositories/gitosis-admin.git/已成功了。

14.gitosis的有趣之处在于,它通过一个git仓库来管理配置文件,仓库就放在了/home/git/repositories/gitosis-admin.git。

我们需要为一个文件加上可执行权限:

 获取root权限

  light-server@lightserver-GREATWALL-PC:/home$ sudo -i
  目录切换:
  root@lightserver-GREATWALL-PC:/home/git# cd repositories/
  root@lightserver-GREATWALL-PC:/home/git/repositories# ls
  gitosis-admin.git
  root@lightserver-GREATWALL-PC:/home/git/repositories# cd gitosis-admin.git
  root@lightserver-GREATWALL-PC:/home/git/repositories/gitosis-admin.git# ls
  config gitosis.conf gitosis-export HEAD hooks index objects refs

  

更改权限(关键步骤)

root@lightserver-GREATWALL-PC:/home/git/repositories/gitosis-admin.git# sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
root@lightserver-GREATWALL-PC:/home/git/repositories/gitosis-admin.git# exit
准备工作到此结束了,下面可以进行测试了!

15.在服务器上新建一个测试项目仓库

我建了一个叫“teamwork”的仓库。切换到gitadmin用户:
gitadmin@lightserver-GREATWALL-PC:~$ mkdir teamwork.git
gitadmin@lightserver-GREATWALL-PC:~$ cd teamwork.git/
gitadmin@lightserver-GREATWALL-PC:~/teamwork.git$

Ubuntu12.04 Git 服务器详细配置

git的初始化:
gitadmin@lightserver-GREATWALL-PC:~/teamwork.git$ git init --bare
Initialized empty Git repository in /home/gitadmin/teamwork.git/
gitadmin@lightserver-GREATWALL-PC:~/teamwork.git$

Ubuntu12.04 Git 服务器详细配置

但是,到目前为止,这只是一个空仓库,空仓库是不能clone下来的。为了能做clone,我们必须先让某个有权限的人放一个初始化的版本到仓库中。
所以,我们必须先修改一下/home/git/repositories /gitosis-admin.

16.管理gitosis的配置文件

刚刚提到,gitosis本身的配置也是通过git来实现的。在你自己的开发机里,把gitosis-admin.git这个仓库clone下来,就可以以管理员的身份修改配置。

好的,我们回到另一台PC上进行对服务端的测试! 我自己的PC,名字叫:wuzhang@ubuntu

wuzhang@ubuntu:~/work$ sudo git clone root@10.255.13.253:/home/git/repositories/gitosis-admin.git
Cloning into 'gitosis-admin'...
root@10.255.13.253's password:
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 5 (delta 0)
Receiving objects: 100% (5/5), done.
wuzhang@ubuntu:~/work$

Ubuntu12.04 Git 服务器详细配置

PS:目前主机必须是root才可以克隆.

wuzhang@ubuntu:~/work/gitosis-admin$ cd keydir/
wuzhang@ubuntu:~/work/gitosis-admin/keydir$ ls
wuzhang@ubuntu.pub
wuzhang@ubuntu:~/work/gitosis-admin/keydir$
wuzhang@ubuntu上用户权限配置使用vi编辑器打开:
[gitosis]

[group gitosis-admin]
members = wuzhang@ubuntu
writable = gitosis-admin
members = wuzhang@10.255.13.253 light-server@10.255.13.253

[group hello]
writable = teamwork
members = lijiangkun@server b git@10.258.13.100

[group hello_ro]
readonly = teamwork
members = lz

Ubuntu12.04 Git 服务器详细配置

wq!  保存退出.

这个配置文件表达了如下含义:gitosis-admin组成员有wuzhang@ubuntu, wuzhang@10.255.13.100, light-server@10.255.13.253
该组对gitosis-admin仓库有读写权限;
team组有lijinagkun@server,b两个成员.该组对teamwork仓库有读写权限;
team_ro组有lz一个成员,对teamwork仓库有只读权限
在wuzhang@ubuntu上创建一hello.txt的测试文档:
wuzhang@ubuntu:~/work/teamwork_project$ echo "Test git server. " > hello.txt
wuzhang@ubuntu:~/work/teamwork_project$ ls -al
总用量 16
drwxrwxr-x 3 wuzhang wuzhang 4096 5月 13 18:08 .
drwxrwxr-x 4 wuzhang wuzhang 4096 5月 13 18:07 ..
drwxrwxr-x 7 wuzhang wuzhang 4096 5月 13 18:07 .git
-rw-rw-r-- 1 wuzhang wuzhang 18 5月 13 18:09 hello.txt
wuzhang@ubuntu:~/work/teamwork_project$

Ubuntu12.04 Git 服务器详细配置

接下来要提交到远端服务器了。

root@ubuntu:/home/wuzhang/work/gitosis-admin# git add .
root@ubuntu:/home/wuzhang/work/gitosis-admin# git commit -m "add teamwork and user for git server"
[master 51a4055] add teamwork and user for git server
Committer: root <root@ubuntu.ubuntu-domain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config --global user.name "Your Name"
git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

2 files changed, 9 insertions(+)
create mode 100644 keydir/b.pub

wuzhang@ubuntu:~/work/teamwork_project$ git commit -m "initial version"
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# hello.txt
nothing added to commit but untracked files present (use "git add" to track)
wuzhang@ubuntu:~/work/teamwork_project$
添加远端服务器的地址:
root@ubuntu:/home/wuzhang/work/teamwork_project# git remote add origin gitadmin@10.255.13.253:/home/gitadmin/teamwork.git
root@ubuntu:/home/wuzhang/work/teamwork_project# git remote -v
origin gitadmin@10.255.13.253:/home/gitadmin/teamwork.git (fetch)
origin gitadmin@10.255.13.253:/home/gitadmin/teamwork.git (push)
root@ubuntu:/home/wuzhang/work/teamwork_project#
提交文件到服务器:
lroot@ubuntu:/home/wuzhang/work/gitosis-admin# git remote -v
origin gitadmin@10.255.13.253:teamwork.git (fetch)
origin gitadmin@10.255.13.253:teamwork.git (push)
root@ubuntu:/home/wuzhang/work/gitosis-admin# git push -f origin master
gitadmin@10.255.13.253's password:
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 1.12 KiB, done.
Total 9 (delta 1), reused 4 (delta 0)
To gitadmin@10.255.13.253:teamwork.git
* [new branch] master -> master
root@ubuntu:/home/wuzhang/work/gitosis-admin#

换个用户,测试下是否可以从服务器克隆刚提交的文件?
切换到另一账户进行git clone测试:
wuzhang@ubuntu:~$ su git
密码:
git@ubuntu:/home/wuzhang$

查看服务器的地址:
git@ubuntu:/home/wuzhang/work/teamwork_project$ git remote -v
origin gitadmin@10.255.13.253:teamwork.git (fetch)
origin gitadmin@10.255.13.253:teamwork.git (push)

创建个文件夹,用于存放克隆从服务器获取的文件

git@ubuntu:/home/wuzhang/work/teamwork_project$ cd /gitClone/
git@ubuntu:/gitClone$ git clone gitadmin@10.255.13.253:teamwork.git
fatal: could not create work tree dir 'teamwork'.: ????
git@ubuntu:/gitClone$ git clone gitadmin@10.255.13.253:teamwork.git

出错了,不要怕,应该是权限的问题,我们再试下!

PS:因为git@ubuntu是普通用户,克隆的文件在本地需要创建文件夹,所以权限不够,需要sudo
git@ubuntu:/gitClone$ sudo git clone gitadmin@10.255.13.253:/home/wuzhang/work/teamwork
Cloning into 'teamwork'...
gitadmin@10.255.13.253's password:
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 10 (delta 1), reused 10 (delta 1)
Receiving objects: 100% (10/10), done.
Resolving deltas: 100% (1/1), done.
git@ubuntu:/gitClone$ ls
teamwork
git@ubuntu:/gitClone$ cd teamwork/

显示一下克隆的文件:
git@ubuntu:/gitClone/teamwork$ ls
1 1.c 1.c~ hello hello.txt

执行下我编译过得1.c文件

git@ubuntu:/gitClone/teamwork$ ./1
Git Server is OK!
git@ubuntu:/gitClone/teamwork$
克隆完成,服务器环境搭建完成!

Windows 下测试:

Ubuntu12.04 Git 服务器详细配置

打开winidows本地文件,ok!

Ubuntu12.04 Git 服务器详细配置

接下来要搭建一个可以方便访问的Web环境了,下次见!

搭建文档:http://pan.baidu.com/s/1o6A7bVc

上一篇:[转载]Brackets - 强大免费的开源跨平台Web前端开发工具IDE (HTML/CSS/Javascript代码编辑器)


下一篇:Spring Data JPA教程,第一部分: Configuration(翻译)