Skip to content

Instantly share code, notes, and snippets.

@Epigene
Last active October 27, 2015 15:02
Show Gist options
  • Save Epigene/b68edfcbbd592af19db9 to your computer and use it in GitHub Desktop.
Save Epigene/b68edfcbbd592af19db9 to your computer and use it in GitHub Desktop.
Rails App deployment setup

Make Rails App deployable to ubuntu droplet with mina

Mina

Add gems

group :development do
  gem 'mina' # New deploy candidate
  gem 'mina-nginx', :require => false
end

Configure deploy.rb

$ mina init
# in /config/deploy.rb
# >> copy sample deploy.rb contents here and adapt

Puma

Add gem

gem 'puma'

Configure puma.rb

# in /config/puma.rb
app = "SwissLanguages" # App-specific
root = "/home/deployer/apps/#{app}"

workers  2
threads  1, 6

rackup      DefaultRackup
rails_environment = ENV['RAILS_ENV'] || "production"
environment rails_environment
daemonize   true

pidfile "#{root}/shared/tmp/pids/puma.pid"
stdout_redirect "#{root}/shared/log/#{rails_environment}.log", "#{root}/shared/log/#{rails_environment}_errors.log"
bind "unix:/tmp/puma.socket"

Nginx

# in /config/deploy/production_nginx.conf

upstream puma {
  server unix:/tmp/puma.socket fail_timeout=1;
}

server {
  listen 80 default deferred;
  server_name ~.*;
  return 307  https://app.swisslanguages.com$request_uri;
}

server {
  listen                37.139.20.149:443 ssl;
  server_name           app.swisslanguages.com;
  ssl_certificate       /etc/ssl/server.crt;
  ssl_certificate_key   /etc/ssl/server.key;
  ssl_protocols         TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers           RC4:HIGH:!aNULL:!MD5;
  ssl_session_cache     shared:SSL:15m;
  ssl_session_timeout   15m;

  root /home/deployer/apps/SwissLanguages/current/public;

  if ($request_method !~ ^(GET|HEAD|POST)$ ) {
       return 444;
  }
  if ($http_user_agent ~* LWP::Simple|BBBike) {
      return 403;
  }
  if ($http_user_agent ~* (spider|AcoiRobot|msnbot|scrapbot|catall) ) {
      return 403;
  }
  location ^~ /assets/ {
    gzip_static on;
    gzip_vary on;
    expires max;
    add_header Cache-Control public;
  }

  location ~ \.(php|aspx|asp|myadmin)$ { return 444; log_not_found off; }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header  X-Real-IP       $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto https;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_pass        http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

Config

Make sure that assets precompile

# in config/environments/production.rb

config.assets.compile = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment