2012年6月19日火曜日

nginxでgitリポジトリの公開 - Smart HTTPを試してみた



nginxでgitリポジトリを公開してみます。
Smart HTTPというのが使えるようですが、こちらの例ではApache HTTP Serverでしたが、
特別なことは必要なさそうなので、nginxで試してみます。
80番ポートで通信できるとか、認証方法によっては鍵が必要なかったりといろいろ使い道がありそうですね。


こちらを参考にしました。



nginx 1.2.1を使用しますが、インストール手順は省略します。
上記の記事によるとgit-http-backendを動かす必要があるので、fcgiwrapを使ってみます。

fcgiwrapインストール




  • fcgiwrapインストール

yum -y install fcgi-devel.x86_64
cd /usr/loca/src
git clone git://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
autoreconf -i
./configure --prefix=/usr/local/fcgiwrap
make
make install



  • fcgiwrapの起動

sudo -u www /usr/local/fcgiwrap/sbin/fcgiwrap -c2 -s unix:/tmp/fcgiwrap.sock >> /var/log/fcgiwrap.log &



nginx設定




  • 試しに設定したserverセクションです。

ドメイン名、パス、認証方法などは適宜変更してください。

server {
listen   80;
server_name git.example.com;

access_log /usr/local/nginx/logs/git/access_log;
error_log /usr/local/nginx/logs/git/error_log;

location / {
auth_basic            "Restricted";
auth_basic_user_file  htpasswd;

include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/local/git/libexec/git-core/git-http-backend;
fastcgi_param GIT_PROJECT_ROOT /var/git;
fastcgi_param GIT_HTTP_EXPORT_ALL "";
fastcgi_param PATH_INFO $uri;

fastcgi_pass unix:/tmp/fcgiwrap.sock;
}
}


設定が完了したらnginxを再起動します。
認証が無いと誰でもアクセスできるので、なんらかの認証は設定した方がいいですね。



gitリポジトリ作成




  • 空のリポジトリ作成と初期設定

git init --bare --shared=group foobar.git
cd foobar.git
cp hooks/post-update.sample hooks/post-update
git update-server-info
git config http.receivepack true



  • パーミッションの設定

環境の合わせて適宜gitリポジトリのディレクトリに設定してください。

動作確認



git clone http://git.example.com/foobar.git
Cloning into 'foobar'...
Username:
Password:
warning: You appear to have cloned an empty repository.

cd foobar
touch test.txt
git add .

git commit -m 'test'
[master (root-commit) b578ac5] test
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
git push origin master

git push origin master
Username:
Password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 206 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://git.example/com/foobar.git
* [new branch]      master -> master



pushができない(receivepackが有効でない場合のエラー)


receive-packの設定が無効の場合は、pushができませんでした。
アクセスログに401やWebDAVのPROPFINDメソッドとか出てたりしたので若干混乱します。
原因は、下記のようにfcgiwrapが出力するreceive-packの設定でした。

gitコマンドのエラー
error: Cannot access URL http://git.example.com/foobar.git/, return code 22
fatal: git-http-push failed

fcgiwrapのエラー
Service not enabled: 'receive-pack'


これらの必要な設定(http.receivepack)は、git-http-backend(1) Manual Pageに書いてありますね。

httpでも一通りgitが使えることが確認できました。




0 件のコメント:

コメントを投稿