天天看點

阿裡雲搭建私有Git及使用者管理在伺服器上建立使用者和項目倉庫(repository)clone項目并送出修改(本地)建立主機信任關系使用gitosis來配置和管理git服務端可能遇到的問題

轉載須注明原文位址:http://blog.csdn.net/btyh17mxy/article/details/12451715

這裡使用的作業系統是CentOS release 5.9 (Final),并且已經安裝了git和一些必要的開發工具。

在伺服器上建立使用者和項目倉庫(repository)

useradd git #建立git使用者
passwd git #設定密碼
su git #切換到git使用者
cd ~/ #切換到home目錄
git init --bare cube #建立一個叫cube的項目倉庫
           

clone項目并送出修改(本地)

clone

git clone [email protected]位址:cube ~/cube
           

需要說明的是這裡會要求輸入使用者git的密碼,我們稍後建立主機信任後就不需要輸入密碼了.

然後在cube下建立一個README.txt

git add *
git commit -m "create new file"
git push origin master
           

建立主機信任關系

在本地執行

ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub
           

拷貝檔案公鑰内容

在伺服器執行

mkdir ~/.ssh
chmod 755 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
echo 拷貝的公鑰 >> ~/.ssh/authorized_keys
           

要注意檔案權限的問題.

使用gitosis來配置和管理git服務端

當服務端項目和使用者數量增多時, 管理将變得複雜起來,好在有gitosis.

初始化gitosis

在本地執行

scp id_rsa.pub IP位址:/tmp/id_rsa.pub #上傳管理者密鑰到伺服器
           

在服務端執行

gitosis-init < /tmp/id_rsa.pub #初始化gitosis
           

在本地配置gitosis-admin

git clone [email protected]位址:gitosis-admin.git
           

clone到本地後會有一個keydir檔案夾和一個gitosis.conf檔案,前者儲存着git使用者的公鑰後者是配置檔案,其配置檔案通常的結構如下:

[gitosis]
gitweb = yes #開啟gitweb支援

[repo foobar]
description = Git repository for foobar
owner = user

[group devs] #一個使用者組
members = user1 user2

[group admins]
members = user1

[group gitosis-admin]
writable = gitosis-admin
members = @admins

[group foobar]
writable = foobar
members = @devs

[group myteam]
writable = free_monkey
members = jdoe

[group deployer]
writable = free_monkey
readonly = monkey_deployer      

關于配置檔案的詳細介紹在 https://wiki.archlinux.org/index.php/gitosis#Repositories_and_permissions

使用gitosis建立項目

在gitosis.conf中添加如下内容:

[group cubelinux-dev]
writable = projectname
members = username #要與用戶端使用的使用者名一緻
           

送出項目到伺服器

然後在本地建立項目目錄

git init projectname #要與上面的名字一樣
git add . 
git commit -a -m "init progect"
           

送出到伺服器

git remote add origin [email protected]位址:progectname.git 
git push origin master
           

開啟gitweb支援

gitweb是一個簡單的可視化網頁界面,可以使用任何相容CGI的網頁服務來運作,這裡選用的Apache

下載下傳編譯安裝

git clone git://git.kernel.org/pub/scm/git/git.git
cd git/
make GITWEB_PROJECTROOT="/home/git/repositories"\
    GITWEB_JS="gitweb/static/gitweb.js" \
    GITWEB_CSS="gitweb/static/gitweb.css" \
    GITWEB_LOGO="gitweb/static/git-logo.png" \
    GITWEB_FAVICON="gitweb/static/git-favicon.png" \
    bindir=/usr/local/bin \
    gitweb
make gitwebdir=/var/www/cgi-bin/gitweb install-gitweb
           

需要注意的是GITWEB_PROJECTROOT應該修改成你的倉庫的路徑,并且其權限(包括子目錄)應是705。 然後修改/etc/gitweb.conf,改成下面的樣子

$projectroot = "/home/git/repositories"; #倉庫路徑
$GIT = "/usr/local/bin/git"; #git可執行檔案路徑
           

修改apache配置檔案

在/etc/httpd/conf/httpd.conf中加入以下内容:

<VirtualHost *:80>
    ServerName gitserver
    DocumentRoot /var/www/cgi-bin/gitweb
    <Directory /var/www/cgi-bin/gitweb>
        Options ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch
        AllowOverride All
        order allow,deny
        Allow from all
        AddHandler cgi-script cgi
        DirectoryIndex gitweb.cgi
    </Directory>
</VirtualHost>
           

賓果,這就弄好了!

可能遇到的問題

使用者認證不通過的問題:

git clone gi[email protected]:gitosis-admin.git

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

Agent admitted failure to sign using the key.

Permission denied (publickey).

fatal: The remote end hung up unexpectedly

// 解決方法,在管理者PC上 

# ssh-add   ~/.ssh/id_rsa 

gitweb找不到工程的問題

如果遇到gieweb的頁面顯示找不到工程,多半是權限設定錯誤。 簡單的說就是Apache使用者組不具備通路你倉庫目錄的權限,例如我的倉庫是放在/home/git/repositories下的,是以應将/home/git和/home/git/repositories以及你倉庫中的所有項目的權限都設定成705,之前在網上發現有人說設定成775的,如果你設定成775的話雖然也能用,但會導緻無法使用指令通路代碼倉庫,原因我還不清楚。之前的寫錯了,是以在這更正下。