How to Keep a persistent http-server open on AWS instance - node.js

I set up a AWS Ubuntu instance running a http-server using node.js
I was wondering if its possible to log out of my remote server while persistently keeping the http-server on.

This is a pretty good tutorial that deals with keeping a node.js server running, and amongst other things, deals with running it in the background.
http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/

Forever is a nice option (as suggested above).
Though, I recommend using AWS' Elastic Beanstalk over EC2 (that's the service you are using now, if I got it right), it provides you an easy interface to deploy you web-server with no ssh interference and keeps it alive at all times after deployment, and also gives you some other load balancing and auto scaling features with minimum effort.

You could also use pm2 for this. Besides keeping your http-server online it also gives you the possibility to do load balancing and other tasks.
Run
npm install pm2 -g
on your server and start your app with
pm2 start app.js
As marekful points out, logging out of your Ubuntu server will not have any effect on your http-server.

Related

Running NodeJS server in production

I have a react + node app which I need to deploy. I am using nginx to serve my front end but I am not sure what to use to keep my nodejs server running in production.
The project is hosted on a windows VM. I cannot use pm2 due to license issues. I have no idea if running the server using nodemon in production is good or not. I have never deployed an app in production, hence I have no idea about appropriate methods.
You may consider forever or supervisor.
Check this blog post on the same.
You can also use docker. You can create multiple docker containers that will run your node server. Now at the nginx level at your host machine you can do load balancing configuration which will route the traffic equally to different docker node containers this will improve your availability and scalability, In heavy traffic you just need to increase the number of docker node containers as and when required. I guess initially 2 containers will be enough to handle traffic (depends on your use case though).
Note:- You can also use forever or supervisor as suggested by #Rajesh Gupta inside your docker containers for running node server. We use PM2 for that.
If you have a database then you can create a separate docker container for the database and map it to a volume in your host machine.
You can learn about docker from here.
Also you can read about load balancing in nginx from here.
Further more to improve your availability you can add a caching layer in between nginx and docker containers. Varnish is the best caching service i have used till date.
PS:- We use a similar but more advanced architecture to run our Ecommerce application that generates 5-10k orders daily. So this is a tested approach with 0 downtime.
Try to dockerize the whole app including the db, caching server (if any) etc.
Here are some examples why:
You can launch a fully capable development environment on any
computer supporting Docker; you don't have to install libraries,
dependencies, download packages, mess with config files etc.
The working environment of the application remains consistent across
the whole workflow. This means the app runs exactly the same for
developer, tester, and client, be it on development, staging or
production server. In short, Docker is the counter-measure for the
age-old response in the software development: "Strange, it works for
me!"
Every application requires a specific working environment: pre-installed applications, dependencies, data bases, everything in specific version. Docker containers allow you to create such environments. Contrary to VM, however, the container doesn't hold the whole operating system—just applications, dependencies, and configuration. This makes Docker containers much lighter and faster than regular VM's.

How can i access nodejs terminal in production to view run time errors

I want to access node app terminal in production if there is a way, as in development we can access terminal to see activities, events and log messages, but in production(cPanel) i did not find such tool which i can use to view runtime error to solve any error in the code.
If you know any solution regard this problem, please answer me, i am looking very seriously at it.Thanks, any help will be appriciated
You're welcome 🙏🏼
Shared hosts do not have access to the terminal at all
You have to buy the virtual private server (VPS) so you can run your nodejs app and access terminal.
Use pm2 for production nodejs app.
npm i pm2 -g
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Elastic Beanstalk Node.Js needs PM2 or Forever

In my local node server , it fails on any uncaught exception and has to be manually restarted. So local was run using either forever or pm2.
But my development server is AWS Elastic Beanstalk with Node.Js environment .Though it logs uncaught exceptions and throws it , it never terminates.Even if it terminates , it starts automatically.
So is there a need for PM2 or Forever to keep node.js running for ever in AWS Elastic Beanstalk environment.
My 2 cents:
There are many advantages for PM2 over EBS:
with PM2 you can control and manage many applications (processes) with different platforms (JS, Ruby, ...).
Where in EBS you can deploy only one application at a time to an EBS.
PM2 gives you direct control over all running process, where you can reload/restart/stop one process and keep other intact.
EBS can fix only few problems for you, like restart on crash, or limit memory leaks. But PM2 gives you control over many other problems (graceful reload, 0 latency reload, ...)
Now, I might be a little bit wrong about the full functionalities of EBS, and it might be providing more
Here is a tutorial on how to use PM2 in EBS:
http://pm2.keymetrics.io/docs/tutorials/use-pm2-with-aws-elastic-beanstalk/
Hope it helps!

Deploy node.js in production

What are the best practices for deploying a nodejs application in production?
I would like to know how deploy for production Api's nodejs is being done today, today my application is in docker and running locally.
I wonder if I should use a Nginx inside the container and deploy my server on it or just upload my image node that is already running today.
*I need load balance
There are few main types of deployment that are popular today.
Using platform as a service like Heroku
Using a VPS like AWS, Digital Ocean etc.
Using a dedicated server
This list is in the order of growing difficulty and control. So it's easiest with PaaS but you get more control with a dedicated server - thought it gets significantly more difficult, especially when you need to scale out and build clusters.
See this answer for more details on how to install Node on a VPS or a dedicated server:
how to run node js on dedicated server?
I can only add from experience on AWS using a NAT Gateway which is a dedicated Node server with a MongoDB server behind the gateway. (Obviously this is a scalable system and project.)
With or without Docker, you need to control the production environment. This means clearly defining which NPM libraries you will need for production, how you handle environment variables and clusters for cores.
I would suggest, very strongly, using a tool like PM2 to handle clusters, server shutdowns and restarts and logs. (Workers & slaves also if you need them and code for them).
This list can go on and on, but keep in mind this is only from an AWS perspective. Setting up a Gateway correctly on AWS is also not an easy process. Be prepared for some gotcha's along the way.

Keep node in running state even after user log-off

How to keep a node application running in windows even when user logs off?
Also how to keep running a node http-server even after user log-off?
You have 2 great options. One is as mentioned in comments above Forever.
The other is PM2 which is easy to install and offers an incredible amount of options. I use this in all projects, but I cannot attest to the Windows version as I am on Linux & Ubuntu servers and work on a Mac. You can daemonize your node process, follow logs, cluster it and make sure the process reboots even with a server shutdown (it is a service).
windows task scheduler: execute node.exe: start in project folder: and argument (app.js)

Resources