It is impossible for us to manage remote sites directly through node commands, which cannot ensure the sustainable operation of the website. We use Forever to solve this problem. It can run the NodeJS application in the background daemon, and we can also set the NodeJS application to run automatically as the system starts.
First, install Forever:
The code copy is as follows:
npm install forever -gd
In this way, Forever is installed, we can directly run the Forever command:
The code copy is as follows:
forever --help
forever start app.js
forever stop app.js
The above command first checks the Forever help file, then run app.js, and then stop app.js. We want Forever to run automatically, first create a file node in the /etc/init.d directory, with the following content:
The code copy is as follows:
#!/bin/bash
#
# node Start up node server daemon
#
# chkconfig: 345 85 15
# description: Forever for Node.js
#
PATH=/home/node/0.8.9/bin
DEAMON=/home/ftp/1520/weizt-20120918-tKx/weizt.com/app.js
LOG=/home/hosts_log
PID=/tmp/forever.pid
case "$1" in
start)
forever start -l $LOG/forever.log -o $LOG/forever_out.log -e $LOG/forever_err.log --pidFile $PID -a $DEAMON
Stop)
forever stop --pidFile $PID $DEAMON
stopall)
forever stopall --pidFile $PID
restartall)
forever restartall --pidFile $PID
reload|restart)
forever restart -l $LOG/forever.log -o $LOG/forever_out.log -e $LOG/forever_err.log --pidFile $PID -a $DEAMON
list)
forever list
*)
echo "Usage: /etc.init.d/node {start|stop|restart|reload|stopall|restartall|list}"
exit 1
esac
exit 0
The above code is my configuration on the local virtual machine. I modify the relevant parameters according to the actual situation. It is mainly the path parameters of DEAMON, give the file executable permissions, and run chkconfig to add automatic running:
The code copy is as follows:
chmod 755 /etc/init.d/node
chkconfig /etc/init.d/node on
Reboot restarts the system, enters the website through the browser and finds that NodeJS can be run automatically. The rest is to study NodeJS, Express and AngularJS well and make an application that truly belongs to you!