分享

ubuntu 安装git 服务器

 langhuayipian 2013-08-28
  
本文主要介绍了git 服务端安装及应用,内容来自网络,我加以修改

(一)   Git服务端

1.请使用apt-get update和apt-get upgrade命令更新当前系统,如果不更新,在后面安装一些程序的时候可能会出现某些依赖包没安装的现象。

 2.安装OpenSSH Server( ubuntu (当前用户为wirror800)

    首先,使用ps -e|grep ssh命令,检查本机上是否已经运行ssh服务。如果没有,继续;否则,跳过此步骤。

    执行命令:

    sudo apt-get install openssh-server

 

3.修改ssh服务端配置文件 /etc/ssh/sshd_config (当前用户为wirror800)

   Port 22 # 修改成你想要的登陆端口,如2222

   PermitRootLogin no # 禁止root用户登陆

   StrictModes yes # 检查密钥的用户和权限是否正确,默认打开的

   RSAAuthentication yes # 启用 RSA 认证

   PubkeyAuthentication yes # 启用公钥认证

   PasswordAuthentication no # 禁止密码认证,默认是打开的

   ServerKeyBits 1024 # 修改后变为此状态,将ServerKey强度改为1024比特

   PermitEmptyPasswords no # 修改后变为此状态,禁止空密码进行登录

   修改完成后,重启ssh服务:

   sudo /etc/init.d/ssh restart

   注意:至此,服务端SSH的操作就已经结束了。网上说的“ id_rsa文件拷贝到Client端的~/.ssh/目录下,将id_rsa.pub拷贝到

Server端的~/.ssh/目录下,改名为authorized_keys ”其实都是不准确的。id_rsa是使用 ssh-keygen生成的rsa私钥, id_rsa.pub

是同时生成的公钥,服务器端通信使用的秘钥可以应该由服务器管理员来创建和管理,如果你想把当前用户wirror800作为管理

员也是可以的,相关管理员的操作见本文第二部分。不推荐在ubuntu服务器端建立管理员账号。

    建议大家参考一下OpenSSH的相关内容:http://www./doc/zh_CN/books/handbook/openssh.html

 

4.安装git (当前用户为wirror800)

    sudo apt-get install git-core

    安装git过程中可能会遇到一些依赖包下载失败的情况,主要是由于ubuntu的中文站点访问的问题,我们可以到其英文站点

上下载相关文件并进行手动安装。给大家一个链接地址:

     http://archive./ubuntu/pool/main/

 5.安装gitosis(为什么要使用gitosis是大家首先需要明确的问题) (当前用户为wirror800)

    (1)在你喜欢的位置下,新建一个文件夹,用来存放下载的gitosis文件,如

    mkdir ~/gitosis_setup

    (2)安装gitosis

    cd ~/ gitosis_setup

  1. git clone git://github.com/res0nat0r/gitosis.git

    cd gitosis

    sudo python setup.py install

    注意:如果python setup.py install失败,需要安装python-setuptools.py

    sudo apt-get install python-setuptools

 6. 为gitosis创建系统用户 (当前用户为wirror800)

      sudo useradd -m git

      sudo password git

 

7. 运行gitosis(当前用户为wirror800)

      (1)将管理员生成的公钥上传或拷贝到服务器上。这里的公钥需要在git服务器管理员下使用ssh-keygen -t rsa命令来创建,

网上流传的方法是scp /your pub_path/id_rsa.pub ${SERVER_IP}:id_rsa.pub。我们这里使用的是直接U盘拷贝的方法。

      (2)初始化gitosis

      进入到拷贝过来的id_rsa.pub所在目录:cd /tmp

      sudo chmod 777 id_rsa.pub

      sudo -H -u git gitosis-init < id_rsa.pub (或者切换到git用户下执行gitosis-init < id_rsa.pub也可以,记得exit切换回当前用户)

      此时,会在/home/git目录下生成一些目录,如果想要别人能够clone gitosis-admin.git,需要执行以下操作:

      sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update

 至此,gitosis的安装工作已完成,其相关配置可以有管理员来操作,然后再提交到服务器上。以下是关于gitweb的安装配置,

不需要的同学,请跳过下面的8~10步。

 8.安装gitweb (当前用户为wirror800)

   sudo apt-get install gitweb

9.安装apache2

  sudo apt-get install apache2

 10.配置gitweb (当前用户为wirror800)
(1)默认没有 css 加载,把 gitweb 要用的静态文件连接到 DocumentRoot 下:
   cd /var/ www/
   sudo ln -s / usr/ share/ gitweb/* .

   (注意后面的点)

注意:ubuntu 不用修改 可以跳过去 直接访问http://yourip/gitweb

yourip 是你服务器ip

(2)修改配置:

   sudo vi /etc/ gitweb.conf

   将 $projectroot 改为gitosis-admin.git所在目录: /home/git/repositories

 (3)修改 /home/git/repositories权限,默认情况下,gitosis将 repositories权限设置为不可读的

    sudo chmod 777 -R /home/git/repositories

 11.编辑apache2配置文件,建立web站点 (当前用户为wirror800,默认情况下可以忽略此步骤)

(1) 编辑apache2配置文件

    ubuntu中默认的web目录是/var/www,默认的cgi目录是 /usr/lib/cgi-bin/,安装完成gitweb后,gitweb的gitweb.cgi会自动放置

到该目录下。如果你的cgi路径不是默认的/usr/lib/cgi-bin/,需要将gitweb安装在/usr/lib/cgi-bin中的gitweb.cgi复制到原来配置

的cgi-bin路径,并修改apache的配置文件/etc/apache2/apache.conf:

    SetEnv  GITWEB_CONFIG   /etc/gitweb.conf
    gitweb.conf配置文件形如:(可自行修改,这里不做详细介绍)
<Directory "/srv/www/cgi-bin/gitweb">          
      Options FollowSymlinks ExecCGI         
      Allow from all                         
      AllowOverride all                      
      Order allow,deny                       

      <Files gitweb.cgi>
           SetHandler cgi-script
      </Files>                   
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^.* /gitweb.cgi/$0 [L,PT]
</Directory>

(2)重新启动apache:sudo /etc/init.d/apache2 restart,访问http://localhost/gitweb

 (二)   Git 管理员(git bash下运行)

(在管理git PC上,不一定在服务器上)

1: 安装git 具体方法和上边的一样

1.生成服务器管理员公钥。

    执行命令ssh-keygen -t rsa,操作时刻参照第一部分第7步,用于提供给服务器端初始化gitosis。

 3.从服务器 克隆gitosis-admin.git

    首先选定目录,如/home/yourname/

    然后,新建一个目录mkdir gitproject

    git clone git@YOUR_SERVER:gitosis-admin.git

    ( YOUR_SERVER可以是你的服务器名称或者ip,我这里的ubuntu的ip为192.168.1.102 )

4. 编辑 gitosis.conf

     cd gitosis-admin

     你将看到以下文件gitosis.conf和keydir/,gitosis.conf是gitosis的配置文件,用于配置用户和权限,keydir/是所有组成员的

公钥。

     vi gitosis.conf, 打开gitosis.conf文件,内容为:

     [group gitosis-admin]
     writable = gitosis-admin
     members =  Administrator@LILIN

     这个是管理组的权限,members中的用户名就是刚才上传的公钥里面的用户名。

      把内容修改为:

     [gitosis]
     gitweb = yes

     [group gitosis-admin]
     writable = gitosis-admin
     members = Administrator@LILIN wirror800@192.168.1.102

     [group apps ]
     writable = apps
     members = Administrator@LILIN wirror800@192.168.1.102

     其中 gitweb = yes表示启用对gitweb的支持。 这里定义了一个叫apps 的组,授予Administrator@LILIN和wirror800@192.168.1.102

两个用户写“apps ”这个repo的权限

       然后push到服务端后,服务器端就立刻生效。

     git commit -a -m “created a new repository”

     git push

     这样就 更新了服务端的权限。

 

5.创建一个app的repo

     mkdir apps

     cd apps

     git init

6.创建、编辑 .gitignore , 用于忽略某些不需要进行代码管理的内容,如rails的应用可能如下:

    .DS_Store
      log/*.log
      tmp/**/*
      config/database.yml
      db/*.sqlite3

  

7.编辑 .git/description 内容, 加上apps 的说明在gitweb 中显示,介绍下项目,刚开始我也不知道这个干嘛的,后来打开web发现description是空的

8. 提交代码

    git remote add origin git@YOUR_SERVER:apps.git

     git add .

     git commit -am "imitial import"

    git push origin master:refs/heads/master

注意:这是你的repositories中就会有app.git 了, 要chmod 777 app.git,这样gitweb中才能看到,

另外要修改.git/config 加上 ,否则git pull 是有问题

[branch "master"]
         remote = origin
          merge = refs/heads/master

增加其他分支

git branch test

git push orgin test:test

修改 .git/config


[branch "test"]
         remote = origin
          merge = refs/heads/test



9.增加成员的公钥到系统中

    cd ../gitosis-admin

    cp member_1.pub keydir/

    cp member_2.pub keydir/ git

    add keydir/member_1.pub keydir/member_2.pub

 

    修改gitosis.conf,并提交

    [group apps]
    - members = wirror800
    + members = wirror800 member_1 member_2
    writable = apps

 

    git commit -a -m "Granted new members commit rights to apps"
    git push

 10. 其它成员获取apps的代码

    git clone git@YOUR_SERVER:apps.git

11: 采用git clone git://yourip/app.git 方式让所有人git clone

a: sudo apt-get install git-daemon-run

b: sudo vi /etc/service/git-daemon/run

"$(git --exec-path)"/git-daemon --verbose --export-all \
    --base-path=/home/git/repositories/ 代替下边


  #"$(git --exec-path)"/git-daemon --verbose --reuseaddr \
   #--base-path=/var/cache /var/cache/git

c: sudo sv down git-daemon

d: sudo sv up git-daemon

这样就可以git clone git://yourip/app.git

*****************************************************************************************


三、常见问题

首先确定 /home/git/repositories/gitosis-admin.git/hooks/post-update 为可执行即属性为 0755

1. git操作需要输入密码

原因
公密未找到
解决
上传id_pub.rsa到keydir并改为'gitosis账号.pub'形式,如miao.pub。扩展名.pub不可省略

2. ERROR:gitosis.serve.main:Repository read access denied

原因
gitosis.conf中的members与keydir中的用户名不一致,如gitosis中的members = foo@bar,但keydir中的公密名却叫foo.pub
解决
使keydir的名称与gitosis中members所指的名称一致。
改为members = foo 或 公密名称改为foo@bar.pub

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多