Skip to content

How to Deploy GymFlow on an AWS EC2 Instance

  • by

This guide explains how to deploy GymFlow, a SaaS platform built with Next.js 14, NestJS, Prisma, PostgreSQL, Redis, and Stripe, on an AWS EC2 Ubuntu server.

Prerequisites

Before installing GymFlow, make sure you have:

  • An AWS account
  • An Ubuntu 22.04 LTS EC2 instance
  • At least a t3.medium instance or equivalent
  • A domain name pointed to the server’s Elastic IP
  • SSH access to the EC2 instance

Step 1 — Connect to the AWS EC2 Instance

Connect using SSH:

ssh -i your-key.pem ubuntu@your-ec2-public-ip

Step 2 — Install Node.js

Install Node.js 20:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs

Verify:

node -v
npm -v

Install npm 11:

sudo npm install -g npm@11

Step 3 — Install PostgreSQL

Install PostgreSQL:

sudo apt install postgresql postgresql-contrib -y

Enable PostgreSQL:

sudo systemctl enable postgresql
sudo systemctl start postgresql

Create the GymFlow database user:

sudo -u postgres psql -c "CREATE USER gymflow_user WITH PASSWORD 'strong_password';"

Create the database:

sudo -u postgres psql -c "CREATE DATABASE gymflow_db OWNER gymflow_user;"

Use a strong production password instead of the example password.

Step 4 — Install Redis

Install Redis:

sudo apt install redis-server -y

Enable Redis:

sudo systemctl enable redis-server
sudo systemctl start redis-server

Verify Redis:

redis-cli ping

A successful installation should return:

PONG

Step 5 — Download GymFlow

Navigate to /var/www:

cd /var/www

Clone the GymFlow repository:

sudo git clone https://github.com/your-org/gymflow.git

Set ownership:

sudo chown -R ubuntu:ubuntu gymflow

Enter the project directory:

cd gymflow

Step 6 — Configure GymFlow

Create the environment file:

cp .env.example .env

Edit it:

nano .env

Configure the GymFlow production settings:

DATABASE_URL="postgresql://gymflow_user:strong_password@localhost:5432/gymflow_db"
REDIS_URL="redis://localhost:6379"
JWT_SECRET="your-very-long-random-secret"
NEXT_PUBLIC_API_URL="https://api.yourdomain.com"

STRIPE_SECRET_KEY="sk_live_..."
SENDGRID_API_KEY="SG...."

Keep all GymFlow credentials and API keys private.

Switch GymFlow to PostgreSQL:

npm run db:provider:postgresql

Step 7 — Install GymFlow Dependencies

Install the project dependencies:

npm install

Build GymFlow:

npm run build

Run the Prisma migrations:

npm run db:migrate

Seed the GymFlow database:

npm run db:seed

Step 8 — Install PM2

Install PM2:

sudo npm install -g pm2

Start the GymFlow API:

pm2 start "npm run start --workspace=apps/api" --name "gymflow-api"

Start the GymFlow web application:

pm2 start "npm run start --workspace=apps/web" --name "gymflow-web"

Save the PM2 configuration:

pm2 save

Configure PM2 to restart automatically after a reboot:

pm2 startup

Run the command displayed by PM2.

Step 9 — Configure Nginx for GymFlow

Install Nginx:

sudo apt install nginx -y

Create the Nginx configuration:

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

Add:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

server {
    listen 80;
    server_name api.yourdomain.com;

    location / {
        proxy_pass http://localhost:4000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Enable the GymFlow configuration:

sudo ln -s /etc/nginx/sites-available/gymflow /etc/nginx/sites-enabled/

Test Nginx:

sudo nginx -t

Restart Nginx:

sudo systemctl restart nginx

Step 10 — Enable HTTPS for GymFlow

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Create SSL certificates:

sudo certbot --nginx -d yourdomain.com -d api.yourdomain.com

Your GymFlow application and API will now use HTTPS.

Step 11 — Configure S3 for GymFlow File Uploads

For production GymFlow deployments, Amazon S3 is recommended for file uploads.

Create an S3 bucket in AWS and add the required credentials to .env:

AWS_ACCESS_KEY_ID="AKIA..."
AWS_SECRET_ACCESS_KEY="..."
AWS_REGION="us-east-1"
AWS_S3_BUCKET="gymflow-uploads"

Use an IAM user or role with only the permissions required by GymFlow.

Step 12 — Verify GymFlow

Open:

https://yourdomain.com

The GymFlow web application should load.

Monitor the application with:

pm2 status

View logs:

pm2 logs

Use:

pm2 monit

for live process monitoring.