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. Thank you
You could install forever using npm like this:
sudo npm install -g forever
And then start your application with:
forever server.js
Or as a service:
forever start server.js
Forever restarts your app when it crashes or stops for some reason. To restrict restarts to 5 you could use:
forever -m5 server.js
To list all running processes:
forever list
Note the integer in the brackets and use it as following to stop a process:
forever stop 0
Restarting a running process goes:
forever restart 0
If you're working on your application file, you can use the -w
parameter to restart automatically whenever your server.js
file changes:
forever -w server.js
I'd recommend looking for something such as Forever to restart Node in the event of a crash, and handle daemonizing this for you.
If you just want to run your node app in the terminal always, just use screen.
Install on ubuntu/ debian:
sudo apt-get install screen
Usage:
$ screen
$ node /path/to/app.js
ctrl + a
and then ctrl + d
to dismiss
To get is back:
One screen: screen -r
If there's more than one you can list all the screens with: screen -ls
And then: screen -r pid_number
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.
During development, I recommend you use nodemon. It will restart your server whenever a file changes. As others have pointed out, Forever is the most widely used solution in production.
Although the other answers solve the OP's problem, they are all overkill and do not explain why he or she is experiencing this issue.
The key is this line, "I close putty, then I cannot reach the address"
When you are logged into your remote host on Putty you have started an SSH linux process and all commands typed from that SSH session will be executed as children of said process.
Your problem is that when you close Putty you are exiting the SSH session which kills that process and any active child processes. When you close putty you inadvertently kill your server because you ran it in the foreground. To avoid this behavior run the server in the background by appending & to your command: node /srv/www/MyUserAccount/server/server.js &.
The problem here is a lack of linux knowledge and not a question about node. For some more info check out: http://linuxconfig.org/understanding-foreground-and-background-linux-processes