Ruby on Rails Production Setup
π Ruby on Rails Production Setup: The Complete Guide to Deploying Secure, Fast & Scalable Rails Applications π
βAnyone can make a Rails app work locally. Great engineers make it reliable in production.β
Building an application is only 30% of the journey. The remaining 70% lies in deploying, monitoring, securing, scaling, and maintaining it in production.
Many developers deploy a Rails application successfully but encounter issues such as:
β Downtime during deployment β Slow response times β Memory leaks β Database bottlenecks β Security vulnerabilities β Lost logs β Broken assets β SSL issues β Background job failures
This guide covers everything you should know before taking your Ruby on Rails application to production, from infrastructure planning to monitoring and scaling.
π Table of Contents
- Production Mindset
- Infrastructure Planning
- Choosing a Cloud Provider
- Server Setup
- Ruby Installation
- PostgreSQL Setup
- Redis Setup
- Web Server
- Application Server
- Reverse Proxy
- SSL Configuration
- Environment Variables
- Rails Credentials
- Asset Pipeline
- Active Storage
- Background Jobs
- Cron Jobs
- Logging
- Monitoring
- Error Tracking
- Performance Optimization
- Security Checklist
- Scaling
- Deployment Strategies
- Backup Strategy
- Production Mistakes
- Production Checklist
π― 1. Production Mindset
Production is different from development.
Development focuses on:
- Writing features
- Debugging
- Rapid iteration
Production focuses on:
- Stability
- Security
- Performance
- Reliability
- Scalability
Think like a DevOps engineer.
βοΈ 2. Choose Infrastructure
Popular choices include:
| Platform | Best For |
|---|---|
| AWS EC2 | Full control |
| AWS ECS | Containers |
| AWS EKS | Kubernetes |
| DigitalOcean | Small apps |
| Render | Easy deployment |
| Railway | Side projects |
| Fly.io | Fast deployment |
| Heroku | Simplicity |
| Hatchbox | Rails-specific |
| Kamal | Docker deployment |
π₯οΈ 3. Server Requirements
Typical stack:
Ubuntu 24.04 LTS
Ruby
PostgreSQL
Redis
Nginx
Puma
Node
Yarn/Bun
ImageMagick
Git
Certbot
π 4. Install Ruby Properly
Recommended:
mise
or
rbenv
Avoid:
β System Ruby
Reason:
- Easier upgrades
- Version management
- Multiple projects
ποΈ 5. PostgreSQL Configuration
Production database tips:
β Enable connection pooling
pool: 20
Enable:
- WAL
- Backups
- Replication
- Auto Vacuum
Indexes are critical.
Avoid:
SELECT *
Instead:
Select only required columns.
β‘ 6. Redis
Redis powers:
- Sidekiq
- Action Cable
- Cache
- Sessions
Keep Redis separate from PostgreSQL.
π 7. Nginx
Nginx handles:
- SSL
- Static assets
- Compression
- Reverse proxy
- Load balancing
Flow:
Internet
β
Nginx
β
Puma
β
Rails
π 8. Puma
Puma is the default Rails web server.
Configure:
Workers
Threads
Example:
WEB_CONCURRENCY=2
RAILS_MAX_THREADS=5
Too many workers can exhaust RAM.
π 9. SSL
Always enable HTTPS.
Use:
Let's Encrypt
Auto renew certificates.
Enable:
HTTP β HTTPS redirect
π 10. Secrets Management
Never store secrets in Git.
Use:
Rails Credentials
config/credentials.yml.enc
or
Environment Variables.
Store:
- API keys
- Secret Key Base
- Database passwords
- AWS keys
π 11. Active Storage
Storage options:
Development
Local Disk
Production
Amazon S3
Cloudflare R2
Google Cloud Storage
Azure Blob
Never keep uploads only on the application server if you plan to scale horizontally.
π¨ 12. Asset Pipeline
Precompile assets:
rails assets:precompile
Enable:
config.public_file_server.enabled
Compress:
- CSS
- JS
- Images
Use fingerprinting.
βοΈ 13. Background Jobs
Never perform long tasks in controllers.
Use:
- Sidekiq
- Solid Queue
- GoodJob
- Delayed Job
Examples:
- PDF generation
- Notifications
- Reports
- Image processing
π 14. Cron Jobs
Use:
Whenever gem
or
Linux Cron
Tasks:
- Cleanup
- Reports
- Backups
- Notifications
π 15. Logging
Log everything important.
Examples:
Request
Response
Errors
Background Jobs
Authentication
Payments
Use:
lograge
to simplify logs.
Rotate logs regularly.
π¨ 16. Error Tracking
Use:
β Sentry
β Bugsnag
β Honeybadger
Receive alerts instantly.
π 17. Monitoring
Monitor:
CPU
RAM
Disk
Redis
Database
Queue
Response Time
Uptime
Tools:
- Grafana
- Prometheus
- Datadog
- New Relic
- AppSignal
π 18. Performance Optimization
Cache
Use:
Fragment Cache
Russian Doll Cache
Low-Level Cache
Redis Cache
Eager Loading
Avoid:
N+1 Queries
Use:
includes
Database
Always add indexes.
Example:
add_index :users, :email
Pagination
Never load:
100000 rows
Use:
Pagy
Kaminari
Compression
Enable:
gzip
brotli
CDN
Serve assets via:
Cloudflare
Fastly
AWS CloudFront
π‘οΈ 19. Security
Enable:
Force SSL
config.force_ssl = true
Secure Headers
X-Frame-Options
CSP
HSTS
CSRF Protection
Rails enables it by default.
Donβt disable it.
Strong Parameters
Never trust user input.
SQL Injection
Use:
where(id: params[:id])
Avoid:
"SELECT * FROM users WHERE id=#{params[:id]}"
Brute Force Protection
Use:
Rack Attack
Authentication
Use:
Devise
Auth0
JWT
Authorization
Use:
Pundit
CanCanCan
π¦ 20. Backups
Daily:
Database
Weekly:
Files
Monthly:
Full snapshot
Always test restore.
A backup you cannot restore is not a backup.
π 21. Scaling
Vertical Scaling
Increase:
- CPU
- RAM
Horizontal Scaling
Increase:
Multiple servers
Add:
Load Balancer
Shared Redis
Shared Database
Shared Storage
π 22. Deployment Strategies
Blue-Green Deployment
Old Server
β
Switch Traffic
β
New Server
Zero downtime.
Rolling Deployment
Update:
Server 1
β
Server 2
β
Server 3
Canary
Deploy to:
5%
Monitor
Deploy to:
100%
β οΈ Common Production Mistakes
1.
Running
RAILS_ENV=development
2.
Hardcoding secrets
3.
No SSL
4.
No backups
5.
No monitoring
6.
Running migrations during peak traffic without planning
7.
No indexes
8.
Huge ActiveRecord queries
9.
Memory leaks
10.
Ignoring logs
11.
Single server with no redundancy
12.
No health checks
13.
No rate limiting
14.
Uploading files locally
15.
Blocking requests with long jobs
π§° Recommended Production Stack
| Category | Recommendation |
|---|---|
| OS | Ubuntu LTS |
| Ruby | mise or rbenv |
| Database | PostgreSQL |
| Cache | Redis |
| App Server | Puma |
| Reverse Proxy | Nginx |
| Background Jobs | Sidekiq or Solid Queue |
| Object Storage | Amazon S3 / Cloudflare R2 |
| CDN | Cloudflare |
| Monitoring | Grafana + Prometheus / AppSignal |
| Error Tracking | Sentry |
| Deployment | Kamal, Capistrano, or Hatchbox |
| SSL | Letβs Encrypt |
| Authentication | Devise |
| Authorization | Pundit |
| Pagination | Pagy |
| Logging | Lograge |
π Ultimate Rails Production Checklist
Infrastructure
- β Ubuntu LTS installed
- β Firewall (UFW) configured
- β SSH keys configured
- β Automatic security updates enabled
- β Time synchronization (NTP) configured
Application
- β
RAILS_ENV=production - β
SECRET_KEY_BASEconfigured - β Rails credentials encrypted
- β Assets precompiled
- β Database migrated
- β Health check endpoint added
- β Environment variables validated
Database
- β PostgreSQL optimized
- β Proper indexes added
- β Connection pool configured
- β Automated backups scheduled
- β Restore process tested
Web & App Server
- β Nginx configured
- β Puma workers tuned
- β Gzip/Brotli enabled
- β HTTP β HTTPS redirect
- β Static assets served efficiently
Security
- β Force SSL enabled
- β Content Security Policy configured
- β HSTS enabled
- β Secure cookies enabled
- β Rack::Attack or equivalent rate limiting
- β Strong Parameters enforced
- β Dependencies scanned for vulnerabilities
Background Processing
- β Sidekiq/Solid Queue running
- β Redis monitored
- β Cron jobs scheduled
- β Failed jobs retry strategy configured
Monitoring & Operations
- β Centralized logging
- β Error tracking (Sentry/Honeybadger)
- β Uptime monitoring
- β Metrics dashboard (CPU, RAM, DB, Redis)
- β Alerts configured
Scaling & Reliability
- β Shared object storage
- β CDN enabled
- β Load balancer (if multiple instances)
- β Zero-downtime deployment strategy
- β Rollback plan documented
π― Final Thoughts
A successful Rails deployment isnβt just about getting an application onlineβitβs about building a platform that is secure, observable, resilient, and easy to maintain. By combining proven Rails practices with robust infrastructure, continuous monitoring, disciplined deployments, and regular backups, you can confidently serve users at any scale.
Remember this progression:
Build β Secure β Optimize β Monitor β Scale β Automate
Following this lifecycle will help you avoid common production pitfalls and ensure your Rails application remains fast, reliable, and ready for growth.
© Lakhveer Singh Rajput - Blogs. All Rights Reserved.