Skip to content

Multiple server setup

bruno- edited this page Oct 18, 2014 · 4 revisions

This page describes how to use capistrano-unicorn-nginx plugin to deploy your Rails app to multiple servers.

Presumably, your stage file config/deploy/production.rb currently looks something like this:

server '111.111.111.111', user: 'deploy', roles: %w{web}, no_release: true
server '112.112.112.112', user: 'deploy', roles: %w{app}
server '113.113.113.113', user: 'deploy', roles: %w{app}
server '114.114.114.114', user: 'deploy', roles: %w{db}

The above example setup has:

  • one web server that will run nginx as a load balancer
    Nginx will automatically forward requests to unicorn processes on app servers on port 8080 (unicorn_tcp_listen_port option).
  • two app servers that will run unicorn processes
    Unicorn will automatically be set up to listen on port 8080 (unicorn_tcp_listen_port option).
  • one db server where the database runs

0. requirement: passwordless sudo for deployer user

Deployer user on the web and app servers should have passwordless sudo enabled. Setting this should be pretty straightforward (google if not sure how).

1. update Capfile

require 'capistrano/unicorn_nginx'

2. specify domain name in config/deploy/production.rb (optional)

Only if you have a domain for your app specify it:

set :nginx_server_name, 'mydomain.com'

If you don't have a domain, you do not have to do anything. nginx_server_name will use the default value - server IP.

3. SSL setup in stage file config/deploy/production.rb (optional - only if you have SSL)

# ignore this if you do not need SSL
set :nginx_use_ssl, true
set :nginx_ssl_cert_local_path, '/path/to/ssl_cert.crt'
set :nginx_ssl_cert_key_local_path, '/path/to/ssl_cert.key'

4. run setup task

In the terminal run:

$ bundle exec cap production setup

This will set up Nginx for load balancing your Unicorn app servers. For more information about load balancing using Nginx, see Using nginx as HTTP load balancer. Note that this gem currently only supports the default load balancing method (round robin).

Gotchas

  • Make sure the deploy_to path (i.e. /var/www/myapp) exists and has the right privileges on all of the release servers.
    Or just install capistrano-safe-deploy-to plugin and don't think about it.
  • Make sure app servers have unicorn_tcp_listen_port port (8080 by default) open.
Clone this wiki locally