AWS Deployment

Shamelessly taken from Eric Pearson @nyloneric, as I haven't documented my own process yet.

Getting Started

Start an EC2 instance with Amazon Linux and t2 micro. Set up security policy on AWS EC2 I started with everything open and then restricted to port 80 UDP/TCP traffic for incomming. I also opened ICMP traffic for pinging purposes as well as ssh port 22 for logging into server.

Set up elastic IP so it will persist after reboot/termination.

Logging in

Each Linux instance launches with a default Linux system user account. The default user name is determined by the AMI that was specified when you launched the instance.

Start up EC2[1] and SSH from local: set permissions to PEM file: chmod 400 hr-nylon-eric.pem

ssh -i hr-nylon-eric.pem

Then navigate to and create folder for project:

git clone
sudo yum install git

Setting up Node[7]

Install NVM to handle versioning in Node.

curl -o- | bash

At this point, log out of the EC2 instance and back in to continue.

# install node
nvm install node
# initiate nvm with new node version
. ~/.nvm/
# ensure desired version of node is running
node -e "console.log('Running Node.js ' + process.version)"
# Install NPX if you need it
npm install -g npx

Setup Database (MongoDB)

# Install Mongo using their package management system:
wget -qO - | sudo apt-key add -
# Create a list file for MongoDB
echo "deb [ arch=amd64,arm64 ] focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
# Reload local package database
sudo apt-get update
# Install MongoDB packages
sudo apt-get install -y mongodb-org

Now the DB and software should be installed, so now we can start it up.

sudo systemctl start mongod
# Verify it is running
sudo systemctl status mongod
# Stop/restart
sudo systemctl stop mongod
sudo systemctl restart mongod
# Use the shell to test install worked correctly

Copy any DB data to instance. You can use scp[9], or something like Filezilla[8] or Cyberduck. scp looks like this:

scp -i /directory/to/abc.pem /your/local/directory/files/to/download

Router/Reverse Proxy:

sudo amazon-linux-extras install nginx1
sudo service nginx start
# message: Redirecting to /bin/systemctl start nginx.service
cat /etc/nginx/sites-available/default

This didn't work and I had to go find the nginx.conf file and edit it with nano: sudo nano /etc/nginx/nginx.conf. Most of this config was filled out, Just needed to add the location block and change to port 1234 for our server

server {
    listen 80;
    server_name tutorial;
    location / {
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  Host       $http_host;

Link config files

sudo service nginx restart

Set Up App

Use NPM to install all project dependencies necessary. NPM start in project folder, needed to install nodemon

Keep The App Running

npm i -g pm2
pm2 start tutorial/index.js

To make sure that your PM2 restarts when your server restarts pm2 startup

You will be given a custom string similar to this but specific to your machine, copy that:

sudo env PATH=$PATH:/home/ec2-user/.nvm/versions/node/v16.4.0/bin
startup systemd -u ec2-user --hp /home/ec2-user

Save config with everything running (no nginx at this point) pm2 save

pm2 start nodemon server/server.js

That’s it! You can log out/in to SSH, even restart your server and it will continue to run on port 80.

Make NGINX Persistent

systemctl start nginx
systemctl enable nginx

Having trouble restarting nginx on reboot, sudo systemctl enable nginx is supposed to solve it. needed to start nginx/make persistant and THEN redo the pm process

Run It All

sudo systemctl start mongod
sudo service nginx start



Last modified: 202212070107