Skip to content

Latest commit

 

History

History
606 lines (430 loc) · 9.72 KB

File metadata and controls

606 lines (430 loc) · 9.72 KB

Deployment Guide

Deploy Crypto Data Aggregator to production.


Table of Contents


Vercel (Recommended)

Vercel is the recommended platform for Next.js applications.

One-Click Deploy

Deploy with Vercel

Manual Deployment

  1. Install Vercel CLI
npm install -g vercel
  1. Login to Vercel
vercel login
  1. Deploy
# From project root
vercel

# For production
vercel --prod
  1. Configure Environment Variables
# Set optional API key for higher rate limits
vercel env add COINGECKO_API_KEY

Vercel Configuration

Create vercel.json for custom settings:

{
  "framework": "nextjs",
  "regions": ["iad1"],
  "headers": [
    {
      "source": "/api/(.*)",
      "headers": [
        { "key": "Access-Control-Allow-Origin", "value": "*" },
        { "key": "Cache-Control", "value": "public, s-maxage=60" }
      ]
    }
  ]
}

Edge Functions

API routes automatically deploy to Edge Runtime for low latency:

// src/app/api/market/coins/route.ts
export const runtime = 'edge';

Railway

Railway provides simple deployments with automatic scaling.

Quick Start

  1. Create Railway Account

    Go to railway.app and sign up.

  2. Deploy from GitHub

    • Click "New Project"
    • Select "Deploy from GitHub repo"
    • Choose crypto-data-aggregator
    • Railway auto-detects Next.js
  3. Configure Build

    Railway auto-detects settings. Override if needed:

    Build Command: npm run build
    Start Command: npm start
    
  4. Add Environment Variables

    In Railway dashboard:

    • Go to Variables tab
    • Add COINGECKO_API_KEY (optional)

Railway Configuration

Create railway.json:

{
  "$schema": "https://railway.app/railway.schema.json",
  "build": {
    "builder": "NIXPACKS"
  },
  "deploy": {
    "numReplicas": 1,
    "restartPolicyType": "ON_FAILURE",
    "restartPolicyMaxRetries": 3
  }
}

Custom Domain

  1. Go to Settings → Domains
  2. Add custom domain
  3. Configure DNS:
    CNAME your-app.railway.app
    

Docker

Deploy with Docker for maximum portability.

Dockerfile

Create Dockerfile in project root:

# Build stage
FROM node:20-alpine AS builder

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci

# Copy source
COPY . .

# Build application
RUN npm run build

# Production stage
FROM node:20-alpine AS runner

WORKDIR /app

ENV NODE_ENV=production

# Create non-root user
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# Copy built application
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

CMD ["node", "server.js"]

Docker Compose

Create docker-compose.yml:

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=production
      - COINGECKO_API_KEY=${COINGECKO_API_KEY:-}
    restart: unless-stopped
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:3000/api/trending']
      interval: 30s
      timeout: 10s
      retries: 3

Build & Run

# Build image
docker build -t crypto-data-aggregator .

# Run container
docker run -p 3000:3000 crypto-data-aggregator

# With environment variables
docker run -p 3000:3000 \
  -e COINGECKO_API_KEY=your_key \
  crypto-data-aggregator

Docker Compose

# Start
docker-compose up -d

# View logs
docker-compose logs -f

# Stop
docker-compose down

Next.js Standalone Output

Enable standalone output in next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: 'standalone',
};

module.exports = nextConfig;

Self-Hosted

Deploy to your own server.

Prerequisites

  • Node.js 18+
  • npm or yarn
  • Process manager (PM2 recommended)
  • Reverse proxy (nginx recommended)

Setup

  1. Clone Repository
git clone https://github.com/nirholas/crypto-data-aggregator.git
cd crypto-data-aggregator
  1. Install Dependencies
npm ci --production=false
  1. Build Application
npm run build
  1. Install PM2
npm install -g pm2
  1. Create PM2 Config

Create ecosystem.config.js:

module.exports = {
  apps: [
    {
      name: 'crypto-data-aggregator',
      script: 'npm',
      args: 'start',
      cwd: '/path/to/crypto-data-aggregator',
      instances: 'max',
      exec_mode: 'cluster',
      env: {
        NODE_ENV: 'production',
        PORT: 3000,
      },
      env_production: {
        NODE_ENV: 'production',
        COINGECKO_API_KEY: 'your_key',
      },
    },
  ],
};
  1. Start Application
pm2 start ecosystem.config.js --env production
pm2 save
pm2 startup

Nginx Configuration

server {
    listen 80;
    server_name crypto.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_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }

    # Cache static assets
    location /_next/static {
        proxy_pass http://localhost:3000;
        proxy_cache_valid 200 365d;
        add_header Cache-Control "public, max-age=31536000, immutable";
    }
}

SSL with Certbot

# Install certbot
sudo apt install certbot python3-certbot-nginx

# Get certificate
sudo certbot --nginx -d crypto.yourdomain.com

# Auto-renewal
sudo certbot renew --dry-run

Environment Variables

Required

None. The application works with free tier APIs.

Optional

Variable Description Default
COINGECKO_API_KEY CoinGecko Pro API key -
COINGECKO_BASE_URL CoinGecko API URL https://api.coingecko.com/api/v3
DEFILLAMA_BASE_URL DeFiLlama API URL https://api.llama.fi

Setting Environment Variables

Vercel

vercel env add COINGECKO_API_KEY

Or via dashboard: Settings → Environment Variables

Railway

Dashboard: Variables tab → Add variable

Docker

docker run -e COINGECKO_API_KEY=xxx ...

Or in docker-compose.yml:

environment:
  - COINGECKO_API_KEY=xxx

Self-Hosted

Create .env.local:

COINGECKO_API_KEY=your_api_key
COINGECKO_BASE_URL=https://pro-api.coingecko.com/api/v3

Post-Deployment

Verify Deployment

# Check health
curl https://your-domain.com/api/trending

# Check specific endpoint
curl https://your-domain.com/api/market/coins?limit=5

Monitor Performance

Vercel Analytics

Enable in next.config.js:

const nextConfig = {
  experimental: {
    webVitals: true,
  },
};

Custom Monitoring

Add response timing in API routes:

import { withTiming } from '@/lib/api-utils';

export async function GET() {
  const startTime = Date.now();
  const data = await fetchData();
  return Response.json(withTiming(data, startTime));
}

Troubleshooting

Build Fails

# Clear cache and rebuild
rm -rf .next node_modules
npm install
npm run build

API Rate Limited

  • Add COINGECKO_API_KEY for higher limits
  • Check rate limit state in logs
  • Increase cache TTLs if needed

Memory Issues

For self-hosted:

# Increase Node.js memory
NODE_OPTIONS="--max-old-space-size=4096" npm start

For Docker:

deploy:
  resources:
    limits:
      memory: 1G

Scaling

Horizontal Scaling

Vercel

Automatic. Edge functions scale globally.

Railway

Increase replicas in settings or railway.json:

{
  "deploy": {
    "numReplicas": 3
  }
}

Docker/Self-Hosted

Use PM2 cluster mode:

// ecosystem.config.js
{
  instances: 'max',  // Use all CPU cores
  exec_mode: 'cluster',
}

Caching Recommendations

For high traffic:

  1. Enable Redis for shared cache
  2. Use CDN for static assets
  3. Increase stale-while-revalidate windows

CI/CD

GitHub Actions

Create .github/workflows/deploy.yml:

name: Deploy

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - run: npm ci
      - run: npm run lint
      - run: npm run test:run
      - run: npm run build

      - uses: amondnet/vercel-action@v25
        with:
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
          vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
          vercel-args: '--prod'

Preview Deployments

Vercel automatically creates preview deployments for PRs.

For Railway:

{
  "build": {
    "builder": "NIXPACKS"
  }
}

Railway creates preview environments from PRs when connected to GitHub.