Ticker

6/recent/ticker-posts

Deploying Rails with Capistrano & Puma on AWS EC2 in 15 minutes



Gem: https://index.rubygems.org/gems/capistrano_quick

Capistrano is a powerful deployment automation tool for Ruby on Rails applications. In this guide, we’ll set up Capistrano with Puma on an AWS EC2 instance to deploy a Rails application smoothly.

Why Use Capistrano?

✅ Automates deployment tasks
✅ Ensures a rollback option in case of failures
✅ Supports multi-server deployment

Let's get started!


Step 1: Create and Connect to EC2 Instance

After launching an Ubuntu EC2 instance, connect to it via SSH:

ssh -i "BitsAtom.pem" ubuntu@<EC2_PUBLIC_IP>

Step 2: Update & Upgrade the System

Before installing anything, update the system:

sudo apt-get update && sudo apt-get -y upgrade

Step 3: Create a Deploy User

Create a deploy user for deployment purposes:

sudo useradd -d /home/deploy -m deploy  
sudo passwd deploy  

Grant the user sudo privileges:

sudo visudo

Add this line at the end:

deploy ALL=(ALL) NOPASSWD:ALL

Step 4: Set Up SSH Keys for GitHub Access

Capistrano pulls the code from GitHub, so we need SSH access:

su - deploy  
ssh-keygen -t rsa -b 4096  
cat ~/.ssh/id_rsa.pub  

Copy the output and add it to GitHub as a Deploy Key in your repository settings.


Step 5: Install PostgreSQL & Create a Database

Install PostgreSQL:

sudo apt-get install -y postgresql postgresql-contrib libpq-dev  

Create a database user and database:

sudo -u postgres createuser -s bitsatom_emailer  
sudo -u postgres psql  

Inside the PostgreSQL console, set a password:

\password bitsatom_emailer

Exit (\q) and create the database:

sudo -u postgres createdb -O bitsatom_emailer bitsatom_emailer_production

Step 6: Install RVM & Ruby

We’ll use RVM to install Ruby:

su - deploy  
gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys \
  409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB  
\curl -sSL https://get.rvm.io | bash -s stable  
source /home/deploy/.bashrc  
rvm install 3.2.2  
rvm --default use 3.2.2  

Step 7: Create App Directory & Configure Database

mkdir -p ~/bitsatom_emailer/shared/config  
vim ~/bitsatom_emailer/shared/config/database.yml  

Add the following:

production:
  adapter: postgresql
  encoding: unicode
  database: bitsatom_emailer_production
  username: bitsatom_emailer
  password: root
  host: localhost
  port: 5432

Step 8: Install Nginx & SSL (Let’s Encrypt)

Install Nginx and Certbot for HTTPS:

sudo apt update  
sudo apt install -y nginx certbot python3-certbot-nginx  
sudo certbot --nginx -d yourdomain.com  

Step 9: Exclude Sensitive Files from Git

Ensure database.yml and application.yml are not in Git:

echo "config/database.yml" >> .gitignore  
echo "config/application.yml" >> .gitignore  

Step 10: Set Up Secret Key Base

Create config/secrets.yml:

vim config/secrets.yml
default: &default
  secret_key_base: <CHANGE_ME>
production:
  <<: *default

🚀 Deploy with Capistrano

At this point, you’re ready to configure and deploy using Capistrano. You can set up Capfile and deploy.rb, then deploy using:

cap production deploy

Conclusion

🎉 Congratulations! You’ve successfully set up a Rails deployment with Capistrano, Puma, PostgreSQL, and Nginx on an AWS EC2 instance.

🔹 Benefits of this Setup:
✅ Automated deployment with rollback support
✅ High-performance web server with Puma
✅ Secure PostgreSQL configuration

Got questions? Let us know in the comments! 🚀💡

Post a Comment

0 Comments