74

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 ec2-user@ec2-xx-xx-xx-x.eu-west-1.compute.amazonaws.com 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

Kawd
  • 4,122
  • 10
  • 37
  • 68
  • 1
    The answers here are basically out of date / wrong. You simply use pm2. https://stackoverflow.com/a/36907953/294884 perfectly explained, is dead easy – Fattie Sep 15 '19 at 16:13
  • Possible duplicate of [Amazon EC2 NodeJS server stops itself after 2 days even after using sudo nohup](https://stackoverflow.com/questions/36907766/amazon-ec2-nodejs-server-stops-itself-after-2-days-even-after-using-sudo-nohup) – Fattie Sep 15 '19 at 16:13

6 Answers6

105

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

starball
  • 20,030
  • 7
  • 43
  • 238
James Wilson
  • 1,174
  • 1
  • 9
  • 7
  • 1
    For test / dev purposes, this works like a charm for a NodeJS Express setup. So glad I don't have to keep my Putty terminal open anymore! On another note - A tip for the newbies (like myself): To shut the running process in your `screen` environment, press *CTRL+C* to stop it (if that doesn't work, you may need to find out the Process ID or PID and kill it via a new Putty / terminal running from the main environment), and finally type `exit` to close the screen environment and return to the main one. – chamberlainpi Dec 14 '16 at 21:43
  • 1
    What if I wanted to keep multiple things running such as a mongodb server, nodejs server, react server, etc? I believe screen -r would only take me to the the one that is initiated with `screen`. I am looking for something like `screen myScreenOne` , `screen myScreenTwo`, etc? Can we do this? – Singh Dec 25 '17 at 19:03
  • Is it safe to use this on a production environment? And is it suitable for java applications? – User-8017771 Jan 15 '18 at 06:49
  • @User-8017771 No, don't use in production. Most distro's have [systemd](https://unix.stackexchange.com/questions/320314/configure-java-daemon-with-systemd) available. – Matt Mar 30 '20 at 03:45
  • @Matt Why should I not use it in production? I am currently serving a React app from my node HTTP server. – Rishav May 26 '20 at 17:38
  • @Rishav What happens when the system restarts? What happens when the node process or screen crashes? What happens to the process logs? A service manager like systemd or workload manager like kubernetes handles these for you. – Matt May 27 '20 at 01:50
  • @Matt Thanks, I hadn't thought of this. But because I don't think I would need to log anything on a static website I should be fine I suppose. :) – Rishav May 27 '20 at 08:37
  • @Singh Hi from 2021, I'm sure you already got it, but yea it should prompt you with the list of detached screen when you reconnect, you simply need to do `screen -r [the name of the screen]` – Yoho Mar 21 '21 at 03:59
48

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.

Juan Camilo Mejia
  • 952
  • 3
  • 14
  • 28
14

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/

Joseph Juhnke
  • 854
  • 9
  • 9
3

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

Pritam Roy
  • 193
  • 2
  • 7
3

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

Thomas G Henry LLC
  • 10,887
  • 8
  • 30
  • 32
0

If you are using a Ubuntu EC2, better to use the following we have been using this for the past 6 years and have had no issues with this.

sudo npm i -g forever

Now start your main, example

forever start index.js
forever start src/server.js

To stop the server use the following command

forever stop index.js

To list multiple servers running forever

forever listall
Vinodh Ram
  • 709
  • 1
  • 9
  • 22