如果你的網站是利用 github 來做管理,那這篇對你大概沒什麼幫助,因為 github 通通都幫你做完啦!

最近我負責的專案需要改版,考慮到寫程式的人不再是只有我一個,決定來把 git web 介面弄起來,就可以直接在瀏覽器看 git repositories。

前提:
假設你已經用 gitosis 架設好你的 git server。
我的機器是跑 Debiannginx web server。

架設 PHP-FastCGI
如果機器一開始就是打算跑 Ruby on Rails ,那上面很可能會沒有 phpperl 環境,但是我使用的架設方式跑 fcgi ,所以需要先完成這個步驟。

參考文件: PHP-FastCGI with Nginx - Nginx and PHP-FastCGI on Debian 5 (Lenny) - Linode Library

1. 先安裝套件

apt-get update
apt-get upgrade
apt-get install php5-cli php5-cgi libfcgi-perl psmisc gitweb

2. 安裝 spawn-fcgi ,如果 spawn-fcgi 網站有釋出新版本請自動換掉下面的程式碼

cd /opt
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
tar -zxf spawn*
cd spawn*
./configure
make
cp src/spawn-fcgi /usr/bin/spawn-fcgi

3. 讓 php-fastcgi 跑起來,完成後可以 ps aux 檢查一下

cd /opt
wget https://library.linode.com/web-servers/nginx/php-fastcgi/reference/php-fastcgi-deb.sh
mv php-fastcgi-deb.sh /usr/bin/php-fastcgi
chmod +x /usr/bin/php-fastcgi
wget https://library.linode.com/web-servers/nginx/php-fastcgi/reference/php-fastcgi-init-deb.sh
mv php-fastcgi-init-deb.sh /etc/init.d/php-fastcgi
chmod +x /etc/init.d/php-fastcgi
update-rc.d php-fastcgi defaults
/etc/init.d/php-fastcgi start

4. 設定 nginx

# /etc/nginx/nginx.conf
server {
  listen      80;
  server_name gitweb.yoursite.com.tw;

  access_log /var/log/nginx/gitweb.access.log;
  error_log  /var/log/nginx/gitweb.error.log;

  # First rewrite rule for handling maintenance page
  if (-f $document_root/system/maintenance.html) {
      rewrite ^(.*)$ /system/maintenance.html last;
      break;                                                                                                 
  }

  location / {
     fastcgi_pass 127.0.0.1:7000;

     fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
     fastcgi_param QUERY_STRING $query_string;

     fastcgi_param REQUEST_METHOD $request_method;
     fastcgi_param CONTENT_TYPE $content_type;
     fastcgi_param CONTENT_LENGTH $content_length;
     fastcgi_param REQUEST_URI $request_uri;
     fastcgi_param DOCUMENT_URI $document_uri;
     fastcgi_param DOCUMENT_ROOT $document_root;
     fastcgi_param SERVER_PROTOCOL $server_protocol;
     fastcgi_param GATEWAY_INTERFACE CGI/1.1;
     fastcgi_param SERVER_SOFTWARE nginx;
     fastcgi_param REMOTE_ADDR $remote_addr;
     fastcgi_param REMOTE_PORT $remote_port;
     fastcgi_param SERVER_ADDR $server_addr;
     fastcgi_param SERVER_PORT $server_port;
     fastcgi_param SERVER_NAME $server_name;
  }

  location /static { root /home/git/gitweb; }

接著,我是到 git 帳號的家目錄下 mkdir gitweb,把 /usr/share/gitweb 裡面有三個檔案都複製到 /home/git/gitweb/static/ 。

5. 設定 gitweb

# /etc/gitweb.conf

# path to git projects (<project>.git)
$projectroot = "/home/git/repositories";
 
# directory to use for temp files
$git_temp = "/tmp";
 
# target of the home link on top of all pages
#$home_link = $my_uri || "/";
 
# html text to include at home page
$home_text = "indextext.html";
 
# file with project list; by default, simply scan the projectroot dir.
$projects_list = "/home/git/gitosis/projects.list";
 
# stylesheet to use
$stylesheet = "/static/gitweb.css";
 
# logo to use
$logo = "/static/git-logo.png";
 
# the 'favicon'
$favicon = "/static/git-favicon.png";
 
# A list of base urls where all the repositories can be cloned from.
# # Easier than having per-repository cloneurl files.
@git_base_url_list = ('git+ssh://git@yoursite.com.tw');

6. 設定 gitosis

我有設定 gitosis-admin.git ,直接編輯 gitosis.conf ,再 git push 更新設定。
記得 repo, owner, description 都要改成自己的。 

# gitosis.conf

...
[repo your-repository]
gitweb = yes
owner = wildjcrt
description = "Your repository's description"
...

7. 製作 gitweb.fcgi 執行檔

把下面這對程式碼全部複製後儲存,我是存到 /home/git/gitweb/gitweb.fcgi ,然後 chmod +x gitweb.fcgi 。

#!/usr/bin/perl -w

use strict;
use FCGI;
use CGI;
use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;

sub usage {
       print STDERR "$0 --fcgi-socket=(path|[host]:port) ",
                    "--cgi-bin=path\n";
       exit 1;
}

my ($fcgi_sock, $cgi_bin);
GetOptions('fcgi-socket|s=s' => \$fcgi_sock,
           'cgi-bin|c=s' => \$cgi_bin) or usage();

usage() unless ($fcgi_sock && $cgi_bin);

die "FastCGI socket: $fcgi_sock already exists!\n" if (-S $fcgi_sock);
die "CGI executable: $cgi_bin does not exist!\n" if (!-f $cgi_bin);

# gitweb will exit, make it throw an exception instead:
no warnings qw/once/;
*CORE::GLOBAL::exit = sub { die 'gitweb_exit' };
use warnings;

# FCGI will erase the current %ENV; so make sure we save this:
my $gwcfg = $ENV{GITWEB_CONFIG};

my $fcgi_req = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV,
                             FCGI::OpenSocket($fcgi_sock, 128),
                             FCGI::FAIL_ACCEPT_ON_INTR);
while ($fcgi_req->Accept >= 0) {
       unless ($ENV{PATH_INFO}) {
               # nginx currently fails to set PATH_INFO,
               # so we'll do it ourselves
               my $pi = $ENV{SCRIPT_NAME};
               $pi =~ s!^/\+!!;
               $ENV{PATH_INFO} = $pi;
       }
       # clear CGI query parameters set inside gitweb so we can reparse
       # the %ENV fed to us
       CGI::initialize_globals();
       $ENV{GITWEB_CONFIG} = $gwcfg if defined $gwcfg;
       do $cgi_bin;
       delete $ENV{PATH_INFO};
}

END {
       unlink $fcgi_sock if (defined $fcgi_sock && -S $fcgi_sock);
}

8. 建立 start-gitweb.sh

跟步驟7類似,我是存到 /home/git/gitweb/start-gitweb.sh ,然後 chmod +x start-gitweb.sh

/home/git/gitweb/gitweb.fcgi --fcgi-socket=127.0.0.1:7000 --cgi-bin=/usr/lib/cgi-bin/gitweb.cgi &

最後跑 ./start-gitweb.sh , ps aux 檢查一下,沒問題的話到 http://gitweb.yoursite.com.tw 應該就可以看到你的 git repositories 囉!

 

延伸閱讀: 在 gitweb 網站上加入簡單的 htpasswd 帳號管理

 

參考文件整理:
Pro Git - Pro Git 4.6 服务器上的 Git 网页界面 GitWeb
Pro Git - Pro Git 4.7 服务器上的 Git 权限管理器 Gitosis
Debian Linux 架設 Gitweb | Tsung's Blog
PHP-FastCGI with Nginx - Nginx and PHP-FastCGI on Debian 5 (Lenny) - Linode Library
Gitweb with gitosis & nginx HowTo

arrow
arrow
    全站熱搜

    笨笨小蟹 發表在 痞客邦 留言(1) 人氣()