CentOSにgitosis入れてgitwebからも参照出来るように

今までsvn使ってたけどそろそろgitに移行しなきゃなって思ったけどgitって接続時にパスワードとかパスフレーズとか求められるようなリモートホストとの接続は難しいんですね。だからと言ってクライアント認証しないのはセキュリティ的にどーだとかでと思ってるとgitosisってツールを使えばクライアントの公開鍵を予めサーバ側に登録しておく事でユーザの識別が可能なよう。gitosisを使って管理する各プロジェクトの設定もgitで管理するってのはちょっと面白い。


CentOS等の場合、yum等のパッケージインストールコマンドにGit関連が含まれていない場合がある。
その為、以下のコマンドでfedora用のパッケージをCentOSでも利用可能にする。

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm


Gitのインストール

yum install git-core


gitosisのインストール

yum install gitosis


gitユーザの追加とパスワードの設定

useradd -g root gitosis
passwd gitosis

※今回は自動でユーザ"gitosis"、グループ"gitosis"が作成されたのでそちらを使用。


gitosisの初期化

sudo -H -u gitosis gitosis-init < /root/.ssh/id_rsa.pub


gitosisの初期設定

mkdir /root/gitwork
cd /root/gitwork
git clone gitosis@your-domain.com:gitosis-admin.git


gitosisのユーザ追加

cd /root/gitwork/gitosis-admin


「/root/gitwork/gitosis-admin/gitosis.conf」ファイルを編集

[gitosis]

[group gitosis-admin]
writable = gitosis-admin
members = root@your-domain.com user-name@host-name


追加対象ユーザの公開鍵を管理ディレクトリ以下にコピー(追加対象クライアント上での操作)

sudo scp ~/.ssh/id_rsa.pub root@your-domain.com:/root/gitwork/gitosis-admin/keydir/user-name@host-name.pub


変更分をcommit&push

git add -u && git add .
git commit -av
git push


gitwebのインストール

yum install gitweb


バーチャルホストの設定「/etc/httpd/conf.d/vhosts.conf」の編集

<VirtualHost *:80>
    ServerAdmin user-name@your-domain.com
    ServerName git.your-domain.com

    SetEnv  GITWEB_CONFIG   /var/lib/gitosis/gitosis/gitweb.conf

    Alias /gitweb.css /var/lib/gitosis/gitweb.css
    Alias /git-logo.png /var/lib/gitosis/git-logo.png
    Alias /git-favicon.png /var/lib/gitosis/git-favicon.png

    ScriptAlias /cgi-bin /var/lib/gitosis

    DocumentRoot /var/lib/gitosis/repositories
    <Directory /var/lib/gitosis/repositories>
        Options Indexes FollowSymlinks ExecCGI
        AllowOverride None
        Order allow,deny
        Allow from all

        DirectoryIndex /cgi-bin/gitweb.cgi

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^.* /cgi-bin/gitweb.cgi/$0 [L,PT]
    </Directory>

    LogLevel debug
    ErrorLog logs/git.your-domain.com-error_log
    CustomLog logs/git.your-domain.com-access_log common
</VirtualHost>


gitwebの実行ファイル等をgitosisのホームディレクトリ以下にコピー

cp /var/www/git/* /var/lib/gitosis/


参照制限の為ベーシック認証設定

htpasswd -c /var/lib/gitosis/.htpasswd root


ファイル「/var/lib/gitosis/.htaccess」を編集(作成)

AuthUserFile /var/lib/gitosis/.htpasswd
AuthGroupFile /dev/null
AuthName "Please enter your ID and password"
AuthType Basic
require valid-user


gitwebの設定ファイルをコピー

cp /usr/share/doc/gitosis-0.2/gitweb.conf /var/lib/gitosis/gitosis/


「/var/lib/gitosis/gitosis/gitweb.conf」を編集

# Include the global configuration, if found.
do "/etc/gitweb.conf" if -e "/etc/gitweb.conf";

# Point to projects.list file generated by gitosis.
# Here gitosis manages the user "git", who has a
# home directory of /srv/example.com/git
#$projects_list = "/srv/example.com/git/gitosis/projects.list";
$projects_list = "/var/lib/gitosis/gitosis/projects.list";

# Where the actual repositories are located.
#$projectroot = "/srv/example.com/git/repositories";
$projectroot = "/var/lib/gitosis/repositories";

# By default, gitweb will happily let people browse any repository
# they guess the name of. This may or may not be what you wanted.  I
# choose to allow gitweb to show only repositories that git-daemon
# is already sharing anonymously.
#$export_ok = "git-daemon-export-ok";
$export_ok = "";

# Alternatively, you could set these, to allow exactly the things in
# projects.list, which in this case is the repos with gitweb=yes
# in gitosis.conf. This means you don't need daemon=yes, but you
# can't have repositories hidden but browsable if you know the name.
# And note gitweb already allows downloading the full repository,
# so you might as well serve git-daemon too.
# $export_ok = "";
# $strict_export = "true";

# 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://example.com');
@git_base_url_list = ('git://your-domain.com');

$feature{'search'}{'default'} = [1];
$feature{'blame'}{'default'} = [1];
$feature{'pickaxe'}{'default'} = [1];
$feature{'grep'}{'default'} = [1];
$feature{'snapshot'}{'default'} = [1];


gitosisグループにapacheユーザを追加

usermod -G gitosis apache


gitosis-adminのgitosis.confにgitwebからの参照許可設定を追加

[gitosis]
gitweb = yes

[group gitosis-admin]
writable = gitosis-admin git-test
members = root@your-domain.com user-name@host-name

[repo git-test]
description = Git Test Project
owner = user-name@your-domain.com
gitweb = yes


これでgitosisでいろんなリモートホストからgitのサーバレポジトリにpushしたりpullしたり変更差分をgitwebでブラウザ上で確認したり出来る。