⚠️
Advanced

This guide requires familiarity with Linux, SSH, and server administration. For simpler deployment, use Vercel.

📋 Prerequisites

  • AWS account with EC2 access
  • Basic Linux command line knowledge
  • Domain name (for SSL)
  • All environment variables ready

🖥️ Launch EC2 Instance

1

Launch Instance

Go to EC2 → Launch Instance. Select Ubuntu 22.04 LTS. Minimum t2.small (2GB RAM).

2

Configure Security Group

Allow ports: 22 (SSH), 80 (HTTP), 443 (HTTPS), 3000 (Next.js).

3

Create Key Pair

Download .pem file for SSH access.

4

Connect via SSH

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

🔧 Server Setup

Install Dependencies
# Update system
sudo apt update && sudo apt upgrade -y

# Install Node.js 20
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

# Install PM2
sudo npm install -g pm2

# Install Nginx
sudo apt install -y nginx

📦 Deploy Application

Clone and Build
# Clone repository
git clone https://github.com/your-username/betterlearn.git
cd betterlearn

# Install dependencies
npm install

# Create .env.local
nano .env.local
# Add all environment variables

# Build application
npm run build

# Start with PM2
pm2 start npm --name "betterlearn" -- start
pm2 save
pm2 startup

🔒 Nginx & SSL

/etc/nginx/sites-available/betterlearn
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;
    }
}
Enable Site & SSL
# Enable site
sudo ln -s /etc/nginx/sites-available/betterlearn /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

# Install SSL with Certbot
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com

🔄 Updating

Update Deployment
cd betterlearn
git pull origin main
npm install
npm run build
pm2 restart betterlearn

📊 Monitoring

CommandPurpose
pm2 statusCheck app status
pm2 logs betterlearnView app logs
pm2 monitReal-time monitoring