6

I have a node.js app that runs on port 3000. I deployed it on amazon web services (ec2) and it works over there. My server.js file says:

var port            = process.env.PORT || 3000;
(...)
app.listen(port);
console.log('App listening on port ' + port);

My security group in aws settings seems to have the port 80 also opened:

enter image description here

so I thought it's enough to just change the var port to = 80 and restart the server. But when I did that I got an error:

bitnami@ip-172-31-47-102:~/apps/myproject/www/myproject$ sudo node server.js
App listening on port 80
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::80
    at Object.exports._errnoException (util.js:856:11)
    at exports._exceptionWithHostPort (util.js:879:20)
    at Server._listen2 (net.js:1237:14)
    at listen (net.js:1273:10)
    at Server.listen (net.js:1369:5)
    at Function.app.listen (/opt/bitnami/apps/myproject/www/myproject/node_modules/express/lib/application.js:542:24)
    at Object.<anonymous> (/opt/bitnami/apps/myproject/www/myproject/server.js:43:5)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:430:10)
    at startup (node.js:141:18)
    at node.js:1003:3

I'm using the Bitnami MEAN 3.2.1-0 system on Amazon.

Also, the reason why I want to change this port is this:

so far all my webservices operate on port 3000. However, I also have there a public_html folder with the index.html file. So when any user wants to display my webpage he has to enter not only the webpage, but also the port (3000) which is not that convenient.

So far the whole app stays under www.ec2-some-random-amazom-numbers.eu-west-1.compute.amazonaws.com:3000/index.html so I will buy a normal top level domain to point at it (eg. something.com ) but then - do I still need to change the port 3000 to 80 in that case? Or maybe it's common to leave apps on port other than 80?

If the latter, then will it be possible for me to leave the port as it is and just point the top level domain on this whole long amazon one with a port 3000 at the end?

So for example: when user types www.something.com it will redirect him to www.ec2-some-random-amazom-numbers.eu-west-1.compute.amazonaws.com:3000/index.html ?

user3766930
  • 5,629
  • 10
  • 51
  • 104

5 Answers5

10

Something like this should work for you:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

You want to use iptables to forward request coming in on port 80 to port 3000 internally.

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
  • Thank you. Could you give me a hint whether leaving the app on port `3000` is safe and could work as a production environment? Then I would just point my top level domain to the `address.com:3000/index.html` and leave all webservices working on port `3000`. – user3766930 Apr 24 '16 at 12:34
  • I don't think it would be 'unsafe'. I just don't personally like users having to remember to enter a port, but if you have a small number of users and they are/can be 'trained', then I don't think there would be an issue. – E.J. Brennan Apr 24 '16 at 12:36
4

Based on the error you're getting, (EADDRINUSE), some other web server is listening on port 80 already on your server. If you can prevent that server from running, then you can change your app to run on port 80. Do the following:

  1. Figure out what's already running on port 80 on your server. There's a good chance it's Apache. Try using netstat to verify.
  2. Kill it (and prevent it from restarting). This will depend on whats listening.
  3. Move your app to port 80 just like you've already tried.

Additional resources:

Matt Houser
  • 33,983
  • 6
  • 70
  • 88
1

If you are using the Bitnami Mean Stack then Apache is listening in port 80, hence the conflict. You can either stop the bundled Apache:

sudo /opt/bitnami/ctlscript.sh stop apache

Or you could add a ProxyPass rule in the Apache configuration:

https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#proxypass

Javier Salmeron
  • 8,365
  • 2
  • 28
  • 23
-1

I use nginx proxy server for port forwarding

kta
  • 19,412
  • 7
  • 65
  • 47
  • how? mine showing error 500. – Harat Aug 30 '22 at 08:22
  • @Harat - Any proxy server (nginx/apache) can help to map any internal port 5000 to port 80. Please Read this thread - https://stackoverflow.com/questions/24861311/forwarding-port-80-to-8080-using-nginx. – kta Oct 18 '22 at 00:48
-3

I have faced the same issue.

The solution is to start your NodeJs or ExpressJs app using Port 80, I mean var port = 80 and start the app using sudo node bin/www

My issue is not able to access the app from internet when the app is running on port 3000: NodeJs App in AWS using port 3000 is not accessible from Internet

Rohith
  • 43
  • 2
  • 10