Deploying Rails on AWS EC2

πŸš€ Deploying Rails on AWS EC2: The No-Mistake Step-by-Step Guide

The Ultimate Beginner-to-Pro Deployment Handbook πŸ’‘πŸ”§

Deploying a Ruby on Rails app to AWS EC2 can be confusing β€” servers, dependencies, secrets, networking, ports… one wrong step and everything crashes. This guide removes all guesswork and gives you a bulletproof, production-grade deployment flow. Let’s do it β€” mistake-free! πŸ’―βœ¨

ChatGPT Image Nov 19, 2025, 05_33_50 PM


🧠 Concepts You MUST Know Before Deploying

πŸ”Έ 1. AWS EC2 (Elastic Compute Cloud)

It’s your virtual Linux machine in the cloud. Think of it as a laptop running 24x7, where your Rails app lives.

πŸ”Έ 2. SSH (Secure Shell)

A secure way to connect to your EC2 instance using your terminal.

πŸ”Έ 3. Nginx

A powerful web server that receives browser requests β†’ forwards to Rails.

πŸ”Έ 4. Passenger / Puma

The Rails application server.

  • Passenger integrates well with Nginx (classic choice).
  • Puma is fast and modern (default Rails choice).

πŸ”Έ 5. RDS (Optional)

Managed database service for PostgreSQL/MySQL.

πŸ”Έ 6. Deployment Directory Structure

Example:

/var/www/myapp
β”œβ”€β”€ current     # Active version
β”œβ”€β”€ shared      # Logs, uploads, secrets
└── releases    # Old deployments

πŸ“¦ Step 1: Launch Your EC2 Instance

  • AMI: Ubuntu 22.04 LTS
  • Instance type: t2.micro (free-tier)
  • Storage: 20GB
  • Security groups:

    • SSH β†’ port 22
    • HTTP β†’ port 80
    • HTTPS β†’ port 443

πŸ“ Generate a key pair

Download myapp.pem β€” don’t lose it! You’ll use it to SSH into the server.


πŸ”— Step 2: SSH Into the Server

chmod 400 myapp.pem
ssh -i myapp.pem ubuntu@your-public-ip

If connected, you’re inside your cloud machine πŸ™Œ


πŸ› οΈ Step 3: Install Dependencies

πŸ“Œ Update packages

sudo apt update && sudo apt upgrade -y

πŸ“Œ Install Git, cURL, Nginx

sudo apt install -y git curl gnupg nginx build-essential

πŸ”₯ Step 4: Install Ruby Using rbenv

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
rbenv init

Install Ruby-build:

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
sudo apt-get install -y libssl-dev libreadline-dev zlib1g-dev

Install Ruby:

rbenv install 3.3.0
rbenv global 3.3.0

Install Bundler:

gem install bundler

πŸ—„οΈ Step 5: Install and Configure PostgreSQL (or MySQL)

Install PostgreSQL

sudo apt install postgresql postgresql-contrib -y

Create user & database:

sudo -u postgres createuser --interactive
sudo -u postgres createdb myapp_production

Set password:

sudo -u postgres psql
ALTER USER username WITH PASSWORD 'your-password';

πŸ“ Step 6: Clone Your Rails App

cd /var/www
sudo mkdir myapp
sudo chown ubuntu:ubuntu myapp
cd myapp
git clone https://github.com/your/repo.git .

Install gems:

bundle install

Set up secrets:

EDITOR="nano" bin/rails credentials:edit

πŸ—ƒοΈ Step 7: Precompile Assets & Migrate DB

RAILS_ENV=production bin/rails assets:precompile
RAILS_ENV=production bin/rails db:migrate

πŸ”₯ Step 8: Install & Configure Passenger + Nginx (or Puma)

Install Passenger

sudo apt install -y dirmngr gnupg
sudo sh -c 'curl -sS https://oss-binaries.phusionpassenger.com/apt/passenger/$(lsb_release -sc) \
 | sudo tee /etc/apt/sources.list.d/passenger.list'
sudo apt update
sudo apt install -y libnginx-mod-http-passenger

Check installation:

passenger-config validate-install

🌐 Step 9: Configure Nginx for Rails

Edit configuration:

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

Paste this:

server {
    listen 80;
    server_name your-server-ip;

    root /var/www/myapp/public;

    passenger_enabled on;
    passenger_ruby /home/ubuntu/.rbenv/shims/ruby;

    client_max_body_size 20m;
}

Enable site:

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

If you see your app in the browser β†’ πŸŽ‰ success!


☁️ Step 10: Setup Firewall (UFW)

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

πŸŽ‰ Step 11: Deploy Updates Without Errors (The Safe Way!)

Pull updates:

git pull origin main
bundle install
RAILS_ENV=production rails db:migrate
rails assets:precompile
sudo systemctl restart nginx

πŸ“Š High-Level Deployment Architecture Diagram

User β†’ Nginx β†’ Passenger/Puma β†’ Rails App β†’ PostgreSQL (local or RDS)

⚑ Tips to Improve Your Deployment

⭐ 1. Use ENV variables instead of credentials

Store secrets with:

  • AWS SSM Parameter Store
  • dotenv gem
  • Rails encrypted credentials

⭐ 2. Move DB to AWS RDS

Better scaling, backups, monitoring.

⭐ 3. Enable HTTPS with Let’s Encrypt

Use Certbot:

sudo certbot --nginx

⭐ 4. Enable Auto-Restart for Rails

With Passenger, it restarts automatically after deployments.

⭐ 5. Create Deployment Script

Automate with Bash or GitHub Actions.

⭐ 6. Use CloudWatch for Monitoring

Track RAM, CPU, traffic.

⭐ 7. Use Load Balancer + Auto Scaling

For big applications.


🏁 Final Words

Deploying a Rails app on AWS EC2 is not hard β€” it’s just a checklist. Follow the exact steps above and you’ll have a production-grade, scalable, secure infrastructure with zero mistakes. πŸš€πŸ”₯

© Lakhveer Singh Rajput - Blogs. All Rights Reserved.