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! π―β¨
π§ 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
π§ Recommended Setup
- 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.