Listening on port 80 w/o root正在侦听端口80(不带根)

It’s a general rule that you should not run node as root. 一般规则是,不应以root用户身份运行节点。However only root can bind to ports less than 1024. 但是,只有root用户可以绑定到小于1024的端口。This is where authbind comes in. 这就是authbind的用武之地。Authbind allows non-root users to bind to ports less than 1024. Authbind允许非根用户绑定到小于1024的端口。Replace %user% with the user that will be running pm2.%user%替换为将运行pm2的用户。

sudo apt-get install authbind
sudo touch /etc/authbind/byport/80
sudo chown %user% /etc/authbind/byport/80
sudo chmod 755 /etc/authbind/byport/80

You should also add an alias to the user that runs pm2 profile, e.g. ~/.bashrc or ~/.zshrc (note you will need to run source ~/.bashrc or source ~/.zshrc immediately after):您还应该为运行pm2配置文件的用户添加别名,例如~/.bashrc~/.zshrc(注意,您需要在之后立即运行source ~/.bashrcsource ~/.zshrc):

+alias pm2='authbind --deep pm2'

Finally ensure that pm2 is updated with authbind:最后,确保使用authbind更新pm2

authbind --deep pm2 update

Or simply pm2 update if you added the alias to your user’s profile.或者,如果将别名添加到用户的配置文件中,则只需进行pm2 update

Now you can start applications using PM2 that can bind to port 80 without being root!现在,您可以使用PM2启动应用程序,PM2可以绑定到端口80,而无需成为root用户!

Multiple PM2 on the same server同一服务器上有多个PM2

The client and daemon communicate via socket files available in $HOME/.pm2/pub.sock and $HOME/.pm2/rpc.sock.客户端和守护进程通过$HOME/.pm2/pub.sock$HOME/.pm2/rpc.sock中提供的套接字文件进行通信。

You can start multiple PM2 instances by changing the PM2_HOME environment variable.通过更改PM2_HOME环境变量,可以启动多个PM2实例。

PM2_HOME='.pm2' pm2 start echo.js --name="echo-node-1"
PM2_HOME='.pm3' pm2 start echo.js --name="echo-node-2"

This will start two different PM2 instances. 这将启动两个不同的PM2实例。To list processes managed by each different instances do:要列出由每个不同实例管理的流程,请执行以下操作:

PM2_HOME='.pm2' pm2 list
PM2_HOME='.pm3' pm2 list

Launch PM2 in no deamon不遗余力地发射PM2

Make sure you kill any PM2 instance before starting PM2 in no deamon mode (pm2 kill).确保在非执事模式(pm2 kill)下启动PM2之前杀死任何PM2实例。

Launching PM2 without daemonizing itself:在不进行后台监控的情况下启动PM2:

pm2 start app.js --no-daemon

There is also the CLI pm2-runtime installed by default at PM2 installation, that is a drop-in replacement of the Node.js binary.pm2安装时默认安装了CLI pm2-runtime,这是Node.js二进制文件的替代品。

Stateless apps无状态应用程序

It is a general rule that your production application should be stateless. 一般来说,生产应用程序应该是无状态的。Every data, states, websocket session, session data, must be shared via any kind of database or PUB/SUB system.每个数据、状态、websocket会话、会话数据都必须通过任何类型的数据库或发布/订阅系统共享。

If not, your application will be painfull to scale on the same server and accross multiple servers.否则,您的应用程序将难以在同一台服务器上扩展,也无法跨多台服务器扩展。

For example you could use connect-redis to share sessions.例如,您可以使用connect-redis共享会话。

We also recommend you to follow the 12 factor convention: 我们还建议您遵循12因素约定:http://12factor.net/

Setup pm2 on a server在服务器上安装pm2

How To Use pm2 to Setup a Node.js Production Environment On An Ubuntu VPS.如何使用pm2在Ubuntu VPS上设置Node.js生产环境。

Log and PID files日志和PID文件

By default, logs (error and output), pid files, dumps, and PM2 logs are located in ~/.pm2/:默认情况下,日志(错误和输出)、pid文件、转储和PM2日志位于~/.pm2/

.pm2/
├── dump.pm2
├── custom_options.sh
├── pm2.log
├── pm2.pid
├── logs
└── pids

Enabling Harmony ES6促进和谐ES6

The --node-args option allows the addition of arguments to the node interpreter. --node-args选项允许向节点解释器添加参数。To enable harmony for a process type the following command:要为流程启用协调,请键入以下命令:

pm2 start my_app.js --node-args="--harmony"

And within a JSON declaration:在JSON声明中:

[{
  "name" : "ES6",
  "script" : "es6.js",
  "node_args" : "--harmony"
}]

CoffeeScript

CoffeeScript v1

pm2 install coffee-script 
pm2 start app.coffee

CoffeeScript v2

pm2 install coffeescript
pm2 start app.coffee

That’s all!这就是全部!

Piping JSON管道JSON

Pull-requests:拉取请求:

#!/bin/bash

read -d '' my_json <<_EOF_
[{
    "name"       : "app1",
    "script"     : "/home/projects/pm2_nodetest/app.js",
    "instances"  : "4",
    "error_file" : "./logz/child-err.log",
    "out_file"   : "./logz/child-out.log",
    "pid_file"   : "./logz/child.pid",
    "exec_mode"  : "cluster_mode",
    "port"       : 4200
}]
_EOF_

echo $my_json | pm2 start -

Process title过程标题

You can specify the env variable PROCESS_FILE when start an application with PM2, it will be set a process title. 使用PM2启动应用程序时,可以指定env变量PROCESS_FILE,该文件将设置为进程标题。It pretty useful when trying to get specific data from the process, for example you can use ps -fC name.它在尝试从进程中获取特定数据时非常有用,例如,您可以使用ps -fC name

Transpilers运输机

Refer to Using transpilers with PM2 tutorial.请参阅使用带PM2的Transpiler教程。

User tips from issues问题的用户提示

External resources and articles外部资源和文章