cannot run node.js webservice on port 80 or 443 - linux

I have a VPS running CENTOS and I'm experimenting with Node.js.
I ran an example node.js server running correctly on various ports, but whenever I tried to run node on port 80 (same error for 443) I get the following error:
root#mic [~/Projects/NodeTutorial2]# node index.js
Server running on port 80.
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:883:11)
at Server._listen2 (net.js:1021:14)
at listen (net.js:1043:10)
at Server.listen (net.js:1109:5)
at Object.<anonymous> (/root/Projects/NodeTutorial2/index.js:8:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
I've tried to redirect port 80 to 3000 by doing:
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000
Apparently I executed this command twice because:
when I did a: sudo iptables -t nat -L, it returned:
root#mic [~]# sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
REDIRECT tcp -- anywhere anywhere tcp dpt:http redir ports 3000
REDIRECT tcp -- anywhere anywhere tcp dpt:http redir ports 3000
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
but to no avail, I still get the same error.
I tried to see what is running on port 80 with the netstat -tulpn | grep:80 command and it returns:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 477/httpd
tcp 0 0 :::80 :::* LISTEN 477/httpd
So I had killed the process running on port 80 and I got kicked off my VPS immediately.
What ports should I be running my node.js webservice?
According to my putty settings the port I'm accessing is port 22, so I don't understand what is going on there.
Additionally, I'm not planning to run my webservice as root.
I've created another user with less permissions that will run the node.js webservice.

Setup nodejs to use port 8000
install nginx and set it up as a reverse proxy for your nodejs app.
Don't use Apache, it creates a thread/process for every request, while nginx doesn't. It works similar to nodejs event loop where you have a queue of request that need to be processed and nginx worker processes take each request from the queue and process it - in your case send a request to nodejs server and then wait for the response.
And never use nodejs on port 80, there are just too many use-cases to handle and there is no need to reinvent the wheel

Related

Exposing a webapp in Azure

I am setting up a Nexus OSS on an Azure VM.
I have set it up on a Ubuntu 16.04 LTS.
When I connect to the webapp via an SSH tunnel, I can access the Nexus repository manager. When I try to open it directly, I cannot get it to work.
As per the Azure docs and several Stackoverflow responses, I have updated the NSG and added port 8081 to be allowed but with no success. I also check the UFW (Ubuntu Firewall) and it is not even activated.
EDIT :
netstat -plant | grep 8081
tcp 0 0 127.0.0.1:33519 0.0.0.0:* LISTEN 18081/java
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 18081/java
tcp 0 0 127.0.0.1:8081 127.0.0.1:60242 TIME_WAIT -
tcp 0 0 127.0.0.1:8081 127.0.0.1:60366 TIME_WAIT -
tcp 0 0 127.0.0.1:8081 127.0.0.1:60244 TIME_WAIT -
EDIT2 :
admin#nexus-vm:~$ sudo iptables -nL INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
Does anyone have any idea what could be wrong?
Thanks in advance!
Regards
The problem was the firewall of my company. Tested it over 4G and it works.

Cant connect express app

Locally I can connect to my express app on port 9000. If I start it on remote server I am unsuccessful to reach app, but I see in console logs that it successfully starts.
I see next output for netstat command after $my-express-app pm2 start bin/www
tcp6 0 0 :::3000 :::* LISTEN 52407/www
tcp6 0 0 :::8000 :::* LISTEN 43298/server.js
tcp6 0 0 :::9000 :::* LISTEN 52407/www
And next if I start as $my-express-app pm2 start app.js
tcp6 0 0 :::8000 :::* LISTEN 43298/server.js
tcp6 0 0 :::9000 :::* LISTEN 53096/app.js
My setup configuration is next
...................
app.set('port', 9000)
...................
app.listen(app.get('port'));
Have I missed something?
Express version is 4.x
Update
I also tried to bind app to listen any ip app.listen(app.get('port'),'0.0.0.0')
I have add 2 input/output rules (udp rule was exist before)
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:9000
ACCEPT udp -- anywhere anywhere udp dpt:bootpc
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ufw status tells me it inactive.
Have no sucess. Environment ubuntu 14.04
Update
I was able to run app on port 8000, where other js app is running normally. I cant find any settings related to this port. 9000 still not works. Below is nmap scan for 9000 port
nmap -p 9000 127.0.0.1
Starting Nmap 6.40 ( http://nmap.org ) at 2017-10-04 08:52 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000070s latency).
PORT STATE SERVICE
9000/tcp open cslistener
Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds
nmap -p 9000 myip
Starting Nmap 6.40 ( http://nmap.org ) at 2017-10-04 08:52 UTC
Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn
Nmap done: 1 IP address (0 hosts up) scanned in 3.05 seconds
RESOLVE
I need to setup endpoint to port 9000 on azure portal. It works now. Thanks.
You should check your remote server firewall and add the port 9000 to be opened for traffic.
What operating system are you using, and who is hosting this server for you? For example, I know that if you rent an Ubuntu server on DigitalOcean, most ports (including 9000) will be blocked by default by the firewall, ufw. If you're running on a new-ish version of Ubuntu, you can check your current firewall rules with ufw status. You may have to modify your firewall rules with ufw allow 9000.

Fresh install of DataStax Cassandra fails with "Install Errored: The installed agent doesn't seem to be responding"

I have 4 fresh Ubuntu 12.04 instances that I am trying to install Cassandra (DataStax Enterprise) on. I have installed and configured everything (from a known good installation procedure) and I am at the point of connecting to OpsCenter and creating a cluster. The OpsCenter agent seems to install fine on 3 out of the 4 nodes, but the one that is actually running OpsCenter (dual-purposed as an OpsCenter and Cassandra node) fails with:
Install Errored: The installed agent doesn't seem to be responding
In the /var/log/opscenter/opscenterd.log I see this:
WARN: HTTP request http://10.1.1.26:61621/alive? failed: 503 Service Unavailable
This is the node with OpsCenter on it. The opscenterd service is running, 61621 is listening and accessible from other nodes and from itself, I tested wget to that URL and it indeed throws a 503. All other nodes respond with a 200. Not sure what to look for... Any suggestions? Let me know if you need more details - I didn't want to clutter the post with too much useless/irrelevant details...
i found same error like you.
I did change rules Iptables , and the issue resolve.
[root#ip-172-xxxxx ~]# iptables -I INPUT -p tcp --dport 61620 -j ACCEPT
[root#ip-172-xxxxx ~]# iptables -I INPUT -p tcp --dport 50031 -j ACCEPT
[root#ip-172-xxxxx ~]# iptables -I OUTPUT -p tcp --dport 50031 -j ACCEPT
[root#ip-172-xxxxx ~]# iptables -I OUTPUT -p tcp --dport 61620 -j ACCEPT
iptables -L
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:61620
ACCEPT tcp -- anywhere anywhere tcp dpt:50031
ACCEPT tcp -- anywhere anywhere tcp dpt:ddi-tcp-1
Lucky!

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

When I try to start node on port 80, the error tells me that the port is in use. I imagine that's Apache.
What is the proper way to "take over" port 80, and keep it that way after a server restart?
(Linux xxxx.__.com 2.6.32-5-amd64 #1 SMP Tue Jun 14 09:42:28 UTC 2011 x86_64 GNU/Linux)
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
To take over port 80 when another process is listening on it, you must kill the process (or somehow tell it to stop listening). To ensure that Apache doesn't try to listen on port 80 again the next time it starts, you need to edit its configuration or prevent it from starting up.
To see which process is listening on port 80, run sudo netstat -ntap and look for the row with Local Address ending in port :80. The PID of the process (and the name) is in the far right column.
you can use node.js with node-http-proxy check this link How to use vhosts alongside node-http-proxy? and
How do I run Node.js on port 80?
Thanks & Regards,
Alok
A constantly running unused apache maybe a security hole, in any case no sense in running unused services.
On the chance you're on ubuntu, this what I used..
sudo service apache2 stop
sudo update-rc.d apache2 remove
You can access port 80 once you stop the service currently using it.
In your case, follow these steps:
1) Use systemctl to stop apache2:
sudo systemctl stop apache2
2) Check apache2 status:
sudo systemctl status apache2
Or just by entering http://localhost in your browser. If you get an error, you are good to go.
ERR_CONNECTION_REFUSED
3) Now start your NodeJS server on port 80.
4) You can access your server at http://localhost
UPDATE
If you are seeing errors on your console, try node preceding with sudo
For eg. sudo node server.js
Here are the errors
events.js:137
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:1003:13)
at _exceptionWithHostPort (util.js:1024:20)
at Server.setupListenHandle [as _listen2] (net.js:1349:19)
at listenInCluster (net.js:1407:12)
at Server.listen (net.js:1495:7)
at Object.<anonymous> (/home/abdus/Desktop/voice-recognition/test.js:7:4)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)

Not start play! application in Microsoft Azure on CentOS

I created a virtual machine CentOS.
Created end-point in the control panel on port 80 and 443. In CentOS added rules to iptables:
# Generated by iptables-save v1.4.7 on Thu Aug 9 18:07:49 2012
*filter
:INPUT ACCEPT [142:12032]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [146:18544]
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
COMMIT
# Completed on Thu Aug 9 18:07:49 2012
Run the application:
CompilerOracle: exclude jregex/Pretokenizer.next
Listening for transport dt_socket at address: 8000
18:07:05,799 INFO ~ Starting /srv/play-1.2.5/localevent
18:07:05,808 INFO ~ Module .svn is ignored, name starts with a dot
18:07:06,820 WARN ~ You're running Play! in DEV mode
18:07:06,975 INFO ~ Listening for HTTP at /127.0.0.1:80 (Waiting a first request to start) ...
I went to the address: *.сloudapp.net
But the application does not start. In what may be the reason?
#update1
SELinux is disabled. Version of CentOS - 6.2
#update2
For the test was installed Apache. Home Apache displayed.
Added proxy from 80 to 9000 port in httpd.conf. Play was launched at the 9000 port.
Apache returns a 503 error. Wget 127.0.0.1:9000 gets everything right.
AFAIK, you have to add an endpoint to your Azure virtual machine in order to allow to connect to the 80 TCP port.
For instance, see this doc, under the "Expose Redis to the outside" section, by setting your private and public ports to 80.
Looked logs Apache:
[error] (13) Permission denied: proxy: HTTP: attempt to connect to 127.0.0.1:9000 (127.0.0.1) failed
[error] ap_proxy_connect_backend disabling worker for (127.0.0.1)
Solution:
setsebool httpd_can_network_connect 1

Resources