Why do this manually?
I am aware that there are PaaS out there (Heroku, EngineYard, DotCloud) that allows developers to deploy Rails app easily without requiring you to understand what’s going on behind the scene. But, I still believe that it’s nice to know how to manually set up your own server and deploy your code manually in the cloud (this time, we are going to use AWS EC2). This is not to say that I dislike PaaS. In fact, I really like them and I think they’re really great solutions that save a lot of developers’ time.
I currently work for a company that does not use any of the aforementioned PaaS. I often have to write my own script or deploy tasks that would start/manage the application and whatnot. Although doing all this is more tedious, I like the visibility and the explicitness that it provides. Not too much magic, just plain old manual setup.
Ruby gems, DB, and other things that I will use in this post:
- Rails 4.0.1
- MySQL RDS
New Rails App (Feel free to skip this)
I am not going to do too much for this part. All I want to do is create a new Rails application and change the root page to show a static text (Hello World). I am going to outline the steps but you should be already familiar with this process.
Open up your terminal and go to your project folder. Mine is
~/workspace. Create a new Rails project (we are going to call it ‘blogapp’) by running the following:
cd ~/workspace rails new blogapp
Now, I am going to create a new git repository for this new app and store it in BitBucket (feel free to use GitHub).
cd ~/workspace/blogapp git init git remote add origin ssh://firstname.lastname@example.org/denniss/blogapp.git git add . && git commit -m "First commit" git push origin master
Next, we are going to create a controller and a view and have the routes specify the root to point to the index page.
rails generate controller pages index
index.html.erb that is generated by the last command and add a
Hello World to it. Go to your
routes.rb and change the root to point to
#routes.rb Blogapp::Application.routes.draw do root "pages#index" end
#pages_controller.rb class PagesController < ApplicationController def index end end
Try running your server locally and see that it works. Just for sanity check, this is what I see.
Although WEBrick will work just fine for development, we are not going to use it for production environment. We want to use a web server that is more scalable and production-ready. You have several choices (Unicorn, Puma, Passenger), but, this time, we are going to use Passenger.
Add the passenger gem to your Gemfile
#Gemfile #... gem passenger #...
And run bundle install on your project directory so that it fetches the newly added gem.
Now try running your app using Passenger by running
You should see something like this in the Terminal
localhost:3000 again on your web browser and you should be able to see your hello world page.
Remember to update your git repo
git add . git commit -m "Add passenger gem for production" git push origin master