Deploying a Ruby on Rails Application

πŸš€ Deploying a Ruby on Rails Application Like a Pro (Step-by-Step Guide) 🌍πŸ”₯

From Localhost to Live Server with Domains, Routing, Production Setup & Optimization

Deploying a Ruby on Rails application is one of the most powerful milestones for any developer. It’s the moment your project goes from:

πŸ’» β€œWorks on my machine” β†’ 🌍 Available to the whole world

ChatGPT Image Jan 29, 2026, 10_02_13 PM

In this guide, you’ll learn:

βœ… Every deployment step βœ… Production vs Development separation βœ… Domain + Routing basics βœ… Best optimization techniques βœ… Real examples + pro-level practices

Let’s begin! πŸš€


πŸ—οΈ 1. What Does Deployment Mean in Rails?

Deployment means:

  • Moving your Rails app from your laptop
  • To a live server (AWS, DigitalOcean, Render, etc.)
  • Configuring it for production users

A deployed Rails app includes:

🌐 Web Server (Nginx) βš™οΈ App Server (Puma) πŸ—„οΈ Database (PostgreSQL/MySQL) πŸ” Environment Variables πŸ“¦ Assets + Optimization


πŸ§‘β€πŸ’» 2. Prepare Your Rails App for Production

Before deploying, your Rails app must be production-ready.


Rails apps in production almost always use PostgreSQL.

Update Gemfile:

gem "pg"

Run:

bundle install

Update database config:

production:
  adapter: postgresql
  encoding: unicode
  database: myapp_production

βœ… Set Rails Environment Correctly

Rails has environments:

  • development (local)
  • test
  • production (live)

Rails automatically uses:

RAILS_ENV=production

in deployment.


🌍 3. Choose a Deployment Server

Popular options:

Platform Best For
AWS EC2 ☁️ Full control, scalable
Render πŸš€ Easiest Rails deployment
DigitalOcean 🌊 Affordable VPS
Heroku (limited free) 🟣 Beginner-friendly

We’ll explain deployment using AWS EC2 + Nginx + Puma (most professional setup).


☁️ 4. Setup Server (AWS EC2)


βœ… Launch an EC2 Instance

Steps:

  1. Go to AWS Console
  2. Launch Instance
  3. Select Ubuntu 22.04
  4. Enable port:
  • 22 (SSH)
  • 80 (HTTP)
  • 443 (HTTPS)

Download .pem key.


πŸ” Connect to Server via SSH

ssh -i mykey.pem ubuntu@your-server-ip

Now you are inside your cloud server πŸŽ‰


βš™οΈ 5. Install Required Dependencies

Update server packages:

sudo apt update && sudo apt upgrade -y

Install essentials:

sudo apt install git curl build-essential -y

βœ… Install Ruby

Use rbenv:

sudo apt install rbenv -y
rbenv install 3.2.2
rbenv global 3.2.2

Check:

ruby -v

βœ… Install Rails

gem install rails
rails -v

βœ… Install Node.js + Yarn (Assets)

Rails needs JS runtime:

sudo apt install nodejs yarn -y

πŸ—„οΈ 6. Setup Database (PostgreSQL)

Install PostgreSQL:

sudo apt install postgresql postgresql-contrib -y

Create DB user:

sudo -u postgres createuser myappuser -s
sudo -u postgres psql

Set password:

ALTER USER myappuser WITH PASSWORD 'password';

Exit:

\q

πŸ“¦ 7. Upload Rails Application Code

Clone from GitHub:

git clone https://github.com/yourusername/myapp.git
cd myapp

Install gems:

bundle install

πŸ” 8. Configure Environment Variables (Secrets)

Never hardcode secrets like:

  • API keys
  • DB passwords
  • Rails master key

Use:

EDITOR=nano rails credentials:edit

Or export variables:

export RAILS_MASTER_KEY=yourkey
export DATABASE_URL=postgres://...

βš™οΈ 9. Run Production Setup Commands


βœ… Precompile Assets

Rails compiles CSS/JS for production:

RAILS_ENV=production rails assets:precompile

βœ… Migrate Database

RAILS_ENV=production rails db:migrate

βœ… Seed Data (Optional)

RAILS_ENV=production rails db:seed

πŸš€ 10. Setup Puma App Server

Rails uses Puma in production.

Start Puma:

bundle exec puma -e production

But in real deployments, Puma runs as a service.

Create:

sudo nano /etc/systemd/system/puma.service

Example:

[Unit]
Description=Puma Rails Server
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/myapp
ExecStart=/home/ubuntu/.rbenv/shims/bundle exec puma -e production
Restart=always

[Install]
WantedBy=multi-user.target

Enable:

sudo systemctl start puma
sudo systemctl enable puma

🌐 11. Setup Nginx Reverse Proxy

Install Nginx:

sudo apt install nginx -y

Configure:

sudo nano /etc/nginx/sites-available/myapp

Example config:

server {
    listen 80;
    server_name example.com www.example.com;

    root /home/ubuntu/myapp/public;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
    }
}

Enable site:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled
sudo systemctl restart nginx

Now your Rails app is live πŸš€


🌍 12. Domain + Routing Explained


βœ… How Domain Works

When a user visits:

www.example.com

DNS maps domain β†’ server IP.


Steps:

  1. Buy domain from GoDaddy/Namecheap
  2. Add DNS Record:
Type Name Value
A @ Server IP
CNAME www example.com

Within minutes, domain points to your Rails server.


βœ… Routing in Rails

Rails routing decides:

URL β†’ Controller Action

Example:

get "/about", to: "pages#about"

User visits:

example.com/about

Rails runs:

PagesController#about

⚑ 13. Optimization Techniques for Production

Deploying is not enough. Optimize it! πŸš€


βœ… Enable Caching

In production.rb:

config.action_controller.perform_caching = true

βœ… Use Background Jobs

Use Sidekiq for heavy tasks:

gem "sidekiq"

Example:

EmailJob.perform_later(user.id)

βœ… Use CDN for Assets

Serve images & JS faster via Cloudflare/AWS CloudFront.


βœ… Database Indexing

Add indexes:

add_index :users, :email

Speeds up queries massively ⚑


βœ… Use SSL (HTTPS)

Install Certbot:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx

Now your app is secure πŸ”’


πŸ§ͺ 14. Separating Development vs Production Properly

Rails automatically separates environments:

Feature Development Production
Debug logs βœ… Yes ❌ No
Caching ❌ Off βœ… On
Assets Live reload Precompiled
Errors Full trace Friendly pages

Use:

rails s

for dev

Use:

RAILS_ENV=production rails s

for prod


🎯 Final Deployment Checklist βœ…

βœ” App runs locally βœ” PostgreSQL configured βœ” Secrets stored safely βœ” Assets precompiled βœ” Puma service running βœ” Nginx routing works βœ” Domain connected βœ” HTTPS enabled βœ” Production optimized


🌟 Conclusion: Rails Deployment Mastery

Deploying Rails is a superpower πŸ’Ž Once you master it, you can launch:

πŸš€ SaaS Products 🌍 Startups πŸ“¦ APIs πŸ›’ E-commerce apps πŸ“± Mobile backends

Rails is built for production success!

© Lakhveer Singh Rajput - Blogs. All Rights Reserved.