user3766930
user3766930

Reputation: 5829

I currently run my amazon ec2 instance on port 3000. I want to run it on port 80 instead. How can I do it?

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 ?

Upvotes: 6

Views: 9134

Answers (5)

kta
kta

Reputation: 20110

I use nginx proxy server for port forwarding

Upvotes: -1

Javier Salmeron
Javier Salmeron

Reputation: 8825

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

Upvotes: 1

Rohith
Rohith

Reputation: 43

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

Upvotes: -3

Matt Houser
Matt Houser

Reputation: 36073

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:

Upvotes: 4

E.J. Brennan
E.J. Brennan

Reputation: 46849

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.

Upvotes: 10

Related Questions