Deploying MERN app on AWS with NGINX and PM2

Posted on Dec 25, 2020

ssh into the server:

Updating the system

sudo apt update -y

sudo apt upgrade -y


sudo apt install git wget

NodeJS and NPM via NVM

curl -o- | bash

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"

[ -s "$NVM_DIR/" ] && \. "$NVM_DIR/" # This loads nvm

nvm install node

node --version && npm --version


sudo apt install nginx -y

sudo ufw app list

sudo ufw enable

sudo ufw status

sudo ufw allow 'Nginx Full'

sudo ufw allow 'OpenSSH'


wget -qO - | sudo apt-key add -

echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

sudo apt update -y

sudo apt install mongodb-org

sudo systemctl enable mongod

sudo systemctl status mongod

Setting up our MERN application

cd ~

git clone<username>/<repo>.git

cd <app_name>

npm i

Configuring NGINX

cd /etc/nginx/sites-available/

sudo vim /etc/nginx/sites-available/

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

sudo systemctl reload nginx

sudo systemctl restart nginx

sudo systemctl status nginx

Configuring the MERN application

cd <app_name>

node <server_file>.js

## SSL using certbot

sudo apt install certbot python3-certbot-nginx -y

sudo certbot --nginx -d -d

sudo certbot renew --dry-run

Using PM2 to run multiple cluster in production mode