Skip to content

Flexible Rails Hosting

Munen Alain M. Lafon edited this page Oct 27, 2013 · 1 revision

Table of Contents

Introduction

Flexible Rails Hosting allows you to select the ruby version on the server using capistrano.

Client Side Installation

Set up rbenv on your development machine.

App configuration for dynamic ruby version

 * Create a file {{{.ruby-version}}} in your project. Contains ruby version, e.g. {{{2.0.0-p0}}} (this is the version rbenv selects when entering the directory)
 * Add {{{gem 'capistrano-rbenv', '>= 1.0.0' }}} to the '''Gemfile'''
 * Run {{{bundle}}}
 * Add  {{{require 'capistrano-rbenv' }}} to '''config/deploy.rb'''
 * Add {{{set :rbenv_ruby_version, "2.0.0-p0"}}} to '''config/deploy.rb''' (this is the version that gets installed on the server)
 * Add {{{set :rbenv_install_dependencies, false}}} to '''config/deploy.rb''' ([[https://github.com/yyuu/capistrano-rbenv/pull/4|pull request at capistrano-rbenv]])

Further reading

 * [[https://github.com/yyuu/capistrano-rbenv|capistrano-rbenv]]

ZSH

Note: rbenv doesn't use gemsets as rvm does, instead it relies on bundler doing its job. This means prepending every command with 'bundle exec'. Fortunately zsh knows a plugin called 'bundler' out of the box that does that for you magically.

App configuration for nginx integration

 * Add '''gem 'unicorn'''' to the '''Gemfile'''
 * Run '''bundle'''
 * Add the following block to '''config/deploy.rb''':

App deployment

This task install rbenv and ruby-install, configure your shell, builds ruby remotely and creates required directories:

Install application:

Application start

You can start the application from your local machine or from the server.

From you local machine, use bundle exec cap deploy ...

 * deploy:start
 * deploy:stop
 * deploy:graceful_stop
 * deploy:reload
 * deploy:restart

On the server, you can use ...

 * start
 * foreground
 * debug
 * debug-direct
 * stop
 * graceful-stop
 * reload
 * restart
 * pid

Server configuration

Use the cdist types __panter_rails_flexible.

Interaction unicorn <=> nginx

Nginx connects to the unix socket /home/rails/app/current/unix.sock.

Files in /home/rails/app/current/public take precendence (i.e. rails is not asked for those).

Databases

Valid for MySQL and PostgreSQL:

 * /home/rails/app/shared/config/database.yml is created by cdist and contains the necessary information to connect to the database.
 * The database '''rails_production''' is created by default
 * Access is granted to user '''rails'''

Monit

 * If required/requested, monit will be installed
 * Supported jobs currently:
  * dj (delayed_job)
  * sunspot

Developer Driven Monit

The new version [tm] of the new hosting [tm] supports monit configurations defined by the developer.

As a sysadmin, add the following cdist-snippet:

As a developer, create /home/rails/app/shared/config/monit.conf with your configuration.

Monit monitors /home/rails/app/shared/config/monit.conf and restarts itself if it detects a change.

unicorn configuration

As of 2013-08-08, unicorn is configured using the file ~/.unicorn-config. unicorn_wrapper has been adjusted to support this change. Besides pre loading the app, this change also adds support for unicorn log files:

 * /home/rails/app/shared/log/unicorn.stderr.log
 * /home/rails/app/shared/log/unicorn.stdout.log

See also

 * https://newrelic.com/docs/ruby/no-data-with-unicorn 

Sendfile support

 * Use the directory /home/rails/app/shared/protected
 * See http://wiki.nginx.org/XSendfile for requirements / configuration

SSL support

 * You can use HTTP_X_FORWARDED_SCHEME in rack to find out whether the request
   came via http or https

Further Reading

 * http://www.rubyinside.com/rbenv-a-simple-new-ruby-version-management-tool-5302.html

Troubleshooting

I get version 1.9.3-p392 instead of the right ruby version

1.9.3-p392 is the standard version selected by capistrano-rbenv. It probably means you did not specify the version correctly/misspelled the variable name. Ensure it reads as following:

Assets cannot be compiled on first deploy

You have to link your database.yml before the asset compilation:

unicorn crashes on deploy, but works with unicorn_wrapper

Ensure you have the

part in your deploy.rb.