Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Testing, Jobs and Deployment

Deploying a Rails App to Production

Deploy a Rails app: production configuration, credentials and secrets, asset pipeline, database migrations, Docker and Kamal, health checks, logging and a launch checklist. Includes a runnable environment-config stand-in.

On your laptop, Rails runs with bin/rails server, a SQLite file and friendly error pages. In production, thousands of strangers use the app at once, errors must never show your code, secrets must stay secret, and the app has to survive restarts and updates. Deployment is everything between "it works on my machine" and "it works for users".

This lesson is a map, not a click-through for one provider. Nothing here can run on the site's Ruby runner (no Rails, no server, no Docker), so shell commands and configuration are read-only. The one runnable example is a plain Ruby stand-in for a rule you must always follow: configuration comes from the environment, and missing secrets must stop the app at boot rather than fail later.

What changes in production

Ruby
# config/environments/production.rb (the important lines)
Rails.application.configure do
  config.enable_reloading = false         # code is loaded once
  config.eager_load = true                # load everything at boot, so errors appear at deploy time
  config.consider_all_requests_local = false   # no stack traces for visitors
  config.force_ssl = true                 # redirect to HTTPS, set HSTS, secure cookies
  config.assume_ssl = true                # when a proxy terminates TLS
  config.log_level = :info
  config.log_tags = [:request_id]
  config.active_job.queue_adapter = :solid_queue
  config.cache_store = :solid_cache_store
end

Differences from development: classes are cached, assets are precompiled and fingerprinted, caching is on, and exceptions render public/500.html while the details go to the log and your error tracker (Sentry, Honeybadger, Rollbar, or Rails' own error reporter).

Secrets and credentials

Never commit passwords or API keys in plain text. Rails keeps secrets in an encrypted file:

SH
bin/rails credentials:edit                 # opens config/credentials.yml.enc decrypted
bin/rails credentials:edit --environment production
Ruby
Rails.application.credentials.dig(:stripe, :secret_key)

The file is safe to commit; the key that decrypts it (config/master.key or the RAILS_MASTER_KEY environment variable) is not. Give it to the server through the host's secret store. Simple values such as DATABASE_URL and RAILS_LOG_LEVEL go in plain environment variables, following the "twelve-factor" idea that config lives in the environment.

Stand-in: fail fast on missing configuration

Ruby Stand-in: reading config from ENV safely

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Rails does the same: if SECRET_KEY_BASE or the master key is missing, the app refuses to start, which is much better than discovering it when the first user logs in.

The database

Use a server database (PostgreSQL or MySQL), or SQLite on one machine if traffic is small; Rails 8 makes SQLite production-ready with separate files for the main data, cache, queue and cable. On every deploy run migrations before the new code serves traffic:

SH
bin/rails db:migrate

Make migrations safe for running apps: add a column first, deploy code that uses it, and only later remove the old column. Renaming or dropping a column in one step breaks the old code that is still running during the rollout. Back up the database on a schedule and test restoring it.

Assets

SH
RAILS_ENV=production bin/rails assets:precompile

Propshaft (the Rails 8 default) or Sprockets copies CSS, JavaScript and images into public/assets with a hash in the file name (app-9f2c1b.css), so they can be cached for a year and changed by renaming. Put a CDN in front for files and set far-future cache headers.

Docker and Kamal

New Rails apps include a production-ready Dockerfile. It installs gems, precompiles assets and runs the app as a non-root user. Kamal (included in Rails 8) deploys that image to any server you can reach over SSH, with zero-downtime switching:

YAML
# config/deploy.yml
service: shop
image: your-user/shop
servers:
  web:
    - 203.0.113.10
proxy:
  ssl: true
  host: shop.example.com
registry:
  username: your-user
  password:
    - KAMAL_REGISTRY_PASSWORD
env:
  secret:
    - RAILS_MASTER_KEY
SH
kamal setup     # first time: installs Docker, boots the proxy and app
kamal deploy    # build, push, start new container, health check, switch traffic
kamal rollback  # go back to the previous version

Other options are platform services (Render, Fly.io, Heroku, Railway) where you push your code and set environment variables. The shape is always the same: build, set secrets, migrate, start web and worker processes, and check health.

Processes and health checks

A Rails system usually runs several kinds of process from the same code: web (Puma), jobs (bin/jobs, see background jobs), and sometimes a scheduler. Puma uses threads (RAILS_MAX_THREADS) and optionally workers (WEB_CONCURRENCY); make sure the database pool is at least the thread count.

A load balancer needs to know if the app is alive. Rails adds GET /up, which returns 200 when the app booted without exceptions:

Ruby
# config/routes.rb
get "up" => "rails/health#show", as: :rails_health_check

Logs and monitoring

Log to standard output (RAILS_LOG_TO_STDOUT) and let the platform collect it. Filter secrets with config.filter_parameters += [:password, :token], as covered in web security. Watch error rate, response time and queue length, and alert when they change.

Launch checklist

Before you go live

Secrets Key in env, not in git
HTTPS force_ssl on
Database Migrate and back up
Tests Green in CI

Run the checklist on every release.

1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file may be committed to git, and which must not?

    The encrypted file is useless without the key, so the key is the secret to protect.
  2. 2. Why does production use eager_load = true?

    Loading everything up front surfaces missing constants immediately and avoids first-request slowness and thread-safety issues.
  3. 3. What is the safest way to remove a database column on a running app?

    During a rolling deploy old code still runs, and it would break if the column vanished underneath it.
  4. 4. What does a /up health check tell a load balancer?

    The balancer only sends traffic to instances that answer 200 on the health endpoint.

Summary

  • Production turns on caching, eager loading, HTTPS and hides stack traces; secrets live in encrypted credentials plus environment variables.
  • Migrate on deploy, change schema in backward-compatible steps, and back up.
  • Precompile fingerprinted assets, and ship a Docker image; Kamal deploys it to your own servers.
  • Run web and job processes, expose a health check, filter logs, and monitor errors.

Next: Project: a blog application