How to make a node.js application run permanently?

2020-01-23 04:58发布

On a Debian server, I installed Node.js. I understand how to launch an app from putty with this command line:

node /srv/www/MyUserAccount/server/server.js

and get to it on the address 50.51.52.53:8080 (IP and port).

But as soon as I close putty, then I cannot reach the address 50.51.52.53:8080 anymore.

How to make a Node.js application run permanently?

As you can guess, I am a beginner with Linux and Node.js.

17条回答
We Are One
2楼-- · 2020-01-23 04:59

You can use PM2, it's a production process manager for Node.js applications with a built-in load balancer.

Install PM2

$ npm install pm2 -g

Start an application

$ pm2 start app.js

If you using express then you can start your app like

pm2 start ./bin/www --name="app"

Listing all running processes:

$ pm2 list

It will list all process. You can then stop / restart your service by using ID or Name of the app with following command.

$ pm2 stop all                  
$ pm2 stop 0                    
$ pm2 restart all               

To display logs

$ pm2 logs ['all'|app_name|app_id]
查看更多
Juvenile、少年°
3楼-- · 2020-01-23 05:02

You could simply use this

nohup node /srv/www/MyUserAccount/server/server.js &

This will keep the application running and to shut it down you will have to kill it.

For that you could install htop and then search for node and then kill it

查看更多
戒情不戒烟
4楼-- · 2020-01-23 05:04

nohup working i checked in AWS Ubunto vm follow the correct syntax

ubuntu@ip-172-00-00-00:~/ms$ nohup node server.js &

then press enter you will see this line

ubuntu@ip-172-00-00-00:~/ms$ nohup: ignoring input and appending output to ‘nohup.out’

then type this

rm nohup.out

enter image description here

查看更多
老娘就宠你
5楼-- · 2020-01-23 05:05

I recommend use PM2, which is a process manager for Node.js applications. PM2 provides an easy way to manage and daemonize applications (run them as a service).

refer this link - https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-centos-7

查看更多
老娘就宠你
6楼-- · 2020-01-23 05:06

Here's an upstart solution I've been using for my personal projects:

Place it in /etc/init/node_app_daemon.conf:

description "Node.js Daemon"
author      "Adam Eberlin"

stop on shutdown

respawn
respawn limit 3 15

script
  export APP_HOME="/srv/www/MyUserAccount/server"
  cd $APP_HOME
  exec sudo -u user /usr/bin/node server.js
end script

This will also handle respawning your application in the event that it crashes. It will give up attempts to respawn your application if it crashes 3 or more times in less than 15 seconds.

查看更多
Summer. ? 凉城
7楼-- · 2020-01-23 05:06

I’ve found forever to do the job perfectly fine.

Assuming you already have npm installed, if not, just do

sudo apt-get install npm

Then install forever

npm install forever --global

Now you can run it like this

forever start app.js

https://codingweb.io/run-nodejs-application-background/

查看更多
登录 后发表回答