Use forever in Linux to implement the Node. js project self-start _ node. js

Source: Internet
Author: User
It is easy to manually run the Node project on a computer, nodexx. 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 on its own, it will be troublesome once the server is restarted. 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.
Run node js forever

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.

The premise is that the forever should be good first, and the method is very simple. Just execute the following command:

N o de forever

npm install forever -g


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

H ow to run node server forever

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.