PM2 启动服务使用80端口

原则上不允许普通用户操作1024以下的端口,如果要绑定,则需要root权限。

解决方法:

  1. 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!

  2. 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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值