
I recently set up a new Rails production server based on Ubuntu 10.04, Nginx, Unicorn and rbenv. A few of my Rails applications is still running on Ruby 1.8.7 so the web server must handle multiple ruby versions.
The idea is that each and every Rails application should be self contained.
Meaning that running unicorn uses the Ruby in .rbenv-version and the Unicorn in Gemfile.lock.
By telling Bundler to generate binstubs we know that the gems installed within the application will be used.
Bundler should be the only gem installed to the global gemset.
This how-to assumes a vanilla Ubuntu with Nginx installed. Please have a look at my how-tos for setting up Ubuntu and install Nginx. Capistrano is used to deploy the Rails application. I will only show you the changes necessary to the deploy file. If you are unfamiliar with Capistrano you can read more about it on the Capistrano website.
To keep it really simple I give the deployer user sudo rights.
That is not necessary and probably not recommendable.
But in this how-to I want to focus on the key parts to get the server up and running.
Deploy User
I’m using a single account for all my application deployments. Some people likes to have a separate user account for each application.
Create an deployer user account and add it to the staff group.
1
| |
Give this user a password:
1
| |
Now open the sudoers file and make the staff user group able to perform a sudo by adding the following line to it:
1
| |
Upload your public ssh-key for password-less login to the deployer account. You can find more detailed instructions in the Ubuntu how-to:
1
| |
With the user account created, you should login to the server as deployer.
Install Ruby
Install the dependencies:
1 2 | |
Install rbenv:
1 2 3 4 | |
Install ruby-build:
1 2 3 4 | |
Install Rubies:
1 2 3 | |
Configure RubyGems
Add to .gemrc
1 2 3 | |
Rails Application Setup
I start with an existing Rails application called myapp.
I will only show you the modifications neccessary to make the application deployable on Nginx/Unicorn with rbenv.
Don’t forget to check in the new files created below.
The application is accessible at http://myapp.com.
Create a .rbenv-version file with a Ruby version of choice:
1
| |
Add Unicorn to the Gemfile and run bundle install:
1 2 3 | |
Create a unicorn.rb configuration file:
1 2 3 4 5 6 7 8 9 10 11 12 | |
The deploy.rb file must be adjusted to load rbenv into the shell that Capistrano uses.
We also want Bundler to generate binstubs and honor the .rbenv-version file and use the project-specified Ruby version.
This allows us to switch versions of Ruby by pushing a new .rbenv-version file.
Add these lines to Capistrano deploy.rb:
1 2 | |
Deploy the application:
1
| |
Server Setup
On the server we need to create a Nginx Virtual Host file for myapp.
Requests that cannot be handled by Nginx web server are passed on to the Unicorn application server. Beacuse they are on the same machine the best communication method is through a Unix domain socket.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Enable the site by creating a symbolic link in sites-enabled and restart nginx:
1 2 3 | |
We also need an init file to startup the myapp Unicorn instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
Make the init script executable and then start the application:
1 2 3 4 | |
Provided that your application has been successfully deployed and available at /home/deployer/myapp_com/current/ the Unicorn application should start.
If the application fails to start please make sure the PID path exists.
1
| |
Done!