Learn how to deploy your Node.js application to an AWS EC2 instance with PM2 process manager and Nginx reverse proxy.
Prerequisites
Essential knowledge and skills you should have before starting
Things You'll Need
- AWS account with EC2 access
- Node.js application ready to deploy
- SSH client installed on your computer
- Domain name (optional)
Requirements
- Basic understanding of Linux commands
- AWS account credentials
- Node.js application with package.json
Steps
Follow these 10 steps to complete this guide
Launch an EC2 Instance
Log into your AWS Console and navigate to EC2. Click 'Launch Instance' and select Ubuntu Server 22.04 LTS. Choose t2.micro for the free tier. Create or select an existing key pair for SSH access.
Tips
- Start with t2.micro to stay within free tier limits
- Keep your .pem key file safe - you won't be able to download it again
Configure Security Groups
Edit the security group to allow inbound traffic on ports 22 (SSH), 80 (HTTP), and 443 (HTTPS). This ensures your application will be accessible from the internet.
# Security group rules needed:
# SSH (22) - Your IP
# HTTP (80) - Anywhere
# HTTPS (443) - Anywhere
# Custom TCP (3000) - Anywhere (for Node.js, can be removed after Nginx setup) Warnings
- Don't expose port 22 to 0.0.0.0/0 in production - limit it to your IP address
Connect to Your Instance via SSH
Use your terminal to connect to the EC2 instance using the downloaded key pair. Replace 'your-key.pem' with your key file name and the IP address with your instance's public IP.
chmod 400 your-key.pem
ssh -i "your-key.pem" ubuntu@ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com Install Node.js and npm
Update the package manager and install Node.js using NodeSource repository. This ensures you get the latest LTS version of Node.js.
# Update system packages
sudo apt update && sudo apt upgrade -y
# Install Node.js 18.x LTS
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# Verify installation
node --version
npm --version Tips
- Use Node.js LTS version for production deployments
- Consider using nvm for managing multiple Node.js versions
Install PM2 Process Manager
PM2 is a production process manager for Node.js applications. It keeps your app running, restarts it if it crashes, and manages logs.
# Install PM2 globally
sudo npm install -g pm2
# Verify installation
pm2 --version Clone and Set Up Your Application
Clone your application from your repository and install dependencies. If using environment variables, create a .env file with your configuration.
# Clone your repository
git clone https://github.com/yourusername/your-app.git
cd your-app
# Install dependencies
npm install
# Create environment file (if needed)
nano .env
# Add your environment variables, save with Ctrl+X, Y, Enter Warnings
- Never commit .env files to version control
- Make sure all production environment variables are set correctly
Start Your Application with PM2
Use PM2 to start your Node.js application. PM2 will keep it running in the background and automatically restart it if it crashes.
# Start your app with PM2
pm2 start npm --name "my-app" -- start
# Or if you have an index.js or server.js:
pm2 start server.js --name "my-app"
# View running processes
pm2 list
# View logs
pm2 logs
# Save PM2 configuration
pm2 save
# Set PM2 to start on system boot
pm2 startup Tips
- Use descriptive names for your PM2 processes
- Set up PM2 to auto-start on server reboot
Install and Configure Nginx
Nginx will act as a reverse proxy, forwarding requests from port 80 to your Node.js application running on port 3000.
# Install Nginx
sudo apt install nginx -y
# Create Nginx configuration
sudo nano /etc/nginx/sites-available/my-app
# Add this configuration:
server {
listen 80;
server_name your-domain.com www.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 site
sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/
# Test Nginx configuration
sudo nginx -t
# Restart Nginx
sudo systemctl restart nginx Set Up SSL Certificate (Optional)
Use Let's Encrypt to add free SSL certificates to your application for HTTPS support.
# Install Certbot
sudo apt install certbot python3-certbot-nginx -y
# Obtain SSL certificate
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
# Certbot will automatically configure Nginx for HTTPS
# Certificates auto-renew, but you can test renewal with:
sudo certbot renew --dry-run Tips
- Make sure your domain DNS is pointing to your EC2 instance's IP
- SSL certificates from Let's Encrypt are free and auto-renew
Test Your Deployment
Visit your domain or EC2 public IP in a browser to verify your application is running correctly. Check PM2 logs if you encounter any issues.
# Check if your app is running
pm2 status
# View real-time logs
pm2 logs my-app
# Check Nginx status
sudo systemctl status nginx
# Test from command line
curl http://your-domain.com Tips
- Keep PM2 logs open in a separate terminal while testing
- Use browser developer tools to check for any errors
Was this guide helpful?
Check out more step-by-step guides for development, deployment, debugging, and configuration.