Deploying a Ruby on Rails Application to Production: A Complete Guide

February 7, 2025

Deploying a Ruby on Rails application to production involves more than just uploading code to a server. It requires careful preparation, configuration, and adherence to best practices to ensure performance, security, and reliability. Below is a step-by-step guide to help you navigate the process.


🚀 Need Expert Ruby on Rails Developers to Elevate Your Project?

Fill out our form! >>

🚀 Need Expert Ruby on Rails Developers to Elevate Your Project?

1. Prepare Your Application for Production

a. Environment Configuration

  • Set RAILS_ENV=production: Ensure your app runs in production mode.
  • Configure config/database.yml and config/secrets.yml: Use environment variables for sensitive data (e.g., database passwords, API keys). Avoid hardcoding secrets in your repository.

b. Asset Precompilation

  • Rails compiles assets (CSS, JavaScript, images) for production using:

c. Gems and Dependencies

  • Remove development/test-only gems (e.g., spring, byebug) from the Gemfile and run:

d. Database Migrations

  • Ensure all pending migrations are applied:

2. Choose a Production Server Stack

A typical Rails production stack includes:

  • Web Server: Nginx or Apache (to handle static assets and reverse proxy requests).
  • Application Server: Puma, Unicorn, or Passenger (to run Rails code).
  • Database: PostgreSQL, MySQL, or MariaDB.
  • Caching: Redis or Memcached (for caching and background jobs).

3. Set Up the Application Server

Example: Configuring Puma

  1. Configure config/puma.rb:
  2. Use a process manager (e.g., systemd) to keep Puma running:

4. Configure the Web Server (Nginx)

Sample Nginx Configuration

server {
  listen 80;
  server_name yourdomain.com;

  # Redirect HTTP to HTTPS
  return 301 https://$host$request_uri;
}

server {
  listen 443 ssl;
  server_name yourdomain.com;

  ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

  root /var/www/your_app/current/public;

  location / {
    proxy_pass http://unix:/var/www/your_app/shared/tmp/sockets/puma.sock;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }

  # Serve static assets directly
  location ~ ^/assets/ {
    expires max;
    gzip_static on;
    add_header Cache-Control public;
  }
}

5. Set Up Environment Variables

Use tools like dotenv, Figaro, or Rails Credentials to manage secrets. Never commit .env files to version control!

Example with Rails Credentials:

EDITOR=vi rails credentials:edit --environment production

6. Automate Deployment with Capistrano

Capistrano streamlines deployment by automating tasks like code updates, migrations, and restarts.

  1. Add Capistrano to your Gemfile:
  2. Configure config/deploy.rb and server-specific files (e.g., config/deploy/production.rb).

7. Enable Monitoring and Logging

  • Monitoring: Use tools like New Relic, Datadog, or Prometheus to track performance.
  • Logging: Centralize logs with Lograge and tools like Papertrail or ELK Stack.
  • Error Tracking: Integrate Sentry or Honeybadger for real-time error alerts.

8. Security Best Practices

  1. Enable HTTPS: Use Let’s Encrypt for free SSL certificates.
  2. Firewall Rules: Restrict access to ports 80, 443, and SSH (e.g., 22).
  3. Regular Updates: Keep OS, Ruby, Rails, and dependencies patched.
  4. Database Backups: Schedule daily backups (e.g., with pg_dump for PostgreSQL).

9. Post-Deployment Checklist

  1. Test all critical user flows (e.g., signup, payments).
  2. Verify cron jobs or background workers (e.g., Sidekiq).
  3. Check asset loading (CSS/JS/images).
  4. Monitor server resources (CPU, memory, disk space).

10. Common Pitfalls to Avoid

  • Ignoring Caching: Use Redis or Memcached for fragment caching.
  • No CI/CD Pipeline: Automate testing and deployment with GitHub Actions, GitLab CI, or CircleCI.
  • Skipping Load Testing: Simulate traffic with tools like JMeter or Locust.
  • Hardcoding Secrets: Always use environment variables or encrypted credentials.

Conclusion

Deploying a Rails app to production requires attention to detail, but following these steps will ensure a robust and scalable setup. Automate repetitive tasks, prioritize security, and continuously monitor your application to catch issues early. Happy deploying!

Leave a comment