在centos上搭建git伺服器

來源:互聯網
上載者:User
0.定義     這裡的樣本中出現的主機有三台:localhost是一台centos主機,也是git伺服器;ubuntu是git伺服器管理員的workstation;linux是某個git使用者jason的workstation。    localhost即git伺服器上出現了兩個賬戶test和git,test是用來搭建git伺服器的已存在賬戶,git是為git伺服器建立的專有賬戶。    ubuntu是git伺服器管理員的workstation,該管理員在自己的這台workstation上的賬戶是user。    linux是jason的workstation。git server:                 [test@localhost]     [git@localhost]git administrator:      [user@ubuntu]git user:                    [jason@linux]    文中省略了ssh在各主機間的登入命令及scp複製公開金鑰的過程,注意觀察命令前的使用者及主機名稱提示即可。1.安裝git
[test@localhost ~]$ sudo yum install git

     檢查git是否安裝正確

[test@localhost ~]$ git --versiongit version 1.7.1
2.為git伺服器建立專有使用者     通常將該使用者取名git
[test@localhost ~]$ sudo useradd git -d /home/git
   最後切換到git使用者
[test@localhost ~]$ su - git
3.安裝gitolite        gitolite是一款git服務管理工具,通過公開金鑰對使用者進行認證,並能夠利用設定檔進行repo的精細授權管理。由於它採用ssh公開金鑰認證,所以先要安裝ssh。
[test@localhost ~]$ sudo yum install ssh[test@localhost ~]$ sudo service sshd start[test@localhost ~]$ sudo chkconfig sshd on

   
然後準備安裝gitolite,git伺服器的管理員需要先準備自己的金鑰組。所以,假設這個管理員在自己的workstation(另一台linux主機,這裡只是為了得到管理員自己的密鑰,並非一定要在另一台linux機器上)上,他需要建立自己的金鑰組(方便起見,不要輸入passphrase):

[user@ubuntu ~]$  ssh-keygen -f ~/.ssh/admin

       該命令在~/.ssh目錄下建立金鑰組admin和admin.pub。    現在回到git伺服器主機,將剛建立的admin.pub複製到git使用者的家目錄下,即/home/git/下,並且chown為git賬戶。另外,在安裝前須保證不存在檔案~/.ssh/authorized_keys或該檔案為空白。    安裝gitolite:

[git@localhost ~]$ git clone git://github.com/sitaramc/gitolite[git@localhost ~]$ mkdir -p ~/bin[git@localhost ~]$ gitolite/install -to ~/bin[git@localhost ~]$ gitolite setup -pk admin.pub

   如果在執行第三條命令時出現錯誤:

Can't locate Time/HiRes.pm in @INC (@INC contains: /home/git/gitolite/src/lib /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5 /usr/share/perl5 .) at /home/git/gitolite/src/lib/Gitolite/Common.pm
line 76.
BEGIN failed--compilation aborted at /home/git/gitolite/src/lib/Gitolite/Common.pm line 76.
Compilation failed in require at gitolite/install line 15.
BEGIN failed--compilation aborted at gitolite/install line 15.

    說明缺少perl需要的軟體Time::HiRes,安裝該軟體包後,重新執行上面的命令:

[test@localhost ~]$ sudo yum install perl-Time-HiRes

4.添加使用者    現在,假設team裡有個成員叫jason,他將自己的公開金鑰jason.pub郵件給管理員,要求為他建立一個名為foo的repo,他要求該repo僅自己可以修改,其他人不能修改但可以查看。首先管理員在自己的workstation上先要擷取gitolite的管理repo,the_git_host是管理員剛搭建的git伺服器位址:
[user@ubuntu ~]$  git clone git@the_git_host:gitolite-admin

   注意,執行該命令時,如果被要求輸入密碼,說明前面某些配置出錯了,需要重新查證後再繼續。   複製完成後,在./gitolite-admin目錄下需要關注兩個子目錄:conf和keydir。conf是gitolite的許可權設定檔夾,keydir用於放置所有使用者的公開金鑰。所以,現在可以將jason的公開金鑰jason.pub放入檔案夾keydir。然後編輯conf/gitolite.conf檔案,在檔案末尾添加新的repo:

repo foo            RW+         =   jason            R           =   @all
    提交更改,完成使用者及其庫的添加:
[user@ubuntu ~]$  git add conf[user@ubuntu ~]$  git add keydir[user@ubuntu ~]$  git commit -m 'added foo, gave access to jason'[user@ubuntu ~]$   git push
5.使用者執行git版本管理
[jason@linux ~]$   git clone git@the_git_host:foo

     
命令執行完成,建立一個空庫foo,現在jason就可以進行版本管理,在需要的時候進行提交。    如果使用者想要查詢自己有權訪問的所有repo,可以使用下面命令查詢:

[jason@linux ~]$ ssh git@the_git_host  info
    注意:從第3步開始,任何地方使用ssh或git登入到git伺服器需要輸入密碼,都說明配置git伺服器出現錯誤,需要重新安裝gitolite,重新安裝前先清除之前的檔案:
[git@localhost ~]$ ls -a | grep gitolite | xargs rm -fr[git@localhost ~]$ rm -fr ~/repositories ~/bin  ~/projects.list ~/.ssh/authorized_keys

6.配置gitweb    如果想要能夠在網頁上訪問git庫,就可以利用gitweb。

[test@localhost ~]$ sudo yum install gitweb

      開啟/etc/gitweb.conf檔案,按照注釋的格式添加projectroot變數,指向git庫:

our  $projectroot = "/home/git/repositories";our @git_base_url_list = qw(git://git.the_git_host                            ssh://git.the_git_host/var/lib/git);

     最後編輯apache伺服器設定檔/etc/httpd/conf/httpd.conf,在文末添加:

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

     最後修改git庫的許可權,否則會出現404 no projects found錯誤,最後重啟Apache伺服器:

[test@localhost ~]$ sudo chmod 775 /home/git/repositories[test@localhost ~]$ sudo chmod 775 /home/git[test@localhost ~]$ sudo apachectl restart

     最後在瀏覽器裡鍵入http://the_git_host就可以看到git庫了,我在本機測試,使用的是http://localhost

     註:如果在完成上述操作後,仍然顯示404 no project found,那很可能又是SELinux惹的麻煩,嘗試更改selinux的狀態為permissive後再重新整理頁面試試:

[test@localhost ~]$ sudo setenforce 0
7.參考文獻1.gitolite2.gitweb

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.