gitweb + Nginx を使ってgitリポジトリをWEBで見られるようにする

今回参考にしたのは、こちらのサイト。

パッケージのインストール

CentOSでnginxをyumを使ってインストールする場合には、nginxのyumリポジトリを追加しておく必要がある。

vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=0

私は、デフォルト以外のリポジトリはenabled=0にしておく人なので、0にしているがnginxレポだけであれば、変更しなくてもいいと思う。

gitweb と nginx をインストールする。

# yum install gitweb nginx --enablrepo=nginx

gitweb の設定

/etc/gitweb.conf を編集する。

our $projectroot = "/home/git/repositories";

nginx と FastCGIを連携するための設定をする

FastCGI関連のPerlモジュールをインストールする。

# yum install spawn-fcgi perl-FCGI perl-FCGI-ProcManager --enablerepo=epel

nginx から fastcgi で動かしたgitweb にアクセスできるように/etc/nginx/conf.d/gitweb.conf に設定する。

server {
    listen *:80;
    root /var/www/git;
    index gitweb.cgi;

    server_name gitweb.example.net;

    location ~ gitweb\.cgi$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index gitweb.cgi;
        include fastcgi_params;
    }
}

nginx を起動する。

# service nginx start

gitwebをFastCGIで動かす

gitweb.fcgi を 下記サイトから頂いてくる。

内容はこんな感じで、今回は /home/git/bin/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);
}

/var/www/git/gitweb.cgi を編集する(該当箇所のみ)。

our $projectroot = "/home/git/repositories";

fastcgiでgitwebを動かす。

# sudo -u git /home/git/bin/gitweb.fcgi --fcgi-socket=127.0.0.1:9000 --cgi-bin=/var/www/git/gitweb.cgi &

これでブラウザからアクセスすれば、gitwebが確認できる。

ただ、参照したサイトにも書いてあるとおり、redifine のエラーが出る。これについては、後でちゃんと調べる。