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 ~/.bashrc
或source ~/.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安装时默认安装了CLI pm2-runtime
installed by default at PM2 installation, that is a drop-in replacement of the Node.js binary.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
Log and PID files日志和PID文件
By default, logs (error and output), pid files, dumps, and PM2 logs are located in 默认情况下,日志(错误和输出)、pid文件、转储和PM2日志位于~/.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 使用PM2启动应用程序时,可以指定env变量PROCESS_FILE
when start an application with PM2, it will be set a process title. 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问题的用户提示
- Vagrant and pm2 #289
- Start the same application on different ports #322
- Using ansible with pm2
- Cron string as argument
- Restart when process reaches a specific memory amount
- Sticky sessions and socket.io discussion
- EACCESS - understanding pm2 user/root rights
External resources and articles外部资源和文章
- PM2 — Utility Overview & Installation
- How To Set Up a Node.js Application for Production on Ubuntu 16.04
- Tutorial: Creating and managing a Node.js server on AWS, part 2
- Goodbye node-forever, hello pm2
- https://www.howtoforge.com/tutorial/how-to-deploy-nodejs-applications-with-pm2-and-nginx-on-ubuntu/
- https://serversforhackers.com/editions/2014/11/04/pm2/
- http://www.allaboutghost.com/keep-ghost-running-with-pm2/
- http://blog.ponyfoo.com/2013/09/19/deploying-node-apps-to-aws-using-grunt
- http://www.allaboutghost.com/keep-ghost-running-with-pm2/
- http://bioselemental.com/keeping-ghost-alive-with-pm2/
- http://blog.chyld.net/installing-ghost-on-ubuntu-13-10-aws-ec2-instance-with-pm2/
- http://blog.marvinroger.fr/gerer-ses-applications-node-en-production-pm2/
- https://www.codersgrid.com/2013/06/29/pm2-process-manager-for-node-js/
- http://www.z-car.com/blog/programming/how-to-rotate-logs-using-pm2-process-manager-for-node-js
- http://yosoftware.com/blog/7-tips-for-a-node-js/
- https://www.exponential.io/blog/nodeday-2014-moving-a-large-developer-workforce-to-nodejs
- http://blog.rapsli.ch/posts/2013/2013-10-17-node-monitor-pm2.html
- https://coderwall.com/p/igdqyw
- http://revdancatt.com/2013/09/17/node-day-1-getting-the-server-installing-node-and-pm2/
- https://medium.com/tech-talk/e7c0b0e5ce3c