Amazon EC2にgit serverをインストールする [aws][git]

まずは、git データを保存する用のボリュームを EBS で作成します。

% ec2-create-volume --region us-west-1 -z us-west-1b -s 1
VOLUME vol-217b854d 1 us-west-1b creating 2011-05-05T02:31:31+0000
% ec2-attach-volume --region us-west-1 -d /dev/sdb -i i-a2c5efe6 vol-217b854d
ATTACHMENT vol-217b854d i-a2c5efe6 /dev/sdb attaching 2011-05-05T02:33:03+0000
% ec2-describe-volumes --region us-west-1
VOLUME vol-b01ae9da 15 snap-ad7ed5c6 us-west-1a available 2010-11-28T09:24:07+0000
VOLUME vol-ed0af481 15 snap-455e2c2e us-west-1b available 2011-05-05T02:00:53+0000
VOLUME vol-6d09f701 15 snap-455e2c2e us-west-1b available 2011-05-05T02:06:06+0000
VOLUME vol-e505fb89 15 snap-455e2c2e us-west-1b in-use 2011-05-05T02:15:18+0000
ATTACHMENT vol-e505fb89 i-a2c5efe6 /dev/sda1 attached 2011-05-05T02:15:36+0000
VOLUME vol-217b854d 1 us-west-1b in-use 2011-05-05T02:31:31+0000
ATTACHMENT vol-217b854d i-a2c5efe6 /dev/sdb attached 2011-05-05T02:33:10+0000


作成したボリュームをマウントします。

% ssh -i ~/.ssh/ec2-keypair.pem ubuntu@ec2-50-18-69-25.us-west-1.compute.amazonaws.com
ubuntu@ip-10-167-9-55:~$ sudo mkfs -t ext3 /dev/sdb
ubuntu@ip-10-167-9-55:~$ sudo mkdir /vol
ubuntu@ip-10-167-9-55:~$ sudo mount /dev/sdb /vol
ubuntu@ip-10-167-9-55:~$ ls /vol
lost+found


gitをインストールし、共有リポジトリを設定します。

ubuntu@ip-10-167-9-55:~$ sudo groupadd gitusers
ubuntu@ip-10-167-9-55:~$ sudo usermod -a -G gitusers <ユーザ名>
ubuntu@ip-10-167-9-55:~$ exit
% ssh -i ~/.ssh/ec2-keypair.pem ubuntu@ec2-50-18-69-25.us-west-1.compute.amazonaws.com
ubuntu@ip-10-167-9-55:~$ umask 002
ubuntu@ip-10-167-9-55:~$ mkdir -p /vol/pub/repositories/gitusers.git
ubuntu@ip-10-167-9-55:~$ cd /vol/pub/repositories/gitusers.git
ubuntu@ip-10-167-9-55:~$ chgrp gitusers .
ubuntu@ip-10-167-9-55:~$ git --bare init --shared
ubuntu@ip-10-167-9-55:~$ cd $HOME
ubuntu@ip-10-167-9-55:~$ mkdir -p work/git
ubuntu@ip-10-167-9-55:~$ cd work/git
ubuntu@ip-10-167-9-55:~$ git clone /vol/pub/repositories/gitusers.git
ubuntu@ip-10-167-9-55:~$ cd gitusers
ubuntu@ip-10-167-9-55:~$ echo test > test.txt
ubuntu@ip-10-167-9-55:~$ git add test.txt
ubuntu@ip-10-167-9-55:~$ git commit -m "test."
ubuntu@ip-10-167-9-55:~$ git push origin master


https 経由で git リポジトリにアクセスできるようにします。

まずは、apache のインストール。

ubuntu@ip-10-167-9-55:~$ sudo apt-get update
ubuntu@ip-10-167-9-55:~$ sudo apt-get install apache2
ubuntu@ip-10-167-9-55:~$ % cd /etc/apache2/sites-available
ubuntu@ip-10-167-9-55:~$ % sudo cp -a default mysite
ubuntu@ip-10-167-9-55:~$ % sudo a2dissite default
ubuntu@ip-10-167-9-55:~$ % sudo a2ensite mysite
ubuntu@ip-10-167-9-55:~$ % sudo usermod -a -G gitusers www-data
ubuntu@ip-10-167-9-55:~$ % sudo usermod -s /usr/sbin/nologin www-data
ubuntu@ip-10-167-9-55:~$ % sudo service apache2 restart
ubuntu@ip-10-167-9-55:~$ exit
% ec2-authorize --region us-west-1 default -p 80


git の http 公開設定。

% ssh -i ~/.ssh/ec2-keypair.pem ubuntu@ec2-50-18-69-25.us-west-1.compute.amazonaws.com
ubuntu@ip-10-167-9-55:~$ cd /etc/apache2/sites-available
ubuntu@ip-10-167-9-55:~$ sudo vi gitusers.git
<Location /git>
  Options Indexes
  DAV on
  AuthType Basic
  AuthName "Git repository"
  AuthUserFile /etc/apache2/.htpasswd
  Require valid-user
</Location>
ubuntu@ip-10-167-9-55:~$ sudo htpasswd -c /etc/apache2/.htpasswd <ユーザ名>
ubuntu@ip-10-167-9-55:~$ sudo chmod 644 /etc/apache2/.htpasswd
ubuntu@ip-10-167-9-55:~$ sudo a2ensite gitusers.git
ubuntu@ip-10-167-9-55:~$ cd /etc/apache2/mods-available
ubuntu@ip-10-167-9-55:~$ sudo a2enmod dav dav_lock dav_fs
ubuntu@ip-10-167-9-55:~$ cd /vol/pub/repositories/gitusers.git
ubuntu@ip-10-167-9-55:~$ git update-server-info
ubuntu@ip-10-167-9-55:~$ sudo service apache2 restart


以下のコマンドで git clone できるようになりました。

% git clone http://<ユーザ名>@ec2-204-236-137-101.us-west-1.compute.amazonaws.com/git/gitusers.git/


git push もできました。


最後に、これまでに行った設定をスナップショットとして保存しておきます。

% ssh -i ~/.ssh/ec2-keypair.pem ubuntu@ec2-50-18-69-25.us-west-1.compute.amazonaws.com sudo umount /vol
% ec2-detach-volume --region us-west-1 vol-217b854d
% ec2-create-image --region us-west-1 i-20d9f364 --name "Ubuntu 10.04 LTS git"