Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AWS

Install Node on EC2 and keep it running

This is the most straightforward way -- install node in the virtual server and simply keep it running. Good for testing, prototyping, etc. Obviously not for production.

First create a new EC2 instance (Amazon Linux AMI). SSH into it. Then install Node, install MongoDB or other database you'll be using, git clone your project repo, install dependencies, run the app, and finally make sure app will be running even if you close the terminal.

Install Node

You can follow the steps here (using NVM to install node): https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html

Or simply use yum to install node, as below.

Prerequisites: SSH into the EC2 instance.

On the terminal, update packages on the server

sudo yum update -y

Get node into yum

curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -

Install node and npm with yum

yum -y install nodejs

Check node is installed

node --version

Install Database server

Check other boilerplate for MongoDB installation on AWS EC2 Linux AMI.

Clone project repo and start app (run server on background)

First install git

npm install git

Or use

sudo apt-get install git

Verify git is installed

git --version

Clone the repo

git clone <Link to the repo>

cd into the repo

cd <repo folder>

Install packages

npm install

Run app in background (so you can exit the terminal without closing the server). On the terminal, open a new screen by typing

screen

You'll be prompt to a new screen. Start the server there.

node app.js

Close the screen, exit the terminal, and the server will still be running. To verify, SSH into instance again, and type the following in the command line

screen -r

The screen window will be prompted and you'll see app running (with any console.log that was triggered). This way, you don't need to keep your EC2 instance terminal open to keep the server running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment