Capistrano is great for deploying Rails applications, but what about all the prerequisites, like Nginx and PostgreSQL? Do you have a firewall configured on your VPS? Have you installed the latest OS security updates? Is HTTPS working right?
The capistrano-fiftyfive gem adds a cap <stage> provision
task to Capistrano that takes care of all that. Out of the box, provision
will:
- Install the latest
postgresql
,node.js
, andnginx
apt packages - Install all libraries needed to build Ruby
- Lock down your VPS using
ufw
(a simple front-end to iptables) - Set up
logrotated
for your Rails logs - Schedule an automatic daily backup of your Rails database
- Generate a self-signed SSL certificate if you need one
- Set up ngnix with the latest SSL practices and integrate it with Unicorn for your Rails app
- Create the
deployer
user and install an SSH public key - Install
rbenv
and useruby-build
to compile the version of Ruby required by your app (by inspecting your.ruby-version
file) - And more!
The gem is named "capistrano-fiftyfive" because it is built first and foremost for serving our deployment needs here at 55 Minutes. You'll notice that capistrano-fiftyfive is opinionated and strictly uses the following stack:
- Ubuntu 12.04 LTS
- PostgreSQL
- Unicorn
- Nginx
- rbenv
- dotenv
Not quite to your liking? Consider forking the project to meet your needs.
Please note that this project requires Capistrano 3.x, which is a complete rewrite of Capistrano 2.x. The two major versions are not compatible.
Add these gems to the development group of your Rails application's Gemfile:
group :development do
gem 'capistrano-bundler', :require => false
gem 'capistrano-rails', :require => false
gem 'capistrano', '~> 3.1.0', :require => false
gem 'capistrano-fiftyfive', :require => false, :github => '55minutes/capistrano-fiftyfive', :branch => :master
end
And then execute:
$ bundle
If your project doesn't yet have a Capfile
, run cap install
with the list
of desired stages (environments):
cap install STAGES=staging,production
Add these lines to the bottom of your app's Capfile
(order is important!):
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/fiftyfive'
Most of the capistrano-fiftyfive recipes are designed to run automatically as part of cap <stage> provision
, for installing and setting up various bits of the Rails infrastructure, like nginx, unicorn, and postgres. Some recipes also contribute to the cap <stage> deploy
process.
This auto-run behavior is fully under your control. In your deploy.rb
,
set :fiftyfive_recipes
to an array of the desired recipes.
If you don't want a recipe to execute as part of deploy
/provision
, simply omit it from
the list.
The following list will suffice for most out-of-the-box Rails apps. The order of the list is not important.
set :fiftyfive_recipes, %w(
aptitude
crontab
dotenv
logrotate
migrate
nginx
postgresql
rbenv
seed
ssl
ufw
unicorn
user
version
)
Even if you don't include a recipe in the auto-run list, you can still invoke the tasks of those recipes manually at your discretion.
Many of the recipes have default settings that can be overridden. Use your
deploy.rb
file to specify these overrides. Or, you can override per stage.
Here is an example override:
set :fiftyfive_unicorn_workers, 8
For the full list of settings and their default values, refer to defaults.rake.
Check out our rails-starter project for a sample Capfile and deploy.rb.
The power of the capistrano-fiftyfive recipes is that they take care of the entire setup of a bare Ubuntu 12.04 server, all the way to a fully configured and running Rails app on top up Unicorn, Nginx, rbenv, and PostgreSQL.
These steps assume you have loaded the full set of capistrano-fiftyfive recipes in your Capfile.
-
Provision an Ubuntu 12.04 VPS at your hosting provider of choice.
-
Install your public SSH key for the root user. Some providers (e.g. DigitalOcean) can do this for you automatically when you provision a new VPS.
-
Repeat steps 1-2 for all the servers in your cluster, if you are using a multi-server setup (e.g. separate web, app, and database servers).
-
Let capistrano-fiftyfive take it from here:
cap staging provision cap staging deploy
For a full description of all available tasks, run:
cap -T
All tasks from capistrano-fiftyfive will be prefixed with fiftyfive:
. You
can run these tasks just like any other capistrano task, like so:
cap staging fiftyfive:seed
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request