How to Start Node.Js on Port 80 on a Linux Server

How to start node.js on port 80 on a linux server?

you can use ip tables to map port 80 to 8000

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000

to make it permanent

sudo sh -c "iptables-save > /etc/iptables.rules"

and add

pre-up iptables-restore < /etc/iptables.rules

to your /etc/network/interfaces

Best practices when running Node.js with port 80 (Ubuntu / Linode)


Port 80

What I do on my cloud instances is I redirect port 80 to port 3000 with this command:

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

Then I launch my Node.js on port 3000. Requests to port 80 will get mapped to port 3000.

You should also edit your /etc/rc.local file and add that line minus the sudo. That will add the redirect when the machine boots up. You don't need sudo in /etc/rc.local because the commands there are run as root when the system boots.

Logs

Use the forever module to launch your Node.js with. It will make sure that it restarts if it ever crashes and it will redirect console logs to a file.

Launch on Boot

Add your Node.js start script to the file you edited for port redirection, /etc/rc.local. That will run your Node.js launch script when the system starts.

Digital Ocean & other VPS

This not only applies to Linode, but Digital Ocean, AWS EC2 and other VPS providers as well. However, on RedHat based systems /etc/rc.local is /ect/rc.d/local.

How do I run Node.js on port 80?

What you need to do is have 2 ip's for the server you are running. Apache has 1 ip bound to port 80 and then node.js has the other ip bound to port 80.

Using node and its listen directive has 2 values eg. .listen(80, NODEJS_IP or DNS NAME);

Some other advice.

I would not use apache with nodejs as it's not evented. So this really isn't recommended. I would actually look into using NGINX as its a much better pairing with Node.

Running NodeJs application on port 80 of amazon linux

sudo bash will allow you to connect as root on your EC2 Amazon Linux instance.
I would question why do you want to run NodeJS on port 80, the best practice would have a load balancer in front of your instance to accept HTTPS calls and relay to whatever port nodejs will run on your instance, in a private subnet.

I would suggest to read this doc to learn how to do this : https://aws.amazon.com/getting-started/projects/deploy-nodejs-web-app/

How to open port 80 for node server on local machine?

Paste the output from

netstat -ptuln

command.I think the problem is that your web server runs on local address and can not be reachable for other machines in network.

deploy nodejs to http server on port 80

It is always better to use Nginx when deploying a Node server. A detailed tutorial of how to set it up is provided in the link below(If you are using Linux). It is very easy to setup and very powerful.

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

Edited -

Here is an alternate solution to get node working in port 80.

https://gist.github.com/kentbrew/776580

You dont want any other server for that.



Related Topics



Leave a reply



Submit