Amazon EC2を使ってWebサービスを公開する「インスタンス編」
今回はインスタンスの選定から接続、各種設定を行うところまで行っていきたいと思います。最近話題のmod_railsを使って、手早くRailsアプリケーションを動かします。
CentOSをベースにしようと思ったのですが、CentOS5ではRuby1.8.5が基本になっており(1.8.6に独自ビルドは可能)、その後も色々と手間取ることが多かったので、ここではUbuntuをベースにしたいと思います。
ちょうど良いインスタンスイメージを探すには、Amazon Web Services Developer Connection : Submit an AMI が役立ちます。この中のUbuntuパッケージとして、Amazon Web Services Developer Connection : Ubuntu 7.10 Gutsy Base Install を選択します。
AMI IDは「ami-226e8b4b」とのことなので、まずはこれを覚えておきます。そしてコンソールで起動をします。
$ ec2-run-instances -k ec2-keypair ami-226e8b4b
RESERVATION r-4cbb4525 203519234899 default
INSTANCE i-0938fe60 ami-226e8b4b pending ec2-keypair 0 m1.small 2008-04-13T23:21:59+0000 us-east-1a
のようなレスポンスが返ってきます。この時のi-0938fe60がインスタンスのユニークIDです。しばらく待つと、インスタンスが起動します。
$ ec2-describe-instances
RESERVATION r-4cbb4525 203519234899 default
INSTANCE i-0938fe60 ami-226e8b4b ec2-75-101-215-122.compute-1.amazonaws.com domU-12-31-35-00-04-E1.z-2.compute-1.internal running ec2-keypair 0m1.small 2008-04-13T23:21:59+0000 us-east-1a
では、実際に接続してみます。ec2-75-101-215-122.compute-1.amazonaws.comがアドレスになります。
$ ssh -i ~/ec2-keys/ec2-keypair root@ec2-75-101-215-122.compute-1.amazonaws.com
The authenticity of host 'ec2-75-101-215-122.compute-1.amazonaws.com (75.101.215.122)' can't be established.
:
Linux (none) 2.6.16-xenU #1 SMP Mon May 28 03:41:49 SAST 2007 i686
:
#
このように接続完了しました。まずは色々なサーバ向けソフトウェアを導入していきます。一気にインストールするので長めです。なお、途中でMySQLのrootパスワード設定が表示されるので、見放せません。もし目を離す場合は、mysql-serverだけ先にインストールしておくのが良いかと思います。
# apt-get install mysql-server imagemagick rubygems ruby1.8-dev librmagick-ruby1.8 libmagick9-dev libmysqlclient15-dev gcc apache2 emacs apache2-prefork-dev libapr1-dev apache2-mpm-prefork rake subversion build-essential
Reading package lists... Done
Building dependency tree
Reading state information... Done
:
Get:140 http://us.archive.ubuntu.com gutsy/universe rubygems 0.9.4-1ubuntu1 [51.5kB]
Get:141 http://us.archive.ubuntu.com gutsy/main subversion 1.4.4dfsg1-1ubuntu3 [241kB]
Setting up build-essential (11.3ubuntu1) ...
Processing triggers for libc6 ...
ldconfig deferred processing now taking place
#
次に必要なgemをインストールします。これもまた長い処理です。随時追加インストールするアプリケーションを問い合わせてきます。全てyでかまわないかと思います。最初の起動時にエラーが出る場合もありますが、再度実行するとうまくいきます。
# gem install aws-s3 fastthread openid ruby-openid passenger uuid gettext rails
Bulk updating Gem source index for: http://gems.rubyforge.org
Install required dependency xml-simple? [Yn] Y
:
Successfully installed gettext-1.10.0
Successfully installed rails-2.0.2
#
# /var/lib/gems/1.8/bin/passenger-install-apache2-module
Welcome to the Passenger Apache 2 module installer.
This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.
Here's what you can expect from the installation process:
1. The Apache 2 module will be installed for you.
2. You'll learn how to configure Apache.
3. You'll learn how to deploy a Ruby on Rails application.
:
And that's it! You may also want to check the Users Guide for security and
optimization tips and other useful information:
/var/lib/gems/1.8/gems/passenger-1.0.1/doc/Users guide.html
Enjoy Passenger, a product of Phusion (www.phusion.nl)
http://www.modrails.com/
#
ここまでで必要なインストールは完了です。後は設定を行います。
まずはApache関連です。下記ファイルの作成と、既存のapache2.confの修正です。
# emacs /etc/apache2/sites-enabled/rails.conf
LoadModule passenger_module /var/lib/gems/1.8/gems/passenger-1.0.1/ext/apache2/mod_passenger.so
RailsSpawnServer /var/lib/gems/1.8/gems/passenger-1.0.1/bin/passenger-spawn-server
RailsRuby /usr/bin/ruby1.8
DocumentRoot /home/app/rails/public
<Directory /home/app/rails/public>
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
</Directory>
こちらはapache2.confの修正。
# emacs /etc/apache2/apache2.conf
User app
Group app
# User www-data ← appに変更する
# Group www-data ← appに変更する:
# Include /etc/apache2/sites-enabled/
Include /etc/apache2/sites-enabled/rails.conf # こちらだけ読み込む
次にユーザを追加します。
# adduser app
Adding user `app' ...
Adding new group `app' (1000) ...
Adding new user `app' (1000) with group `app' ...
:
Other []:
Is the information correct? [y/N] y
#
そして作成したユーザになって、railsを実行します。
# su - app
$ mkdir rails
$ /var/lib/gems/1.8/bin/rails rails
exists
create app/controllers
:
create log/test.log
$
ここまでで処理はほぼ完了です。残りはmod_rewriteを有効にして、Apacheの再起動を行うのみです。
$ exit
# a2enmod rewrite
# /etc/init.d/apache2 force-reload
ここまでで最低限必要な処理は完了です。実際にHTTPでアクセスしてみます。
見慣れたRailsの画面
無事見慣れた画面が出てきました。まだデータベースの作成などは残っていますが、後は通常のRails開発と変わりません。PHP+Apacheであれば、CentOSでも問題ないでしょう(Ubuntuでももちろん大丈夫です)。この機会にぜひEC2を試してみてください。



