天天看点

阿里云搭建私有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的话虽然也能用,但会导致无法使用命令访问代码仓库,原因我还不清楚。之前的写错了,所以在这更正下。