Enough said about Amazon EC2
This tutorial is about deploying an ROR app over Amazon Cloud infrastructure. It will give a walk through on commands and concepts so that you can integrate Github, Amazon production env and your local development env for your ROR app. It assumes you already have an Amazon web services account and will finish up with having ROR server up and running on EC2.
- Get Familiar – You need to know about most of the amazon products listed on their site. Each of the product has a specific functionality and you need to know them to understand the machinery. Here is the minimum set of products that you need to know: https://www.amazon.com/ec2. Make sure you understand the dashboard before you start playing around.
- Install command line tools – Either you play around with the dashboard or use command line tools for better efficiency. There are numerous tutorials to learn them, I followed this one:
- Create your instance - You need to choose an ami
(amazon instance image) and deploy on EC2. There are lot of good
tutorials that show how to do this. At the end of this step, you should
have a running instance on amazon EC2 and should appear in the
dashboard. Here are some pointers:
- http://alestic.com – Ami reference for many Ubuntu images. I used one of them (the latest version, of course)
- http://ubuntu-smoser.blogspot.com/2010/11/using-ubunt-images-on-aws-free-tier.html – An awesome tutorial if you want to avail “free tier” offered by Amazon for its web services.
- SSH to your instance – You will get a private key
from amazon and you will have to use it to ssh to your instance. Before
you can ssh to your instance, you will need to open up security
permissions at amazon side. For that, go to dashboard
-> security groups -> default. Add another entry allowing ssh
from all the IPs (0.0.0.0/0). Your command to ssh from your terminal
will be something like this
- ssh -i <path_to_private_key> ubuntu@domain-name
- domain-name is the ‘public dns’ of your instance. You can read it on your dashboard -> Instances or by executing API command: ec2-describe-instances
- I prefer to run ssh-add <path_to_priavte_key> which tells ssh to add the key permanently in its db and you can then simply run: ssh ubuntu@domain-name without worrying about private key option. (ssh-add -t 99999999 <path_to_priavte_key> to add it for long enough time)
- Prepare the instance – Now you can access to your
crude VM which has nothing but ubuntu (or OS as per your ami). Let us
install the basic packages needed. There are lots of tutorials again. I
referred following tutorials to prepare the VM and install rails, ruby
and gem.
- Ruby, Rails and gem: http://torqueo.net/installing-ruby-192-and-rails-3-stable-on-ubuntu/
- mysql: sudo apt-get install mysql-server libmysqlclient15-dev libopenssl-ruby
- git: sudo apt-get -y install git-core
- README: Other instructions specific to your app mentioned in its READMEs (Optional)
- Clone source code – Now we will get our source code from git repository.
- Get code: git clone https://gitUser:gitPasswd@github.com/repoUser/repo.git
- Install dependencies: bundle install
- README: Start other services such as sphinx, etc. mentioned in app’s READMEs
- db initialization: rake db:create; rake db:migrate; rake db:seed
- Git settings:
- git config –global user.name “username”
- git config –global user.email “emailid@xyz.com”
-
git config –global core.autocrlf input
- Connect your development env to ec2 – I prefer to
directly push my code from local environment to production environment.
To achieve this, add production environment repository as a remote
repository in your local development box.
- git remote add ec2 ssh://<user>@<amazon-public-dns>/~<user>/<repo-path>
- Now you can run git push ec2 master to push your code from your local environment to amazon production.
- Start the server – Start your rails server on amazon VM (rails server) and test out if you can open http://public-dns-of-instance.com:3000 from your browser. Here are the next steps:
- You will need to change the port from 3000 to 80. But 80 port (in fact any port < 1024) can only be opened by root. In this case, I do not want to run “sudo rails server” and hence I prefer configuring IPtables on amazon VM so that they forward all request at port 80 to port 3000. Here is the command:
- sudo iptables -t nat -A PREROUTING -p tcp –dport 80 -j REDIRECT –to-port 3000
- Set the Domain – Last
step is to add your own domain name so that customers can open your
application by the domain name you like. This is a little tricky part
and you should understand what is elastic IP. After you understand the idea behind elastic IP, here is what you need to do.
- Generate an elastic IP from your dashboard -> Elastic IPs. Bind that IP to your instance ID which is running your server. This maps your VM to a static IP (or elastic IP) that you just generated.
- Go to your dns provider (e.g. godaddy.com) and configure your DNS settings there. Add this elastic IP as A name and allow 24 hours for your domain to get bound to the elastic IP. After that your web server is all configured.
- Snapshot - Create a snapshot of your volume in the dashboard. You can create volumes out of this snapshot if anything goes wrong with your current one.
http://www.golygon.com/2010/11/tutorial-ruby-on-rails-hosted-on-amazon-cloud/