You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js应用部署报错listen EACCES: permission denied 0.0.0.0:300求助

Node.js应用端口权限错误排查与解决

问题背景

部署在AWS Ubuntu实例上的Node.js应用(采用pm2+Nginx管理)启动失败,执行pm2 logs得到如下报错:

0|index    | Error: listen EACCES: permission denied 0.0.0.0:300
0|index    |     at Server.setupListenHandle [as _listen2] (node:net:1723:21)
0|index    |     at listenInCluster (node:net:1788:12)
0|index    |     at Server.listen (node:net:1876:7)
0|index    |     at Function.listen (/home/ubuntu/twitter-helper/nodejs/node_modules/express/lib/application.js:635:24)
0|index    |     at Object.<anonymous> (/home/ubuntu/twitter-helper/nodejs/index.js:353:5)
0|index    |     at Module._compile (node:internal/modules/cjs/loader:1254:14)
0|index    |     at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
0|index    |     at Module.load (node:internal/modules/cjs/loader:1117:32)
0|index    |     at Module._load (node:internal/modules/cjs/loader:958:12)
0|index    |     at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23) {
0|index    |   code: 'EACCES',
0|index    |   errno: -13,
0|index    |   syscall: 'listen',
0|index    |   address: '0.0.0.0',
0|index    |   port: 300
0|index    | }

但应用代码与Nginx配置均未指定300端口:

  • Node.js代码:
const PORT = process.env.PORT || 3001
app.listen(PORT, ()=>{console.log(`listen on port ${PORT}`)})
  • Nginx配置:
server {
        listen 80;
        listen [::]:80;

        root /home/ubuntu/app/react/frontend/build;
        index index.html index.htm index.nginx-debian.html;

        server_name something something here;

        location / {
                try_files $uri /index.html;
        }

        location /api {
            proxy_pass http://127.0.0.1:3001;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
}

300端口的来源分析

报错指向app.listen调用,说明代码中的PORT变量实际被解析为300,可能的触发原因:

  • 环境变量PORT被误设为300:包括系统全局环境变量(/etc/profile、~/.bashrc)、pm2启动时传入的临时变量、应用目录下的.env配置文件(若使用dotenv)
  • pm2配置文件硬编码PORT=300:比如ecosystem.config.js的env字段指定了该端口
  • 启动命令显式传递PORT参数:例如用pm2 start index.js --env PORT=300启动应用

另外,300属于1-1024范围内的特权端口,普通用户进程无绑定权限,这是触发EACCES错误的核心原因。

解决步骤

1. 排查PORT变量的实际取值

  • 登录Ubuntu实例,进入应用目录执行:
    echo $PORT
    
  • 查看pm2应用的环境变量详情:
    pm2 show index
    
    在输出的env区块中检查PORT的取值
  • 检查应用目录下的.env文件:
    cat .env
    
  • 检查系统全局环境变量:
    cat /etc/profile | grep PORT
    cat ~/.bashrc | grep PORT
    

2. 修正端口配置

  • 找到所有误设PORT=300的位置,修改为3001
  • 若使用pm2配置文件,更新ecosystem.config.js:
    module.exports = {
      apps : [{
        name: 'index',
        script: './index.js',
        env: {
          PORT: 3001
        }
      }]
    }
    
  • 重启pm2并加载新配置:
    pm2 restart index --update-env
    

3. 若必须使用特权端口(不推荐)

如果业务需求强制绑定300端口,可通过以下方式解决权限问题:

  • 给Node.js进程授权绑定特权端口(避免root运行,更安全):
    sudo setcap 'cap_net_bind_service=+ep' $(which node)
    
    执行后需重启pm2应用生效

内容的提问来源于stack exchange,提问作者zxcisnoias

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 15:00:12