How to deploy NodeJS API on AWS EC2
Beginner steps to deploy NodeJS API on AWS EC2 instance
In this post we will create an EC2 (Elastic Compute Cloud) instance with Ubuntu AMI (Amazon Machine Image) and then deploy a NodeJS/ExpressJS project from GitHub repository. We will use PM2 for running the project and Nginx as a reverse proxy.
Prerequisites
AWS account
Github account
Launch EC2 Instance
Steps to follow:
Launch EC2 instance with Ubuntu AMI.
Select a Key Pair or generate a new one for ssh access.
Create a Security Group or select an existing one. You must allow ssh and http traffic.
Under the Advanced Details > User Data, add scripts for installing Nginx.
#!/bin/bash
# Update the package list
sudo apt update
sudo apt upgrade -y
# Install nginx
sudo apt install nginx -y
# Start and enable nginx
sudo systemctl start nginx
sudo systemctl enable nginx
Once the instance goes to running state, copy the public IPV4 address and open in a browser tab. You should be able to see default Nginx page.
Connect to EC2 Instance
You can connect to EC2 instance either using ssh
or EC2 instance connect (3 in the image above). For simplicity, I recommend using EC2 instance connect. Click connect button in the next page.
Install Node.js and PM2
Run these command to install Node.js and PM2.
# Install Node.js and npm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts
# Install pm2 globally
npm i pm2 -g
Clone Project from GitHub
If you clone any public repository then you can simply clone using https web URL without any issue.
git clone https://github.com/<username>/<project>.git
If your repository is private, then you need to use username and password for authentication. However, Github do not support password based authentication. Therefore you will need to create a personal access token.
Follow this link to learn how to create github personal access token.
Once you create your token, you can use that token instead of your password.
Run The Project
Now move to the project folder, install dependencies, build the project, and run the project using PM2.
cd your-project
# Install dependencies using ci command
npm ci
# Optional build step
npm run build
# Run project using pm2
pm2 start dist/index.js --name my-nodejs-api
pm2
command will run your project at the specified port for example 8080. Though the API is running at 8080 port, we can not use it yet. Because, our security group only allows requests at port 80 for http and 22 for ssh. Therefore, we update our Nginx configuration to forward all the requests at port 80 to port 8080.
Configure NGINX
Move to the nginx sites-available
directory. Then duplicate the default
file and give a custom name. This custom file will be the configuration file for our nodejs API.
cd /etc/nginx/sites-available
# Copy default file and name it my-nodejs-api.conf
sudo cp default my-nodejs-api.conf
# Open configuration file for editing
sudo nano my-nodejs-api.conf
Replace the location
configuration with the following one and save the file.
location / {
proxy_pass http://localhost:8080;
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;
}
Now enable this configuration and disable the default configuration using the following commands.
# Disable default config
sudo unlink /etc/nginx/sites-enabled/default
# Enable our custom config
sudo ln -s /etc/nginx/sites-available/my-nodejs-api.conf /etc/nginx/sites-enabled/my-nodejs-api.conf
Now test the nginx configuration using the following command and look for the success message.
sudo nginx -t
If the configuration test passed then restart the nginx to start with the updated configuration.
sudo service nginx restart
Now test your API endpoint at the public IPV4 address using curl or postman.
Congratulations, you have successfully deployed your Node.js API in the EC2 instance.
Deploy New Builds
After pushing new codes into github repository, you have to connect to your EC2 instance either by using EC2 Connect or ssh. Then navigate to your project directory on the server and run below commands.
# Pull new codes from github (use your username and access token)
git pull
# Install dependencies
npm ci
# Optional build step
npm run build
# Restart your project with new build
pm2 restart my-nodejs-api
Conclusion
We have successfully deployed a NodeJS API project to AWS EC2 instance.
This deployment process is fully manual. In the next article of this series I will show how to setup CI/CD using Github Actions.