Explore Courses Blog Tutorials Interview Questions
0 votes
in AWS by (19.1k points)

As you can tell by my question, I'm new to this...

I built my first website, I set up my first Node.js server to serve it and then pushed everything to live on EC2.

I tested everything on my EC2 IP address and everything seems to be working.

Now up until now, I've been testing my app locally so it makes sense that whenever I closed the terminal, app.js would stop running so nothing would be served on localhost.

Now that my server is on EC2, the same thing happens ("obviously" one could say..) whenever I close my terminal.

So my question is how do I keep my Node.js server running on EC2 for like... that my site stays live forever?

I read something about a node module called "forever" but I'm wondering (being new and all..) why isn't this "forever" functionality a default setting of the Node.js-EC2 system?

I mean, correct me if I'm wrong, but isn't the whole point of setting up a web server and pushing it live to have it stay live forever? Isn't that what servers are supposed to do anyway (infinitely listening for requests)? And if that's the case why do we need extra modules/settings to achieve that?

 After installing my app on EC2 these are the steps that I follow on the terminal (The app is running on Amazon Linux by the way) :

  • I type ssh -i xxxxxxxxxxx.pem [email protected] on the terminal
  • After logging onto the Amazon machine I then go to the relevant folder and execute node app.js
  • There are 3 folders in the machine: node, node_modules and *name of my app*
  • app.js resides in *name of my app*
  • After that, the site goes live on my EC2 IP
  • Once I close the terminal, everything is switched off

1 Answer

0 votes
by (44.4k points)

Before you invoke Node.js, run the command:


This will produce a persistent environment which is able to permit your process to keep running once you disconnect.

When you reconnect, you can use this command to reconnect to that environment:

screen -r

Here's a random link to learn more about screen:

However, this will not assist you if your EC2 instance restarts. There are many different ways to do that. Adding your startup command to /etc/rc.local is one way. Here's a link to an Amazon guide which has added something to /etc/rc.local.

Related questions

0 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer

Browse Categories