paranitips

Never stop learning! がモットーのゆるふわエンジニアブログ

EC2でRuby on Rails+Passenger+Apache環境を構築する

EC2にてRailsアプリケーションを作成し、ページを表示させるまでをまとめます。

EC2のインスタンス作成についてはこちらが参考になります。

Rubyのインストール確認

# ruby -v
ruby 1.8.7 (2012-10-12 patchlevel 371) [x86_64-linux]

rubyはデフォルトで入っています。

Apacheのインストール

yumでインストールします。

# yum install httpd httpd-devel curl-devel

自動起動の設定。

# chkconfig /etc/init.d/httpd
# chkconfig httpd on

Apacheを起動。

# service httpd start

ブラウザでPublic DNSにアクセスするとApacheのTestページが表示されます。

RubyGemsのインストール

# cd /usr/local/src/
# wget http://production.cf.rubygems.org/rubygems/rubygems-2.0.3.tgz
# tar xzvf rubygems-2.0.3.tgz
# cd rubygems-2.0.3
# ruby setup.rb
RubyGems 2.0.3 installed
ERROR:  While executing gem ... (Gem::DocumentError)
    RDoc is not installed: no such file to load -- rdoc/rdoc

rdocがないとエラーが出るのでyumでインストールします。

# yum install -y rdoc
# ruby setup.rb

gemのインストールを確認します。

# gem -v
2.0.3

Railsのインストール

gemでインストールします。

# gem install rails
ERROR:  Error installing rails:
    ERROR: Failed to build gem native extension.

    /usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.h

もろもろエラーが出るので必要なものをyumでインストールします。

# yum -y install ruby-devel
# yum -y install gcc
# yum -y install make
# gem install rails

Railsのインストールを確認します。

# rails -v
Rails 3.2.13

Passengerのインストール

# gem install passenger --no-ri --no-rdoc

Apacheのモジュールをインストールします。

# passenger-install-apache2-module
...
Some required software is not installed.
But don't worry, this installer will tell you how to install them.
Press Enter to continue, or Ctrl-C to abort.

--------------------------------------------

Installation instructions for required software

 * To install GNU C++ compiler:
   Please download it from http://gcc.gnu.org/

 * To install OpenSSL development headers:
   Please download it from http://www.openssl.org/

 * To install Zlib development headers:
   Please download it from http://www.zlib.net/

If the aforementioned instructions didn't solve your problem, then please take
a look at the Users Guide:

  /usr/lib64/ruby/gems/1.8/gems/passenger-4.0.5/doc/Users guide Apache.html

もろもろ足りないとエラーが出るので必要なものをyumでインストールします。

# yum install -y gcc-c++
# yum install -y openssl-devel
# yum install -y zlib-devel

再度実行すると以下のようなログが出てくるのでメモしておきます。

# passenger-install-apache2-module
...
LoadModule passenger_module /usr/lib64/ruby/gems/1.8/gems/passenger-4.0.5/libout/apache2/mod_passenger.so
PassengerRoot /usr/lib64/ruby/gems/1.8/gems/passenger-4.0.5
PassengerDefaultRuby /usr/bin/ruby

Railsプロジェクトの作成

今回は/usr/local/以下にvarディレクトリを作成し、そこにsampleプロジェクトを作成します。

# cd /usr/local/
# mkdir var
# cd var
# rails new sample

ここでファイル/etc/httpd/conf.d/passenger.confを作り、

先ほどメモしておいた3行とともにバーチャルホストを設定します。

LoadModule passenger_module /usr/lib64/ruby/gems/1.8/gems/passenger-4.0.5/libout/apache2/mod_passenger.so
PassengerRoot /usr/lib64/ruby/gems/1.8/gems/passenger-4.0.5
PassengerDefaultRuby /usr/bin/ruby

<VirtualHost *:80>
  ServerName sample
  DocumentRoot /usr/local/var/sample/public
  RailsEnv development
  <Directory /usr/local/var/sample/public>
    AllowOverride all
    Options -MultiViews
  </Directory>
</VirtualHost>

Apacheを再起動し、Public DNSにアクセスするとsampleプロジェクトが表示されます。

# service httpd restart

参考