Deploying Your Rails App to the Cloud with Unicorn, Nginx, and Capistrano

We all know how easy it is to create a Rails app, but what about when your app is ready for production?

The first thing you need to do is to set up your server and install the proper libraries, so fire up a terminal window and SSH into your server, after you have done this you will need to execute a few commands.

Configuring the server

Once you’re done configuring your server you need to install Ruby, for this you can either use RVM or rbenv, but I’m going to leave this choice to you.

Uploading to github

Next we’re going to upload our app to github. Head on over to the github and create a new repository.  Once you’ve done this, fire up a terminal on your local machine and “cd” into the root of your rails app.  Now, I’m going to assume you already have some experience with git commands.  If not, never fear, we’ve got this covered.  Once you’re in the root of your rails app run the following commands:

What we’ve done here is initialize an empty git repository inside the root of your rails app and add all your files to the git repo (If you need to ignore files you can edit the .gitignore file.) Next, we provided a message to the commit and added the remote origin, which will track our local development. Finally, we pushed the changes in our master branch to github.

Installing Capistrano

We’re almost there! It”s time to setup Capistrano for deployment, and the first step is too add it to the Gemfile:

gem 'capistrano'

Save the Gemfile and run the

bundle

command to install the gem. Next we’re going to capify our project, on a terminal window run:

capify .

This will create 2 files: one is the Cap file which will be placed in your root folder. By the way, if you’re using the rails asset pipeline, you will need to uncomment the line -load ‘deploy/assets’-. The second file is the deploy.rb file which will be placed inside the config folder of your rails app, In deploy.rb you will configure all the commands capistrano will be running on our remote server. You can copy and paste the example provided here:

As you can see, this deploy file uses unicorn as the server (which I recommend because of its stability, but you can use the server of your choice.) After you are finished setting up your deploy file with your custom configuration, create two files under your config folder: one named nginx.conf, and another one named unicorn.rb. Feel free to use the examples provided here:

Nginx.conf

Unicorn.rb

Final Steps

Create one final file under your config folder called unicorn_ini.sh edit the file and copy and paste the following code:

We have all the files we need, so let’s setup the server to deploy our app.  Push the latest changes to github and after that run the following command:

cap deploy:setup

This will create 2 folders under the /<user>/apps/<app_name> on your server. as well as some links for the unicorn and nginx configration files to place them in the proper location. After the setup is complete “cd” into the /<user>/apps/<app_name>/shared/config and edit the database.yml file to work with your database.

Once you’ve done this, give the server access to the git repository by running the following command on our development machine:

ssh-add

Now we’re all setup and ready to deploy. Let’s try this out by running:

cap deploy:cold

And watch it go.  A “cold” deploy will run the migrations and do a server start and restart.  In my experience, it will rarely work the first time you will usually get some errors here and there.  The errors will usually point you in the right direction.

If everything went OK, congratulations!  You just deployed your app using unicorn, nginx and capistrano. Hope you liked it!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • John

    What does this have to do with “cloud” hosting? This looks like the deploy strategy for a plain old VPS.

  • http://andredublin.com Andre Dublin

    Great tut, just in time since I’m moving 60+ sites to rackspace and dumping cakephp for rails.

  • Peter Duerden

    “In my experience, it will rarely work the first time you will usually get some errors here and there. The errors will usually point you in the right direction.”

    Are you serious? And this methodology is considered viable for production environments? I’m a .Net developer who has dabbled and enjoyed learning Rails, and would love to use it for projects but it’s this sort of thing that freaks enterprise clients into sticking with more familiar stacks, e.g., ASP.NET/IIS.

    • http://datachomp.com Rob Sullivan

      Peter, I think he is talking about “cap deploy:cold” rarely working the first time. His statement is completely anecdotal and very cautious wording… something you, as an ‘enterprise developer’ of all people should appreciate. Think of “cap deploy:cold” as the very first time to try to run an msbuild to a raw iis… it doesn’t always work the first time due to whatever reasons. However, once you get the configs and stuff in place, it works like a charm.

  • illnino

    Hi, I could deploy my site to the server. However, when I upload files (avatars tiny images) to the server, it always shows 500.

    I know there is limitation for uploads in Nginx by default. But my files wont exceed 1m.

    Any clue? Thanks.