Use forever to implement self-starting Node. js projects on Linux
It is easy to manually run the Node project on a computer, node xx. javascript is done. If you want to run the Node project in the background, you cannot directly use the node command. However, it is easy to install the forever package. However, if a Node project is built on a remote server and cannot be started by itself, once the server is restarted, the project must be manually enabled by the Administrator remotely to run.
Can we solve the above problem by using forever and startup script? The answer is certainly yes, but it is a little troublesome, And the forever official website lacks detailed configuration documents. I also made some detours during configuration, as detailed below.
Note: The experiment environment in this article is Ubuntu Server 12.04 LTS x86_64. The configuration on CentOS is simpler.
First, I want to try at/etc/rc. add a forever start xxx statement to the local file. The result shows that Ubuntu (similar to other systems) is not a zombie. the main contradiction is that mongodb can run in this way, and forever won't work, in desperation, it is still from/etc/init. d.
First of all, I would like to thank the author of this article, http://cnodejs.org/topic/5059ce39fd37ea6b2f07e1a3, which is really a few valuable materials!
The premise is that the forever should be good first, and 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
If no error is prompted, it indicates that forever can be used. That is to say, the basic conditions for using forever to start a Node project in the background are ready, and the rest is to compile 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 # enter the startup script file LOG of your Node project. =/node. js/log # (optional) log file directory PID =/node. js/pid # required content, used to record the forever process number export PATH = $ PATH:/usr/local/bin # specify the Node executable program installation directory here, my options are/usr/local/binexport NODE_PATH = $ NODE_PATH: /usr/local/lib/node_modules # the path of the Node class library # You do not need to modify 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
It is recommended that you create a directory for the Node project in the root directory, such as/node. js, and set the permission to 754, which can avoid the trouble caused by some permission problems!
Because the Ubuntu Server system is used, the MongoDB startup service is configured above, and the following statements are added to its init. d script:
# Required-Start: $all# Required-Stop: $all
Therefore, the system will prompt an error when you add the Node in the future. Therefore, I added the preceding remarks to the startup script of the Node project to enable the Ubuntu Server system to be set, if it is on CentOS, there should be no similar issues, so pay special attention to this!
That is, the following description:
### 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 is edited, run the chkconfig -- list command to check whether the added services take effect. That is, 3 and 5 must be on before they can start automatically.
If 3 and 5 are not set to on, run chkconfig -- level 35 [your service name] on. Ubuntu Server may report some warnings, however, if you can change 3 and 5 of the services you want to set to on, other errors can be ignored (I think this is the system's own thing ).
After the configuration is complete, the Node project can be self-started on Linux. You can shutdown-r now to check whether the Node project can be self-started. After it is started, you can directly access the preset port number and virtual directory, if you want to come out, you will be done!
But if not, check some scripts and make relevant modifications based on the error. After all, I also tried it!
Original article address: http://blog.csdn.net/yuanguozhengjust/article/details/37512993. For more information, see the source!