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.
You can use PM2, it's a production process manager for Node.js applications with a built-in load balancer.
Install PM2
Start an application
If you using express then you can start your app like
Listing all running processes:
It will list all process. You can then stop / restart your service by using ID or Name of the app with following command.
To display logs
You could simply use this
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 itI 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
Here's an upstart solution I've been using for my personal projects:
Place it in
/etc/init/node_app_daemon.conf
: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.
I’ve found forever to do the job perfectly fine.
Assuming you already have npm installed, if not, just do
Then install forever
Now you can run it like this
https://codingweb.io/run-nodejs-application-background/