Ubuntu: Http-server on port 80 starting up, but can't access from browser? - node.js

So I have a web application being run on an http-server via npm. In my package.jsonfile, I have the line "start": "sudo http-server -a [my ip address] -p 8065 -c-1", and my app runs fine when I go to http://myipaddress:8065. However if I change the 8065 to just 80, in the json file (which is what I want), I still get the success message:
Starting up http-server, serving ./
Available on:
http://myipaddress:80
But when I go to the link, chrome givess me an ERR_CONNECTION_REFUSED. Anybody know what's going on?

I would suggest there are three possible problems here.
Port 80 is already in use.
You are not running the application as root (you can't bind to ports <1024 if you are not root)
http-server isn't binding correctly
To check if port 80 is already in use try
netstat -lntu | grep :80
If port 80 is already in use you should see something like
tcp6 0 0 :::80 :::* LISTEN
You will need to close whatever is running on port 80 (apache? nginx?)
To check if you can actually bind to port 80, try running http-server from the console rather than via npm i.e.
sudo http-server -a [my ip address] -p 80 -c-1
If the above works you should be able to run npm as root to start your http-server i.e.
sudo npm start
You may need to remove sudo from your package.json:
"start": "http-server -a [my ip address] -p 8065 -c-1"
We need to make sure that http-server is working correctly on your system. We will test it with w3m a console based web browser.
You may need to install w3m with sudo apt-get install w3m if you do not have it already.
create a new directory. mkdir /tmp/testing
CD into new dir cd /tmp/testing
Start http-server with `http-server . -a localhost -p 1234
Visit http://localhost:1234 with w3m w3m http://localhost:1234/
Start http-server with `http-server . -a localhost -p 80
Visit http://localhost in a w3m w3m http://localhost/ does it work?

Quick tests:
Try to access this on as the localhost address, either localhost or 127.0.0.1 to shortcut any potential firewalls.
Try to telnet to this address on port 80 to see what the server replies (if any).

Do you have Apache installed? Are sure putting your application server on port 80 is not in conflict with Apache?
In that case it is better to redirect port 80 to your application server that just starting it on the Apache port.

Is it error 102? Check this link. Probably it's caused by some extensions you installed.

To run nodejs apps with pot less than 1000 you need a root access. Use sudo node app.js Also dont forget to open firewall. And make sure nobody else listening on port 80.

Related

Starting a simple HTTP-server using "npm" without installing npm

Which command do I need to use to start a simple HTTP server using "npm", the specified port to be used is port 8080. Also, I don't have to download the npm package. The only hint I got is that I can download the basic web server packet, also from there I can specify the port 8080?
Once the http server has been configured with the x-server name at port xxxx, you can start it with the command:
x-server -p xxxx
for example if my server's name is simple-http-server at port 8080:
simple-http-server -p 8080
would start it.
for the next exercice:
php -S 127.0.0.1:8080
I am also getting started on HTB.
contact me (in the next 23 hours) at this account or leave a correspondance as comment if you want to work with me to break through on HTB.
Once npm is installed, the command is simply: http-server -p 8080 (without writing 'npm' at the beginning of the line).

Access Superset through a remote browser

I have installed Apache Superset on a remote Linux Server and initialized it on port 8080. When I pull up localhost:8080 on the Linux server, the homepage shows up which suggests that the installation worked as per their instructions here.
When I try to access this page from my laptop (Windows- Browser:Chrome) with http://server-name:8080. It gives me the 'This site can't be reached' page.
I tested using netcat if the connection was open by typing nc -zvw3 server-name 8080 and it gave me Connection to server-name 8080 port [tcp/webcache] succeeded!
I have Jupyter installed on the same server on port 8888 and it works perfectly. Any thoughts would be greatly appreciated.
You may need to try this command by defining the IP address:
superset run -h 0.0.0.0 -p 8080

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.

Git hooks may need higher privileges

I'm using node.js for my webserver, and I would like forever (or something like it) to run the server.
I'm also using git to manage the website. I have a bare repository on the server that I can push to/pull from on my local machine. I would like the repository to do three things when I push to it.
CD to my working directory (on the server)
Have the working directory pull from the bare repo
Restart the running webserver.
The following script seems like it's what I should use as a post-receive hook in my bare repo.
cd ~/site
git pull
sudo forever stopall
sudo forever start main.js
However, I don't think it's smart to have the git hook use sudo like that. The script needs elevated to run on port 80.
How should I be doing this? What should my git post-receive look like?
Thanks!
Well, for my particular case, it turns out I shouldn't be running node as a super user for security reasons. I wanted it to be elevated to run on port 80, but it didn't need to be elevated to run on port 8000.
So I forwarded port 80 to port 8000, and now am running node on port 8000. It still works identically to how it did before.
The command that I used in particular to forward port 80 to 8000 is
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8000

How can I run node.js Express in production mode via sudo?

I'm using the npm package express version 2.5.2 with node version .0.6.5. I appear to be running bash version 4.1.5 on Debian 4.4.5.
I'm trying to run my server in production mode but it still runs in development mode.
I run these commands in my bash shell:
$ export NODE_ENV=production
$ echo $NODE_ENV
production
$ sudo echo $NODE_ENV
production
$ sudo node bootstrap.js
I have this code inside bootstrap.js:
var bootstrap_app = module.exports = express.createServer();
//...
console.log(bootstrap_app.settings.env);
and here's what I see printed to standard out:
development
Is this a problem with my usage, or my system?
EDIT:
Thanks to ThiefMaster for his properly identifying that this issue stems from my running node as root. ThiefMaster suggested using iptables to forward from port 80 to an unprivileged port, but my system gives me an error. Moving this discussion to superuser.com or serverfault.com (link to follow)
Most environment variables are unset when using sudo for security reasons. So you cannot pass that environment variable to node without modifying your sudoers file to allow that variable to passt through.
However, you shouldn't run node as root anyway. So here's a good workaround:
If you just need it for port 80, run node on an unprivileged port and setup an iptables forward to map port 80 to that port:
iptables -A PREROUTING -d 1.2.3.4/32 -i eth0 -p tcp -m tcp --dport 80 -j DNAT --to-destination 2.3.4.5:1234
Replace 1.2.3.4 with your public IP, 2.3.4.5 with the IP node runs on (could be the public one or 127.0.0.1) and 1234 with the port node runs on.
With a sufficiently recent kernel that has capability support you could also grant the node executable the CAP_NET_BIND_SERVICE privilege using the following command as root:
setcap 'cap_net_bind_service=+ep' /usr/bin/node
Note that this will allow any user on your system to open privileged ports using node!
sudo NODE_ENV=production /usr/local/bin/node /usr/local/apps/test/app.js

Resources