Starting NodeJS on OpenShift without EADDRINUSE errors - node.js

I am trying to get NodeJS to run on OpenShift, do encounter the same issue though no matter what. Here is an example:
UPDATE with more details <<<<<
FIRST:
This piece of code works fine straight out of Cloud9
var port = process.env.PORT || "127.0.0.1";
var ipaddress = process.env.IP || 8080;
console.log("Getting started here!");
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write("Welcome to Node.js on OpenShift!\n\n");
response.end("Thanks for visiting us!!!! \n");
});
server.listen( port, ipaddress, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
console.log("Listening to " + ipaddress + ":" + port + "...");
However as soon as I push it to my OpenShift account changing the port and ipaddress variables to:
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
... I am getting this on the OpenShift VM when I SSH over, change to app-root/runtime/repo and run node server.js:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1129:5)
That would indicate the port is in use already, right? I changed the port to 15550 as I have read somewhere here on stackoverflow I assume that a specific port range is not directly available - only via port forwarding.
Guess what, I just get a different error (EACCES).
Here are the processes running on my OpenShift VM (as far as I can see with my account):
PID TTY STAT TIME COMMAND
177800 ? S 0:00 sshd: XXXX#pts/1
177801 pts/1 Ss 0:00 /bin/bash --init-file /usr/bin/rhcsh -i
210176 pts/1 R+ 0:00 ps ax
SECOND:
I wonder how I can cut down the build process? When I push my code to OpenShift this is what I get:
remote: /bin/sh: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: /bin/bash: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: /bin/bash: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: sh: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: sh: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: Stopping NodeJS cartridge
remote: Wed Mar 18 2015 07:27:49 GMT-0400 (EDT): Stopping application 'nodejs' ...
remote: Wed Mar 18 2015 07:27:50 GMT-0400 (EDT): Stopped Node application 'nodejs'
remote: /bin/sh: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: /bin/bash: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: /bin/bash: warning: setlocale: LC_ALL: cannot change locale (C.UTF-8)
remote: Saving away previously installed Node modules
remote: Building git ref 'master', commit e8239d1
remote: Building NodeJS cartridge
The whole process takes like 10 seconds or more. I am not sure what exactly is happening behind the scenes but it seems to be a lot.
So my second questions, is there a way to cut that build time down?
I tried to create an empty file in the .opengit/markers directory as I have read somewhere, but it did not help to improve anything.
Thanks for your help!
z4c

.. I am getting this on the OpenShift VM when I SSH over, change to app-root/runtime/repo and run node server.js
Don't manually run the server yourself. When you push your code to OpenShift, it stops the server, does the build, deploys the code, and starts the server.
So the server is already running. That's why you're getting Error: listen EADDRINUSE try running it yourself. It won't let you run on your own port. That's why you're getting (EACCES) trying to run it on 15550.
If you need to start/stop/restart the server, use OpenShift's rhc command or from the admin console.
For your 2nd question: is there a way to cut that build time down?
The answer is yes. Setting up hot deploy will save you few seconds (by creating an empty file .openshift/markers/hot_deploy) or change the script not to do the build but that's not a good idea. It's doing what it's supposed to do. The right way to cut down the down time to 0 (unless OpenShift is down) is to use the load balancing feature OpenShift offers. It can run 2+ servers at a time. So when you push your code, it stops 1 server while the other servers are still taking requests. Once the 1st server is back up, it'll rebounce the 2nd (and the rest of the servers)

Related

Error deploying a node.js app to Elastic Beanstalk on Amazon Linux 2

I am trying to have a node js app on Elastic Beanstalk.
On Amazon Linux Image the environment gets created and works smooth.In order to get it done I followed the instructions given in the AWS documentation itself. It works on single instance.Also,using Nginx and have terminated HTTPS on the instance by following the info here
The root directory of the app contains:
.ebextensions
index.js
package.json
By following all those instructions step by step given in AWS documentation the environment is created and the app is deployed successfully(For Amazon Linux Image).
However, been trying to deploy it to Amazon Linux 2.And the deployment fails with the Health status for the environment being "DEGRADED".
A snippet of the eb-engine.log:
[INFO]
> grpc#1.24.3 install /var/app/staging/node_modules/grpc
> node-pre-gyp install --fallback-to-build --library=static_library
Failed to execute '/opt/elasticbeanstalk/node-install/node-v10.15.1-linux-x64/bin/node /opt/elasticbeanstalk/node-install/node-v10.15.1-linux-x64/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --library=static_library --module=/var/app/staging/node_modules/grpc/src/node/extension_binary/node-v64-linux-x64-glibc/grpc_node.node --module_name=grpc_node --module_path=/var/app/staging/node_modules/grpc/src/node/extension_binary/node-v64-linux-x64-glibc --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)
Snippet of the web.stdout.log:
Sep 13 12:55:31 ip-172-31-1-73 web: > nodetryapp#1.0.0 start /var/app/current
Sep 13 12:55:31 ip-172-31-1-73 web: > node index.js
Sep 13 12:55:31 ip-172-31-1-73 web: [DEFAULT]
Sep 13 12:55:32 ip-172-31-1-73 web: events.js:174
Sep 13 12:55:32 ip-172-31-1-73 web: throw er; // Unhandled 'error' event
Sep 13 12:55:32 ip-172-31-1-73 web: ^
Sep 13 12:55:32 ip-172-31-1-73 web: Error: listen EACCES: permission denied 0.0.0.0:80
Sep 13 12:55:32 ip-172-31-1-73 web: at Server.setupListenHandle [as _listen2] (net.js:1260:19)
Snippet from nginx/error.log:
2020/09/13 13:06:31 [alert] 4023#0: *1021 1024 worker_connections are not enough while connecting to upstream, client: 127.0.0.1, server: , request: "GET /hudson HTTP/1.1", upstream: "http://127.0.0.1:80/hudson", host: "*SOME IP ADDRESS*"
For deploying the app on Amazon Linux 2 Elastic beanstalk the root folder of the app consists of the following files/folders:
.ebextensions/https-instance.config and .ebextensions/https-instance-single.config
.platform/nginx/conf.d/extendnginx.conf (In order to extend the nginx config so as to terminate the https at the instance)
index.js
package.json
The instance type is a Single instance, t2.micro.
You cannot run your application on port 80 and 443 because these ports are exclusive for Nginx/Apache service inside Beanstalk AMI. Instead of using these ports, use 8080 for HTTP connections and 8443 to HTTPS connections.
Yes, now it is working!So followed what #Raul Barreto said in his answer above.
Made the following changes:
The 'port' environment variable is now set to 8080.
In the .platform/nginx/conf.d/extendnginx.conf file added
upstream nodejs {
server 127.0.0.1:8080;
}
instead of
upstream nodejs {
server 127.0.0.1:80;
}

Why is my application not being deployed on OpenShift?

I believe I have everything set up properly for my server but I keep getting this error
Starting NodeJS cartridge
Tue Jan 05 2016 10:49:19 GMT-0500 (EST): Starting application 'squadstream' ...
Waiting for application port (8080) become available ...
Application 'squadstream' failed to start (port 8080 not available)
-------------------------
Git Post-Receive Result: failure
Activation status: failure
Activation failed for the following gears:
568be5b67628e1805b0000f2 (Error activating gear: CLIENT_ERROR: Failed to
execute: 'control start' for /var/lib/openshift/568be5b67628e1805b0000f2/nodejs
#<IO:0x0000000082d2a0>
#<IO:0x0000000082d228>
)
Deployment completed with status: failure
postreceive failed
I have my git repo set up with all the steps followed properly.
https://github.com/ammark47/SquadStreamServer
Edit: I have another app on openshift that is on 8080. I'm not sure if that makes a difference.
If the other application is running on the same gear, then it is binding to port 8080 first, making it unavailable for your second application. You will need to run each application on it's own gear. Also, you need to make sure that you are binding to port 8080 on the correct IP address for your gear, you can't bind to 0.0.0.0 or 127.0.0.1

Restarted my server and started getting this error: Forever detected script exited with code: 8

I resized my Digital ocean droplet (Permanently) This required I powered off my droplet and then powered on again. When I visit the webpage I get a typical NGINX page saying that I have succesfully installed NGINX, the web app can no longer be seen
I did a mup logs -f to see what is going on and I am continually getting this error.
I am not sure what is wrong and it looks like something is off with my cron jobs but I am not sure what. Any ideas:
error: Forever detected script exited with code: 8
[xxx.xxx.xxx.xx] error: Script restart attempt #75[xxx.xxx.xxx.xx]
[xxx.xxx.xxx.xx] {"line":"63","file":"synced-cron-server.js","message":"SyncedCron: Scheduled \"Email Weekly Todos for Mentors\" next run #Mon Nov 30 2015 07:00:00 GMT-0500 (EST)","time":{"$date":1448741207434},"level":"info"}[xxx.xxx.xxx.xx]
[xxx.xxx.xxx.xx] {"line":"63","file":"synced-cron-server.js","message":"SyncedCron: Scheduled \"Weekly Push Notifications to students\" next run #Sun Nov 29 2015 10:00:00 GMT-0500 (EST)","time":{"$date":1448741207437},"level":"info"}[xxx.xxx.xxx.xx]
[xxx.xxx.xxx.xx]
[xxx.xxx.xxx.xx] events.js:72[xxx.xxx.xxx.xx]
[xxx.xxx.xxx.xx] throw er; // Unhandled 'error' event
[xxx.xxx.xxx.xx] [xxx.xxx.xxx.xx] ^
Error: listen EADDRINUSE
at errnoException (net.js:905:11)
at Server._listen2 (net.js:1043:14)
at listen (net.js:1065:10)
at net.js:1147:9
at dns.js:72:18
at process._tickCallback (node.js:442:13)
error: Forever detected script exited with code: 8[xxx.xxx.xxx.xx]
Any ideas would help a lot
I fixed the issues by setting the app file in sites-enabled folder to listen to port 3000. I think it was listening to 80 by default and conflicting with nginx

OpenShift socket.io : express deprecated app.configure: Check app.get('env')

After setting client and server side of socket.io, what I think are the right links, now something else sprung up.
My assumption is - I am using an old express version or one that is too new for the code I have?
DEBUG: Program node server.js exited with code 0
DEBUG: Starting child process with 'node server.js' Tue, 29 Jul 2014 13:51:04 GMT express deprecated app.configure: Check app.get('env') in an if statement at server.js:11:5 info: socket.io started warn: error raised: Error: listen EACCES
DEBUG: Program node server.js exited with code 0
DEBUG: Starting child process with 'node server.js' Tue, 29 Jul 2014 13:51:06 GMT express deprecated app.configure: Check app.get('env') in an if statement at server.js:11:5 info: socket.io started warn: error raised: Error: listen EACCES
Any guidance?
For the deprecated function app.configure just use app.use without using app.configure, and for the error Error: listen EACCES check if you have anything listening in the port you are trying to start your server.js

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)

Resources