ExpressJS(AdonisJS)基于PM2部署时80端口EACCES权限错误求助
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.
Option 3: Use Nginx as a Reverse Proxy (Recommended for Production)
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.
Install Nginx if you haven't already:
# Debian/Ubuntu sudo apt install nginx # CentOS/RHEL sudo dnf install nginxCreate 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; } }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 nginxStart your AdonisJS app on port 3333 with PM2:
pm2 start server.js --name your-app --port 3333
Option 4: Run PM2 as Root (Not Recommended)
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

