Home Deploying MERN app on AWS with NGINX and PM2
Post
Cancel

Deploying MERN app on AWS with NGINX and PM2

ssh into the server:

1
2
3
4
5
Updating the system

sudo apt update -y

sudo apt upgrade -y

Dependencies

1
2
3
4
5
6
7
8
9
10
11
12
13
sudo apt install git wget

NodeJS and NPM via NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

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

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

nvm install node

node --version && npm --version

Nginx

1
2
3
4
5
6
7
8
9
10
11
sudo apt install nginx -y

sudo ufw app list

sudo ufw enable

sudo ufw status

sudo ufw allow 'Nginx Full'

sudo ufw allow 'OpenSSH'

MongoDB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu 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 https://github.com/<username>/<repo>.git

cd <app_name>

npm i

Configuring NGINX

1
2
3
4
5
6
7
8
9
10
11
cd /etc/nginx/sites-available/

sudo vim /etc/nginx/sites-available/domain.com

sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/

sudo systemctl reload nginx

sudo systemctl restart nginx

sudo systemctl status nginx

Configuring the MERN application

1
2
3
4
5
6
7
8
9
10
11
cd <app_name>

node <server_file>.js

## SSL using certbot

sudo apt install certbot python3-certbot-nginx -y

sudo certbot --nginx -d example.com -d www.example.com

sudo certbot renew --dry-run

Using PM2 to run multiple cluster in production mode

##

This post is licensed under CC BY 4.0 by the author.

Any history buffs here? Remembering Norm Macdonald

Generating sitemap with custom priority in Jekyll site