Deploying Node.js Apps to Heroku, AWS, and DigitalOcean

1 minute read


“Deploying Node.js Apps to Heroku, AWS, and DigitalOcean”


Preparing for Production

  1. Environment Variables: Use dotenv for configuration (e.g., database URLs, API keys).
  2. Logging: Replace console.log with Winston or Morgan.
  3. Process Manager: Use PM2 to keep apps alive after crashes.

Deploying to Heroku

Step 1: Install Heroku CLI

brew tap heroku/brew && brew install heroku  # macOS
sudo snap install heroku --classic          # Linux

Step 2: Create a Heroku App

heroku login
heroku create your-app-name

Step 3: Configure Procfile
Add Procfile to your project root:

web: node app.js

Step 4: Deploy via Git

git init
git add .
git commit -m "Initial commit"
git push heroku main

Step 5: Set Environment Variables

heroku config:set NODE_ENV=production DATABASE_URL=your_mongodb_url

Deploying to AWS EC2

Step 1: Launch an EC2 Instance

  1. In AWS Console, choose an Ubuntu AMI.
  2. Configure security groups to allow HTTP (80), HTTPS (443), and SSH (22).

Step 2: Connect via SSH

ssh -i "your-key.pem" ubuntu@ec2-xx-xx-xx-xx.compute-1.amazonaws.com

Step 3: Install Node.js and PM2

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
sudo npm install -g pm2

Step 4: Clone and Run Your App

git clone https://github.com/yourusername/your-app.git
cd your-app
npm install
pm2 start app.js

Step 5: Set Up Nginx Reverse Proxy

  1. Install Nginx:
    sudo apt install nginx
    
  2. Configure /etc/nginx/sites-available/your-app:
    server {
      listen 80;
      server_name your-domain.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;
      }
    }
    
  3. Enable the configuration and restart Nginx:
    sudo ln -s /etc/nginx/sites-available/your-app /etc/nginx/sites-enabled/
    sudo systemctl restart nginx
    

Deploying to DigitalOcean App Platform

Step 1: Create a New App

  1. In DigitalOcean, link your GitHub/GitLab repository.
  2. Select “Web Service” and configure:
    • Run Command: node app.js
    • Environment Variables: Add NODE_ENV=production.

Step 2: Auto-Deploy on Git Push
Enable automatic deployments for specific branches.

Step 3: Add a Database (Optional)
Attach a managed MongoDB or PostgreSQL database from the marketplace.


Scaling and Monitoring

  • Heroku: Scale dynos via heroku ps:scale web=2.
  • AWS: Use Elastic Load Balancer (ELB) with multiple EC2 instances.
  • DigitalOcean: Adjust instance size and enable horizontal scaling.

Conclusion

You’ve deployed Node.js apps to Heroku, AWS, and DigitalOcean! Next steps:

  • Implement CI/CD pipelines (e.g., GitHub Actions).
  • Monitor performance with tools like New Relic or Datadog.
  • Secure apps with HTTPS using Let’s Encrypt.

Leave a comment