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

ExpressJS(AdonisJS)基于PM2部署时80端口EACCES权限错误求助

Fixing EACCES Error When Running AdonisJS/ExpressJS on Port 80 with PM2

Hey there! Let's resolve that frustrating Error: listen EACCES IP:80 you're hitting. The core issue is that on most Unix-based systems, ports below 1024 are privileged—regular user accounts don't have permission to bind to them by default. Here are practical, safe solutions to get your app up and running on port 80:

Option 1: Port Forwarding (No App Changes Needed)

Redirect incoming traffic on port 80 to a non-privileged port (like AdonisJS's default 3333) using your system's firewall:

For iptables (Debian/Ubuntu):

# Set up the forward rule
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3333

# Save the rule so it persists after reboot
sudo iptables-save > /etc/iptables/rules.v4

For firewalld (CentOS/RHEL):

# Add the forward rule permanently
sudo firewall-cmd --add-forward-port=port=80:proto=tcp:toport=3333 --permanent

# Reload firewalld to apply changes
sudo firewall-cmd --reload

After this, just run your app on port 3333 via PM2 as usual—traffic to port 80 will automatically route to your app.

Option 2: Grant Node.js Permission to Bind Privileged Ports

Use the setcap command to give your Node.js binary the capability to bind to ports below 1024:

sudo setcap 'cap_net_bind_service=+ep' $(which node)

This lets Node.js (and thus PM2-run apps) use port 80 without root access. Note: If you update Node.js later, you'll need to re-run this command.

This is the most robust approach for production environments—Nginx handles port 80 (and SSL later) and forwards requests to your app running on a non-privileged port.

  1. Install Nginx if you haven't already:

    # Debian/Ubuntu
    sudo apt install nginx
    
    # CentOS/RHEL
    sudo dnf install nginx
    
  2. Create a new Nginx config file (e.g., /etc/nginx/sites-available/your-app.conf):

    server {
        listen 80;
        server_name your-domain.com; # Replace with your domain or IP
    
        location / {
            proxy_pass http://localhost:3333;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
    
  3. Enable the config and restart Nginx:

    # Create symlink to sites-enabled
    sudo ln -s /etc/nginx/sites-available/your-app.conf /etc/nginx/sites-enabled/
    
    # Test config for errors
    sudo nginx -t
    
    # Restart Nginx
    sudo systemctl restart nginx
    
  4. Start your AdonisJS app on port 3333 with PM2:

    pm2 start server.js --name your-app --port 3333
    

While you can run PM2 with root to access port 80, this is a security risk—if your app has vulnerabilities, attackers could gain root access to your server. If you still want to test this:

sudo pm2 start server.js --name your-app --port 80

Avoid this in production!


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:29:41