Implement node. js project startup on Linux with forever

  • 2020-03-30 03:32:32
  • OfStack

So can you use forever plus startup scripts to solve the above problems? The answer, of course, is yes, but it's a bit of a hassle, and forever's official configuration documentation is lacking. I also took some detours in the configuration, as detailed below.

Note: the experimental environment for this article is Ubuntu Server 12.04 LTS x86_64, which is simpler on CentOS

At first, I thought about adding a forever start XXX to /etc/rc.local, but it turned out that Ubuntu (like other systems) didn't work for me. The main contradiction was that mongodb could run in this way, and forever couldn't.

The premise is to put forever good, the method is very simple, execute the following command:


npm install forever -g

Once the installation is complete, test it out with a simple Node program:


forever start test.js
forever stop test.js
forever restart test.js

As long as you don't prompt an error, forever is available, which means that the basic requirements for forever to start a Node project in the background are in place, and all that's left is to write a startup script.

The basic content of the script is as follows, thanks to the original author's hard work:


#!/bin/bash
### BEGIN INIT INFO
# Provides:  xiyoulib
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop:  0 1 6
# Short-Description: Start daemon at boot time
# Description:  Enable service provided by daemon.
### END INIT INFO
# chkconfig: 345 88 08
# description: Forever for Node.js

DEAMON=/node.js/XiyouLibNodeExpress/bin/www  # You need to fill in your own here Node The startup script file for the project 
LOG=/node.js/log/log # Optional, log file directory 
PID=/node.js/pid # Required content for recording forever The process of no. 

export PATH=$PATH:/usr/local/bin # Let me specify it here Node The executable installation directory of mine is /usr/local/bin
export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules # Here is the Node The path to the class library 

# I don't need to change the following 

node=node
forever=forever

case "$1" in
 start)
  $forever start -l $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 --pidFile $PID -a $DEAMON
  ;;
 list)
  $forever list
  ;;
 *)
  echo "Usage: /etc.init.d/node {start|stop|restart|reload|stopall|restartall|list}"
  exit 1
  ;;
esac

A word of caution here: it is best to create a separate directory in the root directory for the Node project, such as /node.js, and then set the permissions to 754 to avoid some permissions problems!

Since you are using the Ubuntu Server system, MongoDB startup service is also configured, and the following statement is added to its init. D script:


# Required-Start:  $all
# Required-Stop:   $all

Therefore, the system will prompt an error when I add it later, so I added the previous series of explanatory comments in the startup script of the Node project, so that the Ubuntu Server system can be set up. If it is on CentOS, similar problems should not occur, which should be paid special attention to!

Namely, the following explanatory information:


### BEGIN INIT INFO
# Provides:     xiyoulib
# Required-Start:  $all
# Required-Stop:   $all
# Default-Start:   2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: Start daemon at boot time
# Description:    Enable service provided by daemon.
### END INIT INFO

After the script editing is completed, use the chkconfig --list directive to check whether the service you added is effective or not, that is, 3 and 5 all need to be on to achieve boot from the start.

If 3 and 5 are not set to on, then please execute chkconfig --level 35 [your service name] on. Ubuntu Server may give some warnings, but as long as you can change 3 and 5 of the service you want to set to on, other errors can be ignored (I feel it is the system's business).

After setting up, the Node project can be started on Linux. You can try shutdown -r now to see if it can be started by itself. After starting up, you can directly access the port number and virtual directory you set.

However, if it is not correct, check some scripts, and then make relevant changes according to the error report, after all, I also tried out!


Related articles: