How do I leave Node.js server on EC2 running forever?

node.jsAmazon Ec2Webserver

node.js Problem Overview


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 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... forever..so 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 ?

Thanks for your help.. As you can tell I'm not only looking for a solution but an explanation as well because I got really confused.. :-)

EDIT (a few details you might need) - 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

node.js Solutions


Solution 1 - node.js

Before you invoke Node.js, run the command:

screen

This will create a persistent environment which will allow your process to keep running after 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:

http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/

However, this won't help 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 includes adding something to /etc/rc.local.

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/building-shared-amis.html

Hope this helps,

James

Solution 2 - node.js

I worked with the valid answer for a while but some times the screen just end with no reason also screen has no balance loader and others features that in a production enviroment you should care , Currently I use a npm component to do this job.

https://www.npmjs.com/package/pm2

This is so easy to use.

$ npm install pm2 -g

then just start your app with pm2 like this

$ pm2 start app.js

In the above link you can find diferents tasks to perform if you need.

Hope this help the newbies like me.

Solution 3 - node.js

There's a better way. Use forever.js.

See it here: https://github.com/foreverjs/forever

This is a nice tutorial for how to use chkconfig with forever on CENTOS.

http://aronduby.com/starting-node-forever-scripts-at-boot-w-centos/

Solution 4 - node.js

Or use tmux Just Enter a tmux screen run node server Ctrl+b Hit D and you're done.

Solution 5 - node.js

I am very late to join the thread and seems its basic problem with every newbie. Follow the below to setup properly your first server.

follow the step on the ec2 instance(before doing this make sure you have a start script for pm2 in your package.json file):

npm install pm2 -g

pm2 startup systemd

See the output and at the last line it must be like..

> You have to run this command as root. Execute the following command: > sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup > systemd -u sammy --hp /home/sammy

Take the last line command and run again with root privilege.

(before running the next command, Provide a new start script for pm2 in your package.json file e.g: "pm2-start": "pm2 start ./bin/www")

npm run pm2-start

for more info follow the link.

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSprout CoderView Question on Stackoverflow
Solution 1 - node.jsJames WilsonView Answer on Stackoverflow
Solution 2 - node.jsJuan Camilo MejiaView Answer on Stackoverflow
Solution 3 - node.jsJoseph JuhnkeView Answer on Stackoverflow
Solution 4 - node.jsPritam RoyView Answer on Stackoverflow
Solution 5 - node.jsRitesh kumar AgrahariView Answer on Stackoverflow