Continuous Deployment of Node.js REST API with AWS EC2 and GitHub Actions
Step 1: Create Your Node.js Server
The first step is to create a Node.js REST API that will act as the backbone of your application.we’ll create a simple Node.js server from scratch. Below is a basic structure of the project as this stucture,
4. Environment Setup
Create a .env file to store environment variables such as your MongoDB connection string
If you are using MongoDB, ensure it’s accessible from any IP address by updating your network access settings in MongoDB Atlas (or your chosen DB host).
Step 2: Launch an EC2 Instance & Push Code
To host your Node.js REST API, you first need an AWS EC2 server and a Git repository to manage your code.
2.1 Launch an EC2 Instance
Configure Security Group
Create/Select an SSH Key Pair
2.2 Prepare Your Git Repository
Step 3: Connect to Your EC2 Instance
Once your instance is up, connect to it using your SSH key.
After creating the instance and pushing code to the repository, connect to the EC2 instance via SSH using the .pem file.
Step 4: Setting Up GitHub Actions
GitHub Actions will handle the CI/CD pipeline, automatically deploying your Node.js REST API whenever you push code to the repository.
4.1 Enable GitHub Actions
4.2 Add a Self-Hosted Runner
If you want the pipeline to run directly on your EC2 server:
Step 5: Environment Setup for GitHub Actions
To ensure your Node.js application works seamlessly during deployment, you need to manage environment variables securely.
In your local Node.js project, creates a .env file containing values want to add as github repo screat keys ;
5.2 Add GitHub Secrets
6. CI/CD Workflows
Create GitHub Actions workflows for CI/CD. Below is an example of a Node.js CI workflow.
Recommended by LinkedIn
name: Node.js CI/CD
on:
push:
branches: [ "main" ]
jobs:
build:
runs-on: self-hosted
strategy:
matrix:
node-version: [22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: |
touch .env
echo "${{ secrets.MONGO_DB_URL }}" > .env
Step 7: Installing Node.js and Nginx on Ubuntu
Before deploying your application, ensure your Ubuntu EC2 instance has both Node.js (to run the backend server) and Nginx (to serve as a reverse proxy and web server).
Why Node.js: Node.js is the runtime environment that allows you to execute JavaScript on the server. Your REST API needs Node.js to run its backend logic, handle HTTP requests, and communicate with the database
How to Install Node.js:
Run the following commands on your EC2 instance
sudo apt update
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
Why Nginx?
Nginx is a powerful, high-performance web server that enhances your API deployment by:
How to Install Nginx?
Run this command on your Ubuntu instance
sudo apt-get install -y nginx
Key Roles of Nginx in Your Setup
8. Setting Up Nginx as Reverse Proxy
To route incoming requests to your Node.js app running on port 5000, configure Nginx as a reverse proxy
sudo nano /etc/nginx/sites-available/default
2. Add this block inside the server {} section
location /api {
rewrite ^/api/(.*)$ /api/$1 break;
proxy_pass http://localhost:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
3. Save and exit the file.
4. Restart Nginx to apply the changes:
sudo systemctl restart nginx
9. Running Node.js App with PM2
PM2 is a Node.js process manager that keeps your app running continuously, even after crashes or restarts.
sudo npm install -g pm2
2. Start your server:
pm2 start server.js --name=apiServer
PM2 ensures your app keeps running in the background and restarts if it crashes.
10. Automatic Deployment with GitHub Actions
Your CI/CD pipeline is now fully automated. Every new push to the main branch will:
1. Trigger the Workflow
GitHub Actions automatically detects new code pushed to the main branch and starts the workflow.
2. CI Process (Continuous Integration)
2.1. Checkout Code Fetches the latest code using actions/checkout.
2.2. Setup Node.js Ensures the correct Node.js version is installed using actions/setup-node.
2.3. Install Dependencies Runs npm ci to install all required dependencies cleanly.
2.4. Run Tests (Optional) If included, tests like npm test are run to validate the changes.
3. CD Process (Continuous Deployment)
3.1. Deploy If everything passes, the workflow continues by:
By following these steps, you've successfully set up a full CI/CD pipeline for your Node.js application using GitHub Actions, EC2, Nginx, and PM2. This setup allows you to:
With this automation in place, you can now focus more on developing features, while your infrastructure takes care of deployment and uptime.
💡 Great insight
Insightful
Insightful