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
[gist id = “gist-2562100”]
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.
We’re almost there! It”s time to setup Capistrano for deployment, and the first step is too add it to the Gemfile:
Save the Gemfile and run the
command to install the gem. Next we’re going to capify our project, on a terminal window run:
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:
[gist id = “2562546”]
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:
[gist id= “2562680”]
[gist id = “2564049”]
Create one final file under your config folder called unicorn_ini.sh edit the file and copy and paste the following code:
[gist id = “2564093”]
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:
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:
Now we’re all setup and ready to deploy. Let’s try this out by running:
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!