Deploying Rails Applications to Production

Deploying a Rails application to production requires careful planning and execution to ensure reliability, performance, and security. This guide covers essential steps and best practices for successful production deployments. For more on environment configuration, check out our guide on configure environments rails application.

Pre-Deployment Checklist

1. Environment Configuration

Configure your production environment properly:

ruby
1# config/environments/production.rb
2Rails.application.configure do
3 config.cache_classes = true
4 config.eager_load = true
5 config.consider_all_requests_local = false
6 config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
7 config.force_ssl = true
8end
9

For more on server configuration, see our guide on configure puma unicorn optimal performance.

2. Database Configuration

Set up your database configuration securely:

yaml
1# config/database.yml
2production:
3 adapter: postgresql
4 encoding: unicode
5 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
6 database: <%= ENV['DATABASE_NAME'] %>
7 username: <%= ENV['DATABASE_USER'] %>
8 password: <%= ENV['DATABASE_PASSWORD'] %>
9 host: <%= ENV['DATABASE_HOST'] %>
10

3. Dependencies Management

Ensure all dependencies are properly managed:

ruby
1# Gemfile
2group :production do
3 gem 'puma'
4 gem 'pg'
5 gem 'redis'
6end
7

For more on dependency management, see our guide on manage ruby project dependencies using bundler.

Deployment Options

1. Platform as a Service (PaaS)

Heroku

Heroku offers a straightforward deployment process:

bash
1# Install Heroku CLI
2brew install heroku/brew/heroku
3
4# Login and create app
5heroku login
6heroku create my-app
7
8# Deploy
9git push heroku main
10
11# Run migrations
12heroku run rails db:migrate
13

2. Virtual Private Server (VPS)

For more control, deploy to a VPS using Capistrano:

ruby
1# Capfile
2require 'capistrano/setup'
3require 'capistrano/deploy'
4require 'capistrano/rails'
5require 'capistrano/bundler'
6require 'capistrano/rvm'
7require 'capistrano/puma'
8
9# config/deploy.rb
10set :application, 'my_app'
11set :repo_url, 'git@github.com:username/my_app.git'
12set :deploy_to, '/var/www/my_app'
13set :linked_files, %w{config/database.yml config/master.key}
14set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle}
15

For more on scaling, see our guide on horizontal scaling techniques rails application.

Server Configuration

1. Web Server (Nginx)

Configure Nginx as a reverse proxy:

nginx
1# /etc/nginx/sites-available/my_app
2server {
3 listen 80;
4 server_name example.com;
5
6 location / {
7 proxy_pass http://localhost:3000;
8 proxy_set_header Host $host;
9 proxy_set_header X-Real-IP $remote_addr;
10 }
11}
12

For more on reverse proxies, see our guide on reverse proxy nginx apache rails role.

2. Application Server

Configure Puma for optimal performance:

ruby
1# config/puma.rb
2workers ENV.fetch("WEB_CONCURRENCY") { 2 }
3threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
4threads threads_count, threads_count
5
6preload_app!
7
8port ENV.fetch("PORT") { 3000 }
9environment ENV.fetch("RAILS_ENV") { "production" }
10

For more on load balancing, see our guide on load balancer role high traffic rails application setup.

Performance Optimization

1. Asset Compilation

Precompile assets for production:

bash
1RAILS_ENV=production bundle exec rails assets:precompile
2

2. Caching

Implement caching strategies:

ruby
1# Enable caching
2config.action_controller.perform_caching = true
3config.cache_store = :redis_cache_store, { url: ENV['REDIS_URL'] }
4
5# Fragment caching
6<% cache product do %>
7 <%= render product %>
8<% end %>
9

For more on performance optimization, see our guide on optimize rails app for high traffic.

Monitoring and Maintenance

1. Error Tracking

Set up error tracking with services like Rollbar or Sentry:

ruby
1# config/initializers/rollbar.rb
2Rollbar.configure do |config|
3 config.access_token = ENV['ROLLBAR_ACCESS_TOKEN']
4 config.environment = Rails.env
5end
6

2. Performance Monitoring

Implement application monitoring:

ruby
1# Gemfile
2gem 'newrelic_rpm'
3gem 'skylight'
4

For more on scaling strategies, see our guide on scaling rails application horizontally guide.

Related Resources

Deployment and Configuration

Server Setup

Performance and Scaling

Conclusion

Deploying a Rails application to production requires attention to many aspects including server configuration, performance optimization, and monitoring. By following these best practices and regularly monitoring your application's performance, you can ensure a reliable and efficient production environment.

Suggested Articles