Using forever to implement Node.js projects from Linux _node.js

Source: Internet
Author: User
Tags mongodb centos port number

Can you solve the problem with forever and startup scripts? The answer, of course, is yes, it's just a bit of a hassle and forever the official lack of detailed configuration documentation. I also took some detours in the configuration, below in detail.

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

The earliest, I think try in/etc/rc.local add a forever start xxx see, the results found Ubuntu (other systems) do not bird me, the main contradiction is MongoDB use this way can run up, forever not, helpless, Let's think about it from a/etc/init.d point of view.

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

NPM Install Forever-g

Once the installation is complete, test with a simple node program:

Forever Start test.js
forever stop test.js
Forever restart Test.js

As long as the error is not prompted, it means that forever is available, that is, the basic conditions for opening a node project with forever are already in place, and the rest 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 the boot time # Description:enable service provided by daemon
.  ### End INIT INFO # chkconfig:345-Description:forever for Node.js deamon=/node.js/xiyoulibnodeexpress/bin/www #这里需要填写你自己的Node项目的启动脚本文件 log=/node.js/log/log #可选, the log file directory Pid=/node.js/pid #必填内容, which is used to record the forever process number export path= $PATH:/ Usr/local/bin #在这里指定一下Node的可执行程序安装目录, mine is/usr/local/bin export node_path= $NODE _path:/usr/local/lib/node_modules This is the path to the Node class library #往下的内容就不用修改了 Node=node forever=forever case "in Start" $forever start-l $LOG--pidfile $PID-a $DE
 AMON;;
 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 "Us"Age:/etc.init.d/node {start|stop|restart|reload|stopall|restartall|list} "exit 1;;

 Esac

Here's a reminder: it's a good idea to create a separate directory for the node project in the root directory, such as/node.js, and then set the permissions to 754, which can avoid some permissions problems!

With the Ubuntu server system, the MongoDB boot service is configured and the following statement is added to its INIT.D script:

# Required-start:  $all
# required-stop:   $all

So in the future when you add the system will be prompted to error, so in the node project startup script I added a previous set of explanatory comments, so that the Ubuntu server system settings, if on the CentOS, should not have similar problems, this should pay special attention to!

The following descriptive information is:

### 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 edits are complete, use the chkconfig--list directive to see if the service you added is in effect, that is, 3 and 5 are all on to be on to start from boot.

If 3 and 5 are not placed on, then perform chkconfig--level 35 [your service name] on, and Ubuntu server may report some warning, but as long as you can change 3 or 5 of the service you need to set to ON, Other mistakes can be ignored (I feel like this is the system itself).

Settings can be completed after the node project on the Linux self-boot, you can shutdown-r now try to start, after the launch of a direct visit to your set of port number, virtual directory God horse, if you want to do it!

But if not, just check some of the script, and then according to the error related changes, after all, I also tried out!

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.