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 https://eu-central-1.console.aws.amazon.com/ec2

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 ubuntu@ec2-3-74-167-16.eu-central-1.compute.amazonaws.com
ssh -i C:/BlogAWSKey.pem ubuntu@
ssh -i C:/BlogAWSKey.pem ubuntu@ec2-3-74-43-69.eu-central-1.compute.amazonaws.com

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

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

curl
https://gist.githubusercontent.com/cornflourblue/f0abd30f47d96d6ff127fe8a9e5bbd9f/
raw/e3047c9dc3ce8b796e7354c92d2c47ce61981d2f/setup-nodejs-mongodb-productionserver-on-ubuntu-1804.sh | sudo bash
#!/usr/bin/env bash
echo NODE & NPM
### add nodejs 10 ppa (personal package archive) from nodesource
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
### install nodejs and npm
sudo apt-get install -y nodejs
sudo apt install npm
echo MONGODB
### import mongodb 4.0 public gpg key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv
9DA31620334BD75D9DCB49F368818C72E52529D4
### create the /etc/apt/sources.list.d/mongodb-org-4.0.list file for mongodb
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu 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
echo UFW (FIREWALL)
### 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 https://github.com/settings/tokens
  • z.B.: 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

2. git clone https://oauth-key-goes-here@github.com/username/repo.git

3. Beispiel: in dem EC2 terminal

sudo git clone https://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@github.com/KFEngineering/KaiserFranzBlog.git 

(hat tatsächlich funktioniert)

sudo git pull https://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@github.com/KFEngineering/KaiserFranzBlog.git 
git config --global --add safe.directory /home/ubuntu/KaiserFranzBlog

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

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.: ec2-18-193-128-45.eu-central

  1. compute.amazonaws.com)

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 0.0.0.0:22 0.0.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

————————————————————————————————-

—–Done—————

execute

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | 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 ] https://repo.mongodb.org/apt/ubuntu 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 http://security.ubuntu.com/ubuntu impish-security main" | sudo tee
/etc/apt/sources.list.d/impish-security.list
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