Application generating "Error: listen EACCES" while starting application with pm2 on port 80? - linux

We are using pm2 for starting my nodejs app on port 80 on ubuntu. But the application generating error **Error: listen EACCES**. Our pm2 version is 0.12.7 and we are using the following command:
sudo pm2 start app.js -- dev
On running whereis node we get the following result:
node: /usr/bin/node /usr/sbin/node
We have already tried following commands:
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/local/sbin/node
Any idea where are we going wrong?

Sounds like you might have another service already listening on port 80. try this:
sudo netstat -tulpn
The output of this will tell you if any other process is currently using port 80.

Related

All ports not available with React npm run start

So up until yesterday I was able to do npm start no issues.
The versions of things I'm running
OSX Mojave...
npm: '8.19.3',
node: '19.2.0',
Now it is saying that port 3000 is in use. I've done all the tricks to see if 3000 is in use is isn't
changed the port to 3001 3005 4444 and various other and it doesn't work.
Still the same message.
serve -s build works great and runs the app on 3000. no idea what is going on..
error message
✔ Something is already running on port 4001.
Would you like to run the app on another port instead? … no
I've tried
user#computer >> npx kill-port 4001
Could not kill process on port 4001. No process running on port.
user#computer >> mtbcrm_react % lsof -i tcp:4001

run npm start with sudo doesn't allow access port 80

I have a EC2 running instance from AWS and I tried to run my node server on port 80 using sudo npm start which is just running script node ./bin/www but it crashed and telling me port 80 requires elevated privileges. It was fine if I just do sudo node ./bin/www. I am not sure why and it seems the npm just doesn't accept my sudo.
I tried to run npm start as a root user and it still wouldn't let me to access port 80.

How do i overcome EADDRESS already in use in PM2

I am running a node js server on aws EC2 instance
when i do node server.js its working but when i start the server using pm2 i am getting code: 'EADDRINUSE',
after installing pm2 can't even use node server.js it also says EADDRINUSE, so to rerun it i have to uninstall the pm2 and stop the node process that runs port 3000, but as soon as i install pm2 the port 3000 is becoming unavailable
root 47971 0.7 2.7 647924 55712 ? Ssl 11:20 0:00 node /home/ubuntu/kanaswap-name-service/server.js
the above process runs automatically when i install pm2 , i cant even kill that process
when i run node server.js
pm2 list
process that runs on port 3000
even after killing the process it runs automatically,
this happens because everytime you start pm2 it says that a service on that port alredy running try changing the port no or you can kill the process if you are using linux
here is the command for kill the process in linux
this is for getting PID
lsof -i:<port no.>
after that you may find that result
node 14977 USER 22u IPv4 191006 0t0 TCP *:3004 (LISTEN)
after that run this
kill 9 <port no.>

How to access node.js site with direct url without mentioning port 8080 in aws ubuntu, Apache2

My node.js application is running on port 8080. I want to access it via url without mentioning port number in the URL. How will i achieve this?
for example :
current : www.domainname.com:8080
Expected :www.domainname.com
Ubuntu os version: 16
Server : Apache2
Need help.
Use Nginx as a reverse proxy for your node js application and mentioned your port in nginx config file.(you can create new config file or you can use default one /etc/nginx/nginx.conf)
Install pm2 and Use pm2 tool to keep running your nodejs application in the background.
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
sudo apt-get install -y nodejs
sudo npm install pm2#latest -g
pm2 start myapp.config.js (replace myapp.config.js with YOURAPP.js )

Running Node app via PM2 on port 80

I have an express that I want to run on port 80. --> app.listen(80);
I'm using PM2 to manage the app (restarting, stopping, monitoring, etc.) . I have a deployment shell script whose last command is PM2 restart index. From the console output, I see no errors and PM2 reports that it successfully completed the command. Yet when I got to my.ec2.ip.address:80 the site is not up. Furthermore, if I run node index.js in my server project directory, I get a Error: listen EACCES 0.0.0.0:80. This makes some sense to me as port 80 is below 1024 and therefore a privileged port. sudo node index.js will allow the launch to work.
I'm a newbie to unix, servers, permissions, and deployment, so in addition to the solution, an explanation of the fundamental concepts contributing to my problem would be greatly appreciated. For instance.. is it bad to simply run my node app as super-user? Is it good practice to run PM2 (therefore possibly running node as..?) root/super-user? The command sudo PM2 restart index leads to sudo: pm2: command not found. Why is PM2 not found when running sudo PM2.. if PM2 is in my path?
Ultimately though, when using PM2 how can I ensure that my server runs on port 80? not found.
Dont use port 80, run on other port like 8080 and redirect 80 to that port with this command
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
It's good to run as little as possible as a priviliged user, as you want to restrict the potential damage in case someone exploits your program. You don't want to run your Node code as root unless you absolutely have to.
Therefore, it's better to run your Node program on an unprivileged port (say, port 8000), and instead have a lightweight web server such as Nginx listen on port 80 and simply forward traffic to your Node program.
If you want to go with Nginx, you can use this configuration to do exactly what I described above, and then just listen with your Node program on port 3000:
server {
listen 80 default;
listen [::]:80 default;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
}
}
I had the same issue, for the ubuntu server.
Fixed with the tutorial below.
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node
https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps
Also here is another solution from 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
https://pm2.keymetrics.io/docs/usage/specifics/#listening-on-port-80-w-o-root
Though, you may have solved the issue but for the one who comes here facing the same issue, this worked me :
For just troubleshooting, run your app using sudo npm start. If your app runs normally then you need to bind port 80 with the help of authbind package. Run these commands :
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
Replace %user% with the user you run pm2. Mine was ubuntu by default.
Set start command in your package.json file to pm2 start <server_file_name>.
Run the app using npm start. It should work !
After lot of time spent configuring nginx, finally uninstall it and followed A.J. suggestion to configure iptables. Thank you A.J.
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
But, if anyone know a perfect tutorial to configure nginx, would be a great help.

Resources