Skip links

Deploy a mern stack application with AWS EC2

Create a new Ubuntu Server on AWS EC2

1. have an AWS account

2. head to

3. start instance

  • choose ubuntu “Ubuntu Server 18.04” Amazon Machine Image (AMI) (use se)
  • check data transfer via http and https

4. launch

5. get key pair

6. make sure key is not publicly visible

chmod 400 C:/BlogAWSKey.pem

7. ssh into server

ssh -i C:/BlogAWSKey.pem
ssh -i C:/BlogAWSKey.pem ubuntu@
ssh -i C:/BlogAWSKey.pem

You are now accessing the bash of the server and are able to do stuff

Server Setup Script (Node.js + MongoDB + NGINX)

raw/e3047c9dc3ce8b796e7354c92d2c47ce61981d2f/ | sudo bash
#!/usr/bin/env bash
echo NODE & NPM
### add nodejs 10 ppa (personal package archive) from nodesource
curl -sL | sudo -E bash -
### install nodejs and npm
sudo apt-get install -y nodejs
sudo apt install npm
### import mongodb 4.0 public gpg key
sudo apt-key adv --keyserver hkp:// --recv
### create the /etc/apt/sources.list.d/mongodb-org-4.0.list file for mongodb
echo "deb [ arch=amd64 ] bionic/mongodborg/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
### reload local package database
sudo apt-get update
### install the latest version of mongodb
sudo apt-get install -y mongodb-org
### start mongodb
sudo systemctl start mongod
### set mongodb to start automatically on system startup
sudo systemctl enable mongod
echo PM2
### install pm2 with npm
sudo npm install -g pm2
### set pm2 to start automatically on system startup
sudo pm2 startup systemd
echo NGINX
### install nginx
sudo apt-get install -y nginx
### allow ssh connections through firewall
sudo ufw allow OpenSSH
### allow http & https through firewall
sudo ufw allow 'Nginx Full'
### enable firewall
sudo ufw --force enable

Privates Repo auf den Server clonen

1. einen SSH key generieren:

  • Hier generieren
  • z.B.: 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

2. git clone

3. Beispiel: in dem EC2 terminal

sudo git clone 

(hat tatsächlich funktioniert)

sudo git pull 
git config --global --add /home/ubuntu/KaiserFranzBlog

aber wenn man gecloned hat, dann kann man auch geht auch nur git pull. —————————————

installing packages for Frontend and Backend

use la ls to list files and directories use

sudo npm install 

in server and backends to install packages for front and backend.

ls la 
cd KaiserFranzBlog
sudo npm install
cd client 
sudo npm install
sudo npm run build

if the error occurs that npm is not yet installed use the following command to install npm. enter:

sudo apt install npm

error: This is probably not a problem with npm. There is likely additional logging output above.

sudo npm cache clean --force
sudo npm install
echo 65536 | sudo tee -a /proc/sys/fs/inotify/max_user_watches

retry installing packages.

Setting up NGINX to serve the site

sudo apt install nginx

The website should be served under the public IPv4-DNS (e.g.:


might need a few minutes to load the installation onto the web output.

Browser EC2 times out … rejects connection with SSH

Looks like this:

disable firewall

sudo iptables -F
sudo service sshd restart

SSH port correct?

verify that the server is listening to the correct ssh-port (22)

sudo netstat -tnlp | grep :22
tcp 0 0* LISTEN 849/sshd
tcp6 0 0 :::22 :::* LISTEN 849/sshd

Installing MongoDB

The materials in this section are taken from the guide Install MongoDB Community Edition on Ubuntu

import public key




wget -qO - | sudo apt-key add -

should respond with:
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)). OK
See if it worked:

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

Create a MongoDB list file

sudo touch /etc/apt/sources.list.d/mongodb-org-5.0.list

Reload package Database

sudo apt-get update

Install MongoDB packages

sudo apt-get install -y mongodb-org

Problem Fix:

For: mongodb-org-mongos : Depends: libssl1.1 (>= 1.1.0) but it is not installable
MongoDb has no official build for ubuntu 22.04 at the moment. Ubuntu 22.04 has upgraded libssl to 3, and
does not propose libssl1.1 You can force the installation of libssl1.1 by adding the ubuntu 21.10 source :

echo "deb impish-security main" | sudo tee
sudo apt-get update
sudo apt-get install libssl1.1

Most probably unit mongodb.service is masked. Use following command to unmask it.

sudo systemctl unmask mongod
sudo systemctl daemon-reload
sudo systemctl start mongod
sudo systemctl status mongod
sudo systemctl enable mongod
sudo service mongod stop
sudo apt-get purge mongodb-org*

Remove Data Directories. Remove MongoDB databases and log files.

Using pm2 to open deploy server

echo PM2
### install pm2 with npm
sudo npm install -g pm2
### set pm2 to start automatically on system startup
sudo pm2 startup systemd
sudo pm2 start server.js
netstat -ao 

Curl Server

curl localhost:4000/api/article
curl localhost:4000/api/article/test
curl http://138.68.244.XXX:4000/article/test

If you get a useful response, pm2 works with your server

Verified by MonsterInsights