Using forever to implement Node.js project on Linux

  

Running Node project manually on a computer is simple, node xx.js is done, and you want the Node project to run in the background, although you can't directly use the node command. But after installing the forever package, it is still very easy. However, if you build a Node project on a remote server, if you can't start it yourself, once the server is restarted, the project must be manually opened by the administrator to run.

So can you use the forever plus startup script to solve the above problem? The answer is of course affirmative, but it is a bit of a hassle, and the forever official lacks detailed configuration documentation. I also took some detours when configuring, as detailed below.

Note: The experimental environment of this article is Ubuntu Server 12.04 LTS x86_64, the configuration on CentOS is simpler

The earliest, I want to try adding a sentence in /etc/rc.local Forever start xxx look, the results found that Ubuntu (other systems are the same) is not a bird, the main contradiction is that mongodb can run this way, forever will not work, but in desperation, from the perspective of /etc/init.d Let's think about it.

First of all, thanks to this article and its author, http://cnodejs.org/topic/5059ce39fd37ea6b2f07e1a3, it is really one of the few precious materials!

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

npm install forever -g

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

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

As long as you don't prompt error, it means that forever is usable, which means using forever to open a Node project in the background. Already available, the rest is to write a startup script.

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

#!/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.jsDEAMON=/node.js/XiyouLibNodeExpress/bin/www # Here you need to fill in your own Node project startup script file LOG=/node.js/log/log #optional, log File directory PID=/node.js/pid #Required content, used to record the process number of forever export PATH=$PATH:/usr/local/bin # Specify the Node executable directory installation directory here, mine is /usr/local/binexport NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules #here is the path of the Node class library #The following content does not need to be modified node=nodeforever=forevercase "$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

Here's a reminder: It's best to create a directory for the Node project in the root directory, such as /node.js, and then set the permissions to 754, which avoids some permissions issues. The trouble caused!

Since the Ubuntu Server system is used, the MongoDB startup service is also configured, and the following statement is added to the script of its init.d: ​​

# Required-Start: $ All# Required-Stop: $all, so the system will prompt an error when adding it later, so in the startup script of the Node project, I added a previous comment to the Ubuntu Server system. If it is on CentOS, There should be no similar problems, so pay special attention to this! 

This is the following 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 editing the script, use the chkconfig --list command to check whether the service you added is valid. That is, all of 3 and 5 must be on to enable booting. 

If 3 and 5 are not set to on, then execute chkconfig --level 35 [your service name] on, Ubuntu Server may report some warning, but as long as you can set the required service 3 , 5 becomes on, other errors can be ignored (I feel this is the system's own business).

After the setting is completed, you can realize the self-starting of the Node project on Linux. You can use shutdown -r now to try to start it automatically. After starting it, you can directly access the port number and virtual directory you set. Ma, if you want to come out, you're done!

But if it is not right, just check some scripts, and then make related changes according to the error. After all, I also tried it out!

Original address: http://blog.csdn.net/yuanguozhengjust/article/details/37512993, please indicate the source!

Copyright © Windows knowledge All Rights Reserved