基于 GitWeb 搭建属于自己的 GIT 服务器前端

陪她去流浪 桃子 2016年06月16日 阅读次数:4328

上一篇文章简单地介绍了如何通过 fcgiwrap 让 nginx 能够和 cgi 程序进行通信。文中的 cgi脚本 是一个简单到几乎没有任何功能的只是用来表明程序正常工作的 shell脚本。这一篇文章就来介绍下通过 nginx + fcgiwrap + gitweb 来搭建属于自己的 Git 服务器前端。

gitweb 是一个由 Perl 写的简单的 CGI脚本 程序,用来生成 Web前端 内容方便在浏览器在查看版本库。

安装

root@ubuntu:~# apt-get install gitweb

安装位置:

root@ubuntu:~# tree /usr/share/gitweb/
/usr/share/gitweb/
|-- gitweb.cgi
|-- index.cgi -> gitweb.cgi
`-- static
    |-- git-favicon.png
    |-- git-logo.png
    |-- gitweb.css
    `-- gitweb.js

1 directory, 6 files

其中 gitweb.cgi 就是主要的 CGI 脚本程序, static 目录下是网站的一些静态资源。

配置文件

root@ubuntu:~# ll /etc/gitweb.conf
-rw-r--r-- 1 root root 698 Mar 21 23:24 /etc/gitweb.conf

配置文件内容:

# path to git projects (<project>.git)
$projectroot = "/var/lib/git";

# directory to use for temp files
$git_temp = "/tmp";

默认的版本库路径是 "/var/lib/git",我就不改了,直接用这个。

服务器端配置

有了前面的使用 fcgiwrap 的经验,要再配一个 gitweb 就太简单了。

location /index.cgi {
    gzip                off;
    fastcgi_param       SCRIPT_FILENAME     /usr/share/gitweb/index.cgi;
    fastcgi_param       GITWEB_CONFIG       /etc/gitweb.conf;
    fastcgi_pass        unix:/var/run/fcgiwrap.socket;
    include             fastcgi_params;
}

location / {
    root    /usr/share/gitweb;
    index   index.cgi;
}

重启 nginx 后,在浏览器在打开 nginx 服务器的地址,应该可以看到一个空的项目列表主页了。

配置仓库

前面通过 gitweb.conf 配置了仓库的路径,现在就可以往里面加东西了。

注意,你只应该往里面加祼仓库,而不应带上工作目录。

可以直接 clone 一个:

root@ubuntu:~# cd /var/lib/git
root@ubuntu:/var/lib/git# ls
root@ubuntu:/var/lib/git# git clone --bare https://github.com/movsb/taoblog.git
Cloning into bare repository 'taoblog.git'...
remote: Counting objects: 1969, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 1969 (delta 5), reused 0 (delta 0), pack-reused 1950
Receiving objects: 100% (1969/1969), 475.01 KiB | 167.00 KiB/s, done.
Resolving deltas: 100% (1370/1370), done.
Checking connectivity... done.

还可以直接软连接过来:

root@ubuntu:/var/lib/git# ln -s ~/taoexec/.git taoexec.git
root@ubuntu:/var/lib/git# ls
taoblog.git  taoexec.git
root@ubuntu:/var/lib/git# ll
total 12
drwxr-xr-x  3 root root 4096 Jun 15 23:53 ./
drwxr-xr-x 37 root root 4096 Dec 19 03:57 ../
drwxr-xr-x  7 root root 4096 Jun 15 23:50 taoblog.git/
lrwxrwxrwx  1 root root   18 Jun 15 23:53 taoexec.git -> /root/taoexec/.git/

请确保项目都是祼仓库:

root@ubuntu:/var/lib/git# ls taoblog.git taoexec.git
taoblog.git:
HEAD  branches  config  description  hooks  info  objects  packed-refs  refs

taoexec.git:
HEAD  branches  config  description  hooks  index  info  logs  objects  packed-refs  refs

最终界面

总结

这东东基本还算能用,不过那URL实在太丑了。这东西貌似没维护了。我用了一段时间后换成了 cgit。后面有空写一篇关于 cgit 的分享。

参考

  1. Gitweb and Nginx | misterpinchy
  2. Git - gitweb Documentation

这篇文章的内容已被作者标记为“过时”/“需要更新”/“不具参考意义”。

标签:git · nginx