原则上不允许普通用户操作1024以下的端口,如果要绑定,则需要root权限。
解决方法:
- AUTHBIND:
It’s a general rule that you should not run node as root. However only root can bind to ports less than 1024. This is where authbind comes in. Authbind allows non-root users to bind to ports less than 1024. Replace %user% with the user that will be running 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):
+alias pm2='authbind --deep pm2'
Finally ensure that pm2 is updated with authbind:
authbind --deep pm2 update
Or simply pm2 update if you added the alias to your user’s profile.
Now you can start applications using PM2 that can bind to port 80 without being root!
-
startFile class:
报 null 80,因为权限不够。
PM2用sudu是有权限的,但是执行命令的是由PM2fork出来的新进程进行处理,新进程是没有root权限的,所以,用启动文件的方式,让PM2自己去启动脚本。
xxx.json,放在www同级目录
{ "apps" : [{ "name" : "app-name", "script" : "./www", "instances" : "max", "exec_mode" : "cluster" , "env_development": { "PORT": 3000, "NODE_ENV": "development" }, "env_production": { "PORT": 80, "NODE_ENV": "production" } }] }
Here the default environment is in the env, but you can decide to use env_production by using pm2 start ecosystem.config.js --env production.
You can define as many environments as you like, just remember that you must pass the name of the environment (after env_) you want to use with --env.
login with root auth
pm2 start xxx.json --env production
if failed check 80 port is used by another process(netstat -anp|grep 80, and kill the process)