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: [gist id=”2625327″] 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: [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: Nginx.conf [gist id= “2562680”] Unicorn.rb [gist id = “2564049”]

Final Steps

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:
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!

Frequently Asked Questions (FAQs) about Deploying Rails App to the Cloud with Unicorn, Nginx, and Capistrano

What are the prerequisites for deploying a Rails app with Unicorn, Nginx, and Capistrano?

Before you start deploying your Rails app, you need to have Ruby on Rails installed on your system. You also need to have a basic understanding of how to use the command line interface. Additionally, you should have a Rails app ready for deployment. If you’re deploying to a server, you’ll need to have SSH access to it. Lastly, you’ll need to install Unicorn, Nginx, and Capistrano on your system.

Why do I need to use both Unicorn and Nginx for deploying my Rails app?

Unicorn and Nginx work together to efficiently handle web requests. Unicorn is a Rack HTTP server that processes incoming requests and sends them to your Rails app. Nginx, on the other hand, is a web server that can handle static files and act as a reverse proxy for Unicorn. This setup allows you to take advantage of the strengths of both Unicorn and Nginx, resulting in a more robust and scalable deployment.

How do I configure Unicorn for my Rails app?

Configuring Unicorn involves creating a configuration file where you specify settings like the number of worker processes, the location of the PID file, and the paths to the standard output and error logs. You can also specify settings for the Unicorn master process, like the timeout value and whether it should preload the app before forking worker processes.

How do I set up Nginx as a reverse proxy for Unicorn?

Setting up Nginx as a reverse proxy for Unicorn involves modifying the Nginx configuration file. You need to specify the location of the Unicorn socket and set the proxy settings. This setup allows Nginx to forward requests to Unicorn and serve the responses back to the client.

How do I use Capistrano for deploying my Rails app?

Capistrano is a remote server automation tool that you can use to automate tasks related to deploying your Rails app. You can use it to automate tasks like updating your app’s code, restarting the Unicorn server, and running database migrations. To use Capistrano, you need to create a Capfile and a deploy.rb file where you specify your deployment settings.

How do I troubleshoot issues with my Rails app deployment?

Troubleshooting issues with your Rails app deployment involves checking the logs for any error messages. You can check the Unicorn log, the Nginx log, and the Rails log. You can also use tools like New Relic or Scout to monitor your app’s performance and identify any potential issues.

How do I ensure that my Rails app is secure when deploying with Unicorn, Nginx, and Capistrano?

Ensuring the security of your Rails app involves several steps. You should keep your system and all your software up to date, use strong passwords, and limit access to your server. You should also configure Nginx to use HTTPS and keep your SSL certificates up to date. Additionally, you should regularly check your logs for any suspicious activity.

How do I scale my Rails app when deploying with Unicorn, Nginx, and Capistrano?

Scaling your Rails app involves increasing the number of Unicorn worker processes, adding more servers, or both. You can also use a load balancer to distribute traffic among your servers. Additionally, you should optimize your Rails app and your database to handle more traffic.

Can I use other servers instead of Unicorn and Nginx for deploying my Rails app?

Yes, you can use other servers for deploying your Rails app. However, Unicorn and Nginx are popular choices because they work well together and are known for their performance and reliability. Other options include Puma, Passenger, and Apache.

How do I keep my Rails app up to date when deploying with Unicorn, Nginx, and Capistrano?

Keeping your Rails app up to date involves regularly updating your app’s code and running database migrations. You can use Capistrano to automate these tasks. You should also keep your system and all your software up to date to ensure that you’re using the latest security patches and bug fixes.