Saturday, January 1, 2011

Setting Up Rails 3.0 On EC2

This tutorial assumes you followed my steps in Creating a (free) Linux EC2 Instance. If you're doing this on a 32-bit instance step 8 will be slightly different.

1. SSH into your instance
$ ssh ec2-user@50.16.195.2 -i ~/rtremainekey.pem
2. Install GCC - enter "y" when prompted
$ sudo yum install gcc
3. Install MySQL - enter "y" when prompted
$ sudo yum install mysql-server mysql-libs mysql-devel
4. Install Ruby - enter "y" when prompted
sudo yum install ruby ruby-devel ruby-irb ruby-rdoc ruby-ri
5. Install RubyGems
$ cd /tmp
$ wget -q http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz
$ tar xzf rubygems-1.3.7.tgz
$ cd rubygems-1.3.7
$ sudo ruby setup.rb
6. Install Rails - this one will take a while
$ sudo gem install rails
7. Start MySQL
$ sudo /etc/init.d/mysqld start
8. Give the root MySQL user a password
$ mysqladmin -u root password yourpassword
9. Create symlinks to MySQL libraries
$ cd /usr/lib64/
$ sudo ln -s mysql/libmysqlclient.so
$ sudo ln -s mysql/libmysqlclient.so.16
$ sudo ln -s mysql/libmysqlclient_r.so
$ sudo ln -s mysql/libmysqlclient_r.so.16
10. Install MySQL Gem
Expect to see some "No definition" errors.
$ sudo gem install mysql
11. Open Port 3000
To keep things simple I'm opening port 3000 which is the default WEBrick binding. Login to the AWS Management Console and select the security group for your instance. Add a new entry with the following values.
  • Connection Method: HTTP
  • Protocol: TCP
  • From Port: 3000
  • To Port: 3000


The remaining steps are taken from the Getting Started with Rails tutorial. I'm only using the first few steps here to verify Rails and MySQL are configured and running correctly.


12. Create A Test Project
$ cd ~
$ rails new blog --database=mysql
$ cd blog
$ sudo bundle install
13. Set your MySQL password
Edit config/database.yml
$ nano config/database.yml
and apply the root password from step 8. You'll need to add it in 3 places, your database.yml should look like this when finished:
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: blog_development
  pool: 5
  username: root
  password: yourpassword
  socket: /var/lib/mysql/mysql.sock

test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: blog_test
  pool: 5
  username: root
  password: yourpassword
  socket: /var/lib/mysql/mysql.sock

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: blog_production
  pool: 5
  username: root
  password: yourpassword
  socket: /var/lib/mysql/mysql.sock
14. Create The Database
$ rake db:create
15. Start WEBrick
$ rails server
Note: WEBrick should really only be used for development & testing. For a production level web server you'll want something like Mongrel.

16. Welcome Aboard!
Browse to http://50.16.195.2:3000/ (using your Elastic Ip). If everything worked you will be greeted with the welcome aboard page.
You should be all set. If you're new to Rails I recommend continuing the Getting Started with Rails tutorial, to keep going with it start at 4.2 Say “Hello”, Rails.

3 comments:

  1. This is great. Did you choose not to use rvm for a specific reason? Obviously not strictly necessary. Could always add it later. Also I wonder why you had to make the mysql symlinks, I've never heard of that.

    ReplyDelete
  2. http://news.ycombinator.com/item?id=2059964

    ReplyDelete
  3. Thanks. No reason against rvm, good idea for a follow up post. I'd also like to get Mongrel running on there.

    Without the symlinks the mysql gem was failing with complaints about not being able to find the libs. There is probably a better a way to solve it.

    ReplyDelete