Skip to content

AWS Recipe

by admin on August 24th, 2011

Seems Im not the only one who has had problems with ruby 1.9.2 and rvm on my Ubuntu servers. This recipe so far has worked great for every project running on it so far. Im not taking credit for all of it. Its a compilation of things that I have found on other blogs (too many to credit).

Use this AMI
ami-a403f7cd

Then add ssh, http and https to the default security group. Now you can shell in

  1. sudo apt-get update
  2. sudo apt-get build-essential
  3. sudo apt-get install curl git-core gitk git-gui
  4. sudo apt-get install libcurl4-openssl-dev
  5. sudo apt-get install vim
  6. sudo apt-get install rake
  7. sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev
  8. sudo apt-get install mysql-server mysql-client
  9. sudo apt-get install libmysql-ruby libmysqlclient-dev

Now for the RVM stuff

  1. sudo apt-get install ruby
  2. bash < <( curl https://rvm.beginrescueend.com/releases/rvm-install-head )

EDIT:: RVM has changed the install address. Check their website to get the latest bash install command

Open .bashrc and replace

  1. [ -z "$PS1" ] && return

with

  1. if [[ -n "$PS1" ]]; then

Then at the end of the file

  1. if [[ -s $HOME/.rvm/scripts/rvm ]] ; then source $HOME/.rvm/scripts/rvm ; fi
  2. fi

You should close the terminal and open a new one then enter

  1. rvm notes

You should see a bunch of options.

Now we need to add a bunch of stuff

  1. sudo aptitude install build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf

Now we can run rvm list known and we should see a list of rubies

Time to upgrade our ruby

  1. rvm install 1.9.2-head
  2. rvm –default 1.9.2-head

EDIT:: RVM has changed a bit since I wrote this. Be sure to read their instructions to be familiar with the differences. Setting default has now changed to

  1. rvm use 1.9.2-head –default

Don’t forget the –default or your git hooks will never work

Lets get rails in there to test it out

  1. gem install rails

Try it out

  1. rails new testing

Should have created a new rails app.

Now we need to get our project from the git repo. But we need to give them a pub key

  1. cd ~/
  2. ssh-keygen -t rsa

(defaults, no passphrase)

  1. cat .ssh/id_rsa.pub

copy and paste that into the repo keys on your repo

Now set up the site

  1. cd /var
  2. sudo chgrp -R ubuntu www
  3. sudo chown -R ubuntu www
  4. cd www
  5. git clone [clone address] [dir name you want]
  6. cd [new dir name]
  7. bundle install

cool, now lets set up the webserver

  1. gem install passenger
  2. passenger-install-apache2-module

follow the instructions

Now we need to set up our git hooks for a heroku like deployment
shell into the server then cd to /var/www/[your app]/.git/hooks
copy the post-receive.sample to just post-receive
chmod it to 777
then edit it like so

  1. #!/bin/bash
  2. APP_PATH=/var/www/myapp
  3. # Production environment
  4. export RAILS_ENV="production"
  5. # This loads RVM into a shell session. Uncomment if you’re using RVM system wide.
  6. # [[ -s "/usr/local/lib/rvm" ]] && . "/usr/local/lib/rvm"
  7.  [[ -s "/home/ubuntu/.rvm/scripts/rvm" ]] && source "/home/ubuntu/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
  8.  
  9. echo "*******************************************"
  10. echo "    Deploying [APP NAME] to live server      "
  11. echo "*******************************************"
  12.  
  13. exit_with_error() {
  14.   echo "[DEPLOY] !!!!!!!!!!!!!!!!!!!! An error has occurred !!!!!!!!!!!!!!!!!!!!!!!"
  15.   exit 1
  16. }
  17.  
  18. cd ${APP_PATH}
  19. env -i git add .
  20. env -i git reset –hard || exit_with_error
  21. env -i git pull origin master
  22.  
  23. echo "[DEPLOY] –  * Running bundle"
  24. bundle install –deployment –without development test || exit_with_error
  25.  
  26. echo "[DEPLOY] –  * Migrating database"
  27. bundle exec rake db:migrate || exit_with_error
  28.  
  29.  
  30. echo "[DEPLOY] –  * Successfully deployed application to live server"
  31.  
  32. echo "[DEPLOY] – * Restarting application"
  33. mkdir -p tmp/
  34. touch tmp/restart.txt
  35.  
  36.  
  37. echo "*******************************************"
  38. echo "    Successfully deployed [APP NAME]       "
  39. echo "*******************************************"
  40.  

Then, in the .git/config file add this to stop all those pesky warnings

  1. [receive] denyCurrentBranch = false

Now we need to create a DB

  1. mysql -u root -p

create database [whatever you need here];

  1. exit

Lets add the new server as a remote for your git

  1. git remote add live ubuntu@[some ip]:/var/www/[app name]

I think were ready for a push. In your project update the config/database.yml and change the production creds to what you just created.
commit that change and push it to origin

  1. git push origin master

And now push it to live

  1. git push live master

tada.. git push deployment with gem updates and migrations

Just one thing left… database backups to S3. Thats for another post.

From → Web Development

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS