git web 服务器的搭建【转】

简介: 转自:http://blog.csdn.net/transformer_han/article/details/6450200 目录(?)[-] git服务器搭建过程 需求 硬件需求一台Ubuntu或者debian电脑虚拟机能通过网络访问到 软件需求git-core gitosi...

转自:http://blog.csdn.net/transformer_han/article/details/6450200

git服务器搭建过程

参考网上资料搭建git服务器过程记录 如下:

需求

硬件需求:一台Ubuntu或者debian电脑(虚拟机),能通过网络访问到。

软件需求:git-core, gitosis, openssh-server, openssh-client, Apache2(Gitweb)

安装配置git服务器

安装git和openssh:
a@server:~$ sudo apt-get install git-coreopenssh-serveropenssh-client

新加用户git, 该用户将作为所有代码仓库和用户权限的管理者:
a@server:~$ sudo useradd -m git
a@server:~$ sudo passwd git
建立一个git仓库的存储点:
a@server:~$ sudo mkdir /home/repo
让除了git以外的用户对此目录无任何权限:
a@server:~$ sudo chown git:git /home/repo
a@server:~$ sudo chmod 700 /home/repo

安装配置gitosis

初始化一下服务器的git用户,这一步其实是为了安装gitosis做准备。在任何一 台机器上使用git,第一次必须要初始化一下:
a@server:~$ git config –global user.name “myname”
a@server:~$ git config –global user.email “myname@server

安装一下python的setup tool, 这个也是为了gitosis做准备:
a@server:~$ sudo apt-get install python-setuptools

获得gitosis包:
a@server:~$ cd /tmp
a@server:/tmp$ git clone git://eagain.net/gitosis.git
a@server:/tmp$ cd gitosis
a@server:/tmp/gitosis$ sudo python setup.py install

切换到git用户下:
a@server:/tmp/gitosis$ su git
默认状态下,gitosis会将git仓库放在 git用户的home下,所以我们做一个链接到/home/repo
$ ln -s /home/repo /home/git/repositories
再次返回到默认用户
$ exit

如果你将作为git服务器的管理员,那么在你的电 脑上(另一台pc)生成ssh公钥:
usr@pc1:~$ ssh-keygen -t rsa
将公钥拷贝到服务器的/tmp下:
usr@pc1:~$ scp .ssh/id_rsa.pubgit@<server>:/tmp
回到git服务器上
a@server:/tmp/gitosis$ sudo chmod a+r /tmp/id_rsa.pub

让gitosis运行起来:
a@server:/tmp/gitosis$ sudo -H -u git gitosis-init < /tmp/id_rsa.pub
Initialized empty Git repository in /home/repo/gitosis-admin.git/
Reinitialized existing Git repository in /home/repo/gitosis-admin.git/

gitosis的有趣之处在于,它通过一个git仓库来管理配置文件,仓库就放在了/home/repo/gitosis- admin.git。我们需要为一个文件加上可执行权限:
a@server:/home/git$ sudo passwd root
a@server:/home/git$ su
root@server:/home/git# cd repositories
root@server:/home/git/repositories# cd gitosis-admin.git/
root@server:/home/git/repositories/gitosis-admin.git#sudo chmod 755 /home/repo/gitosis-admin.git/hooks/post-update

root@server:/home/git/repositories/gitosis-admin.git# exit

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

我建了一个叫“teamwork”的仓库。
切换到Git用户:
a@ubuntu:/home/git$ su - git
$ cd /home/prj_git
$ mkdir teamwork.git
$ cd teamwork.git
$ git init --bare
$ exit

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

管理gitosis的配置文件

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

在你的电脑里:
usr@pc1:~/work$ git clonegit@<server>:gitosis-admin.git
如果出现:
fatal: '~/gitosis-admin.git' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
改成:

usr@pc1:~/work$ cd gitosis-admin/
该目录下的keydir目录是用来存放所有需要访问git服务器的用户的ssh公钥:
各个用户按照前面提到的办法生成各自的ssh公钥文件后, 把所有人的 ssh公钥文件都拿来,按名字命名一下,比如b.pub, lz.pub等,统统拷贝到keydir下:
修改gitosis.conf文件,我的配置大致如下:
[gitosis]
[group gitosis-admin]
writable = gitosis-admin
members = a@serverusr@pc1
[group hello]
writable = teamwork
members = a@server b
[group hello_ro]
readonly = teamwork
members = lz
这个配置文件表达了如下含义:gitosis-admin组成员有a, usr,该组对gitosis-admin仓库有读写权限;
team组有a,b两个成员,该组对teamwork仓库有读写权限;
team_ro组有lz一个成员,对teamwork仓库有只读权限。

当然目前这些配置文件的修改只是在你的本地,你必须推送到远程的gitserver上才能真正生效。
加入新文件、提交并push到git服务器:
usr@pc1:~/work/gitosis-admin$ git add .
usr@pc1:~/work/gitosis-admin$ git commit -am “add teamweok prj and users”
usr@pc1:~/work/gitosis-admin$ git push origin master

初始化测试项目

好了,现在服务器就搭建完了,并且有一个空的项目teamwork在服务器上。接下来呢?当然是测试一下,空仓库是不能clone的,所以需要某一个有写权限的人初始 化一个版本。就我来做吧,以下是在客户端完成。
usr@pc1:~/work$ mkdir teamwork-ori
usr@pc1:~/work$ cd teamwork-ori/
usr@pc1:~/work/teamwork-ori$ git init
usr@pc1:~/work/teamwork-ori$ echo “/*add something*/” > hello
usr@pc1:~/work/teamwork-ori$ git add .
usr@pc1:~/work/teamwork-ori$ git commit -am “initial version”
到此为止teamwork已经有了一个版本了,team的其他成员只要先clone一下 teamwork仓库,就可以任意玩了。
$ cd teamwork
$ vim hello
$ git add .
$ git commit -am “b add”
$ git push origin master
$ exit

添加已有git项目

另外:如果你有一个现成的git仓库,想放到 gitserver上供team使用(比如你clone了一个官方的kernel仓库,想在内部使用它作为基础仓库),怎么办呢。
首先需要从你的工作仓库中得到一个纯仓库, 比如你的工作目录为~/kernel, 你想导出纯仓库到你的优盘里,然后拷贝到gitserver上去。
$ git clone –bare ~/kernel /media/udisk
然后就拿着优盘,交给gitserver的管理员,让他拷贝到/home/repo/下,同时需要配置 gitosis相关配置文件哦,这个就不用再说了吧。比如:下载ALSA库:
git clone git://android.git.kernel.org/platform/external/alsa-lib.git
git clone git://android.git.kernel.org/platform/external/alsa-utils.git
生成bare库
git clone –bare alsa-lib alsa-lib.git
git clone –bare alsa-utils alsa-utils.git

将bare 库移动到git服务器目录
cp alsa-lib.git /home/repo
注意变更所有者,以获取提交权限。
chown -R git alsa-lib.git
然后就O 了,呵呵.

配置gitweb

1. 安装gitweb 

   sudo apt-get install gitweb

2. 安装apache2

  sudo apt-get install apache2

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

   (注意后面的点)

(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站点 (默认情况下可以忽略此步骤)

(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/cgi-bin/gitweb.cgi

<以下未经测试>

配 置web访问方式:
Apache常用命令:
a2dissite gitserver 禁用
a2ensite gitserver  使能
/etc/init.d/apache2 restart 重启
1.apt-get install apache2
2.手动安 装gitweb
git clone git://git.kernel.org/pub/scm/git/git.git
cd git
make GITWEB_PROJECTROOT=”/home/repo” prefix=/usr gitweb/gitweb.cgi
cd gitweb
cp -av git* /home/repo/
3.vim /etc/apache2/sites-available/gitserver
<VirtualHost 172.20.146.39:80>
ServerName 172.20.146.39
DocumentRoot /home/repo
ScriptAlias /cgi-bin/ /home/repo
<Directory /home/repo>
Options ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch
AllowOverride All
order allow,deny
Allow from all
AddHandler cgi-script cgi
DirectoryIndex gitweb.cgi
</Directory>
</VirtualHost>
4.赋予权限,很重要:
chgrp -R www-data /home/repo
chmod a+r repo
chmod a+x repo
mv hooks/post-update.sample hooks/post-update
5.a2ensite gitserver
6./etc/init.d/apache2 restart
匿名访问方式:
git clone http://192.168.1.1/alsa-lib.git
git clone http://192.168.1.1/alsa-utils.git
git访问方式:
git clone git@192.168.1.1:alsa-lib.git
Web网页浏览:
http://192.168.1.1
遇到的问题:
1.windows文件命名不区分大小 写,而linux支持。这样android源码下载时会出现一下问题。大约有15个文件存在这个问题。
2.库的描述文件在.git文件夹的description文件中。编辑该文件,在gitweb页中就会有 description。
3.gitosis库hooks下的post- update不是由post-update.sample重命名过来的,它们不一样。post-update可以更新工作目录,保持与库一致。没有它配置 文件是不会更新的。
4.(1)git@hello:/home/git$ git add .
error: readlink(“external/openssl/apps/md4.c”): No such file or directory
error: unable to index file external/openssl/apps/md4.c
fatal: adding files failed

(2)root@/external/openssl# git init
Initialized empty Git repository in /external/openssl/.git/
root@/external/openssl# git add .
error: readlink(“apps/md4.c”): No such file or directory
error: unable to index file apps/md4.c
fatal: adding files failed
(3)root@android-2.1_r2$ rm -Rf .repo
root@android-2.1_r2$ find . -name “.git” | xargs rm -Rf



架设git服务器--使用git-daemon 架设基于git 协议服务器:


git是一个不错的版本管理的工具。现在自己在搞一个简单的应用程序开发,想使用git来进行管理。在Google了配置文档后,还是受了N多的挫折。某些文档质量不高,浪费了好多时间......

 

好,切入正题:

 

安装必要的git工具

#apt-get install git git-core

 

安装好了以后,进行设置

1. 创建一个git目录

#mkdir /git

#cd /git/

#mkdir myproject

 

2. 创建一个空的git仓库

#git-init-db

 

3. 创建工程的文件

# echo "My test project" > test.txt


* 此步很重要,如果要在远程clone这个project, 这个project必须是非空的,否则会失败。

 

4. 使用git命令添加并提交新的文件

#git-add test.txt

#git-commit -m "Init"

 

至此,本地的git 仓库就创建好了。想要在远程clone这个project,还需要使用git-daemon

5. 安装git-daemon-run.  git-daemon-run实际是一个脚本管理工具,用来启动git-daemon.

#apt-get install git-daemon

 

6. 配置git-daemon-run

 

#vi /etc/sv/git-daemon/run

 

可以看到

#!/bin/sh
exec 2>&1
echo 'git-daemon starting.'
exec chpst -m64000000 /

git-daemon --verbose --base-path=/var/cache /var/cache/git

 

将最后一句的git-daemon修改为


git-daemon --verbose --export-all --base-path=/git/

 

* 这里,我加上了一个--export-all.看下man手册就可以知道,使用该选项后,在git仓库中就不必创建git-daemon-export-ok文件。如果不使用该选项,则在第4步还需要创建该文件,即

#touch git-daemon-export-ok

 

7.重启系统

由于小弟还不知道如何使用git-daemon-run工具重启git-daemon,干脆重启下系统

 

8. 从client导出server(192.168.35.69)上的myproject

git-clone git://192.168.35.69/myproject

 







=============================================
branch 操作:
在本地:
git branch  test
git checkout test   // git branch  branch1 branch2
git push original test

【作者】 张昺华
【新浪微博】 张昺华--sky
【twitter】 @sky2030_
【facebook】 张昺华 zhangbinghua
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
目录
相关文章
|
7月前
|
监控 前端开发 JavaScript
Git Gulp webpack
Git Gulp webpack
Git Gulp webpack
|
11月前
|
运维 Kubernetes 供应链
【极狐 GitLab】在 web 端合并分支
【极狐 GitLab】在 web 端合并分支
272 0
|
开发工具 git
webpack配置篇(三十七):Git 规范和 Changelog 生成
webpack配置篇(三十七):Git 规范和 Changelog 生成
112 0
webpack配置篇(三十七):Git 规范和 Changelog 生成
|
开发工具 git
GIT上传服务器同步到web目录
GIT上传服务器同步到web目录
142 0
GIT上传服务器同步到web目录
|
缓存 Apache 数据库
SVN无法CO检出web目录
SVN无法CO检出web目录
98 0
SVN无法CO检出web目录
|
Shell 数据安全/隐私保护 Python
gitee+宝塔实现本地代码svn提交到仓库之后自动同步到web站点教程
gitee又叫码云是和github类似的国内代码托管平台,之所有选择码云也是因为它在国内速度比较快,地址:https://gitee.com/
463 0
gitee+宝塔实现本地代码svn提交到仓库之后自动同步到web站点教程
|
Linux 开发工具 数据安全/隐私保护
Centos7搭建SVN服务(多个仓库)并利用svn 钩子(hooks)自动部署仓库代码到指定web目录...
Centos7搭建SVN服务(多个仓库)并利用svn 钩子(hooks)自动部署仓库代码到指定web目录...
180 0
Centos7搭建SVN服务(多个仓库)并利用svn 钩子(hooks)自动部署仓库代码到指定web目录...
|
Shell Linux
本地开发环境通过SVN客户端提交到仓库后自动同步到web站点
本地开发环境通过SVN客户端提交到仓库后自动同步到web站点
100 0
本地开发环境通过SVN客户端提交到仓库后自动同步到web站点
GitLab 如何在 Web 界面中 Merge branch
希望在 GitLab 中对 2 个 branch 进行合并,如何创建 Pull Request 并且如何进行合并呢? 在 GitLib 的 Web 界面中选择 Merge Requests 然后再界面中选择新建一个 Merge Request。
1061 0
如何把SAP WebIDE里的Web项目同Github仓库连接起来
我们在SAP WebIDE里进行UI5应用开发时,当然也希望能将开发的代码纳入到github版本管理中去。 步骤其实非常简单。 右键点击WebIDE里UI5应用,git->Initialize Local Repository: 输入github上的用户名和密码,稍后WebIDE会用这个credential与Github建立连接。
897 0