Deploying Node.js Apps to Heroku, AWS, and DigitalOcean
“Deploying Node.js Apps to Heroku, AWS, and DigitalOcean”
Preparing for Production
- Environment Variables: Use
dotenv
for configuration (e.g., database URLs, API keys). - Logging: Replace
console.log
with Winston or Morgan. - 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
- In AWS Console, choose an Ubuntu AMI.
- 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
- Install Nginx:
sudo apt install nginx
- 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; } }
- 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
- In DigitalOcean, link your GitHub/GitLab repository.
- Select “Web Service” and configure:
- Run Command:
node app.js
- Environment Variables: Add
NODE_ENV=production
.
- Run Command:
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