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
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.
β Use PostgreSQL (Recommended)
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:
- Go to AWS Console
- Launch Instance
- Select Ubuntu 22.04
- 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:
- Buy domain from GoDaddy/Namecheap
- 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.