Setting up Rails 3 stack on an Amazon EC2 instance

Recently, we had an encounter with Rails 3 on EC2 for one of our esteemed clients. The client wanted to host his application on Amazon EC2(and rightly so given that its such an amazing infrastructure at such an affordable cost). After searching the public AMIs, we could not come out with an AMI that had all our requirements satisfied(Rails 3, Postgres, phusion passenger). This lead us to trigger our sys admins to prepare the bare minimum EC2 AMI with the required development stack. We documented the steps if they can be helpful to someone. 1. Choose a bare minimum AMI, we chose centos 5.5 based 64 bit AMI. 2. download the keypair and log into your server with ssh. 3. You need to be root to perform the following steps, so if you log in with ec2-user type in the following command to become the root user:
sudo su -
Install Ruby and Rails 3:
  1. The amazon machine instance(AMI) comes with ruby 1.8.7 installed as of today.
  2. Rails 3 needs ruby 1.9+ to work.
  3. Ruby 1.9.1 is a buggy version so we will go ahead and install ruby 1.9.2
  4. We could not find rpms of ruby 1.9.2 on centos repos or even on rpmforge, so we had to go ahead and install ruby from source.
  5. Download ruby sources from http://www.ruby-lang.org/en/downloads
  6. wget ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-1.9.2-p136.tar.gz
  7. unpack it !!
    tar xzvf ruby-1.9.2-p136.tar.gz
  8. Install development tools.
    • yum groupinstall ‘Development Tools’
    • yum install readline-devel
  9. Move to ruby sources and compile
  10. cd ruby-1.9.2-p136
    ./configure
    make && make install
    
  11. Sit back and relax. Ruby will take some time to compile. It took about 7-9 minutes for us on EC2 micro instance
  12. After you are done with ruby install. Install rails.
    gem install rails
Install Apache and Passenger
  • Install Apache 2.
    yum install httpd
  • yum install httpd-devel
  • Install passenger.
    passenger-install-apache2-module
  • In case you get the following error,
    To install OpenSSL support for Ruby:
  • Please (re)install Ruby with OpenSSL support by downloading it fromhttp://www.ruby-lang.org/. Go to ruby source directory and install openssl extension.
Configure Passenger
Configure passenger. Depending upon your passenger version you would need to add following lines to your httpd.conf file. (Please note that these directives would come for you after the passenger install, you just need to copy and paste from there). For me the passenger version was 3.0.2:
LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2
PassengerRuby /usr/local/bin/ruby
LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2
PassengerRuby /usr/local/bin/ruby
Create virtual host configuration:
<VirtualHost *:80>
ServerName www.yourhost.com
DocumentRoot /somewhere/public    # <-- be sure to point to 'public'!
<Directory /somewhere/public>
AllowOverride all              # <-- relax Apache security settings
Options -MultiViews            # <-- MultiViews must be turned off
</Directory>
</VirtualHost>
Install postgres
yum install postgres
Thats it !! We are all done with our awesome rails 3 stack on Amazon EC2 !!
Please feel free to post any queries in the comments.
  • Geo Nicolaides

    Thanks for all the help.
    Just a comment that you might want to add to your process.

    Before the passenger-install-apache2-module you need to “gem install passenger” and then
    passenger-install-apache2-module.

  • Geo Nicolaides

    Thanks for all the help.
    Just a comment that you might want to add to your process.

    Before the passenger-install-apache2-module you need to “gem install passenger” and then
    passenger-install-apache2-module.

  • Yaron

    Thanks for this very needed guide..
    I’m following your steps precisely and am having a problem on step 11.
    I’ve installed ruby 1.9.2 successfully (If I type ruby -v I see I am on 1.9.2).
    When I try to install rails (by typing gem install rails) I get the following error:

    ERROR: Loading command: install (LoadError)
    no such file to load — zlib
    ERROR: While executing gem … (NameError)
    uninitialized constant Gem::Commands::InstallCommand

    Any ideas on what to do?
    Thanks..

    • Yaron

      I solved this problem in case anyone else runs into it:

      I ran:
      # yum install zlib zlib-devel

      Then:

      # cd /usr/local/src/ruby-1.9.2-p136/ext/zlib/
      # ruby extconf.rb –with-zlib-include=/usr/include –with-zlib-lib=/usr/lib
      # make
      # make install

      Not sure why zlib was not configured during the ruby installation, but in any case this fixed it..

      • http://twitter.com/enbake Enbake Consulting

        Hey,

        yes this the best to fix. Although if you are installing from scratch, you can do yum groupinstall ‘Development Tools’ to install all the required libs for any other application that you may install later.

        HTH.

        Thanks.

  • Yaron

    Thanks for this very needed guide..
    I’m following your steps precisely and am having a problem on step 11.
    I’ve installed ruby 1.9.2 successfully (If I type ruby -v I see I am on 1.9.2).
    When I try to install rails (by typing gem install rails) I get the following error:

    ERROR: Loading command: install (LoadError)
    no such file to load — zlib
    ERROR: While executing gem … (NameError)
    uninitialized constant Gem::Commands::InstallCommand

    Any ideas on what to do?
    Thanks..

    • Yaron

      I solved this problem in case anyone else runs into it:

      I ran:
      # yum install zlib zlib-devel

      Then:

      # cd /usr/local/src/ruby-1.9.2-p136/ext/zlib/
      # ruby extconf.rb –with-zlib-include=/usr/include –with-zlib-lib=/usr/lib
      # make
      # make install

      Not sure why zlib was not configured during the ruby installation, but in any case this fixed it..

      • Anonymous

        Hey,

        yes this the best to fix. Although if you are installing from scratch, you can do yum groupinstall ‘Development Tools’ to install all the required libs for any other application that you may install later.

        HTH.

        Thanks.

      • RDasxy

        This solved my issue. Thank you so much!

  • Geo Nicolaides

    another thing that I would like to add is that the last step needs to be fixed.
    instead of yum install postgres is yum install postresql

    • http://twitter.com/enbake Enbake Consulting

      Thanks for the inputs Nicolaides, I will update the post to reflect the changes ASAP !!

  • Geo Nicolaides

    another thing that I would like to add is that the last step needs to be fixed.
    instead of yum install postgres is yum install postresql

    • Anonymous

      Thanks for the inputs Nicolaides, I will update the post to reflect the changes ASAP !!

  • Sam

    Upon passenger-install-apache2-module I had…
    “GNU C++ compiler… not found”
    … and had to …
    yum install gcc-c++
    .. and also had…
    “Curl development headers with SSL support… not found”
    … and had to …
    yum install curl-devel

    • http://twitter.com/enbake Enbake Consulting

      hmmm… Strange to know that gcc was not installed on the amazon instance. Not sure about the AMI that you used.

  • Sam

    Upon passenger-install-apache2-module I had…
    “GNU C++ compiler… not found”
    … and had to …
    yum install gcc-c++
    .. and also had…
    “Curl development headers with SSL support… not found”
    … and had to …
    yum install curl-devel

    • Anonymous

      hmmm… Strange to know that gcc was not installed on the amazon instance. Not sure about the AMI that you used.

  • http://profiles.google.com/francispotter Francis Potter

    One mistake. You say “Rails 3 needs ruby 1.9+ to work.” But see:

    http://edgeguides.rubyonrails.org/3_0_release_notes.html#rails-3-requires-at-least-ruby-1-8-7

    • http://twitter.com/enbake Enbake Consulting

      Good catch Francis but its always better to use 1.9+ to have optimal rails 3 !!

  • http://profiles.google.com/francispotter Francis Potter

    One mistake. You say “Rails 3 needs ruby 1.9+ to work.” But see:

    http://edgeguides.rubyonrails.org/3_0_release_notes.html#rails-3-requires-at-least-ruby-1-8-7

    • Anonymous

      Good catch Francis but its always better to use 1.9+ to have optimal rails 3 !!

  • Beeth9

    Hi, I followed the steps up to passenger install. It prompted to install openssl for ruby and when I tried to install from the ruby ext/openssl dir, got this error:

     ruby extconf.rb
    === OpenSSL for Ruby configurator ===
    === Checking for system dependent stuff… ===
    checking for t_open() in -lnsl… no
    checking for socket() in -lsocket… no
    checking for assert.h… yes
    === Checking for required stuff… ===
    checking for openssl/ssl.h… no
    === Checking for required stuff failed. ===
    Makefile wasn’t created. Fix the errors above.

    Some forums suggest to install openssl-dev but I couldn’t find it using yum. Any ideas?

  • Beeth9

    Hi, I followed the steps up to passenger install. It prompted to install openssl for ruby and when I tried to install from the ruby ext/openssl dir, got this error:

     ruby extconf.rb
    === OpenSSL for Ruby configurator ===
    === Checking for system dependent stuff… ===
    checking for t_open() in -lnsl… no
    checking for socket() in -lsocket… no
    checking for assert.h… yes
    === Checking for required stuff… ===
    checking for openssl/ssl.h… no
    === Checking for required stuff failed. ===
    Makefile wasn’t created. Fix the errors above.

    Some forums suggest to install openssl-dev but I couldn’t find it using yum. Any ideas?

  • Beeth9

    sudo yum groupinstall development-tools

    Then install the OpenSSL libs:

    sudo yum install openssl-develDidn’t know why it didn’t work just now… must be zzzz….. going to sleep.

    • http://twitter.com/enbake Enbake Consulting

      Good to know that you figured it out. Have fun with rails on EC2 .. :)

  • Beeth9

    sudo yum groupinstall development-tools

    Then install the OpenSSL libs:

    sudo yum install openssl-develDidn’t know why it didn’t work just now… must be zzzz….. going to sleep.

    • Anonymous

      Good to know that you figured it out. Have fun with rails on EC2 .. :)

  • Adamgf

    You also have to download postgres-server and postgres-devel.  Calling the function yum list will list all of the available packages.

  • dookerj

    Why not install RVM and let that handle the installation of ruby(s)? Seems RVM is pretty standard these days; allows for managing gemsets, too.

    Once it’s installed you can simply run

    rvm install 1.9.3 to get latest MRI Ruby 1.9.3