How to make my server available with "forever start server.js" - node.js

I have spent multiple hours to try and get my server up and running. As a part of a class assignment we are migrating to ExpressJS. So the instruction go as follows:
Install latest version of nodejs and npm as well as express, morgan,and forever in our ssh server(Ubuntu based).
In our "website" folder we created a "public" folder to throw all web content in.
We then downloaded a server.js script the professor uploaded and we are to place it inside the "website" folder we have.
Then we set the path to the "website" folder. To be clear "website" contains the "public" folder and server.js script.
Write `forever start server.js` in the terminal
Forever processes server.js , but I do not get my website loaded,no matter the browser I access through(the terminal is also open). I tried writing forever list and all processes are stopped. I think the issue might be around here.
I have tried using:
forever start ./bin/www -did not work
sudo forever start server.js-did not work
sudo nohup forever start server.js-did not work
nohup forever start server.js-did not work
I really need to figure how to get this server up and running. If you need something from me let me know what and how to get it, please. I am a windows user and more than half of these commands I found researching other questions. If you need logs, or images also, please let me know where to find them to provide them to you since I have little technical knowledge for some of these things. - Thanks!

You can try nodemon for dev. or pm2 for production if you're trying to keep your server running.
https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/

Related

Do I need to manually restart my app on EC2 if the server restarts?

This is my first time with EC2 so keep that in mind. I spun an EC2 instance and put a really basic nodejs/express app up on it. I connected to the ec2 server via the terminal on my personal computer and ran node app.js to start the app and everything is running fine. The part I am confused about is how long this will run for. Ideally, I just want it to sit there and not touch it and have it run for hopefully years. Will it do this? If not what do I need to do? What if the server restarts for some reason? What is the common practice here?
Go to root directory of your project and type this command to run the server permanently.
sudo npm install forever -g
forever start -c "node app.js" ./
This blog may be helpful, in setting up node for production environments

Nodemon server perpetuality and runtime log issue

I have a sailsjs app on AWS EC2, which I have been running till now using forever. I have two adantages using forever:
1) Perpetuality: I can use the CLI forever start app.js or forever restart app.js and then app starts running and keeps on running till I stop it with the command forever stop app.js. So, the app does not stop even when I close my terminal. The process keeps on running.
2) Runtime Log: I have a .forever directory that has a log file, while on real time records the server logs, and when I check the log using tail -f file_name.log, I get to see run time logs.
However there is a disadvantage: Every time I upload a new/modified server file, I have to restart the app manually. To get rid of this, I am switching from forever to nodemon.
From the documentation provided by Nodemon, I cant figure out how can I replicate the two advantages, as mentioned above, from Nodemon too. Will be a great help if anyone can guide me on how to start my nodejs app using nodemon so that it can keep running even after closing the terminal on my side, and how to watch runtime log of server.
Just my two cents.
I use nodemon daily while developing and I dont think its something you want to use in place of something like forever. Nodemon is used when developing, the software will detect when there has been a file change and restart the server but for deployment it should not be considered.
There is no need to change either because forever has this use case handled with the --w or --watchDirectory comand that will watch for file changes(It can be found here on their readme).

nodejs server run on remote

I wasn't quite sure what to call this question but here i go:
i have a remote server where i have installed node.js now normally this would be how i start the server:
ssh root#ip
cd /var/www/mydomain/server
nodejs server.js
This works without any issues however what happens when i close down the terminal? How can i make sure that the server doesn't just stop. And how can i control it after i have started it (for instance restarting / stopping it).
There are plenty of solutions here, but maybe the most easy to start with is using forever.
Forever is a npm module that keep your app running and restarts it if it crashes.
Also there are more advanced solutions, like using PM2, which I recommend, but first take a look at forever.

How do I leave Node.js server on EC2 running forever?

As you can tell by my question, I'm new to this...
I built my first website, I set up my first Node.js server to serve it and then pushed everything live on EC2.
I tested everything on my EC2 IP address and everything seems to be working.
Now up until now, I've been testing my app locally so it makes sense that whenever I closed the terminal, app.js would stop running so nothing would be served on localhost.
Now that my server is on EC2, the same thing happens ("obviously" one could say..) whenever I close my terminal.
So my question is how do I keep my Node.js server running on EC2 for like... forever..so that my site stays live.. forever :)
I read something about a node module called "forever" but I'm wondering (being new and all..) why isn't this "forever" functionality a default setting of the Node.js-EC2 system ?
I mean, correct me if I'm wrong, but isn't the whole point of setting up a web server and pushing it live to have it stay live forever? Isn't that what servers are supposed to do anyway (infinitely listening for requests) ? And if that's the case why do we need extra modules/settings to achieve that ?
Thanks for your help.. As you can tell I'm not only looking for a solution but an explanation as well because I got really confused.. :-)
EDIT (a few details you might need) - After installing my app on EC2 these are the steps that I follow on the terminal (The app is running on Amazon Linux by the way) :
I type ssh -i xxxxxxxxxxx.pem ec2-user#ec2-xx-xx-xx-x.eu-west-1.compute.amazonaws.com on the
terminal
After logging onto the Amazon machine I then go to the relevant folder and execute node app.js
There are 3 folders in the machine : node, node_modules and *name of my app*
app.js resides in *name of my app*
After that, the site goes live on my EC2 IP
Once I close the terminal, everything is switched off
Before you invoke Node.js, run the command:
screen
This will create a persistent environment which will allow your process to keep running after you disconnect.
When you reconnect, you can use this command to reconnect to that environment:
screen -r
Here's a random link to learn more about screen:
http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
However, this won't help you if your EC2 instance restarts. There are many different ways to do that. Adding your startup command to /etc/rc.local is one way. Here's a link to an Amazon guide which includes adding something to /etc/rc.local.
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/building-shared-amis.html
I worked with the valid answer for a while but some times the screen just end with no reason also screen has no balance loader and others features that in a production enviroment you should care , Currently I use a npm component to do this job.
https://www.npmjs.com/package/pm2
This is so easy to use.
$ npm install pm2 -g
then just start your app with pm2 like this
$ pm2 start app.js
In the above link you can find diferents tasks to perform if you need.
Hope this help the newbies like me.
There's a better way. Use forever.js.
See it here: https://github.com/foreverjs/forever
This is a nice tutorial for how to use chkconfig with forever on CENTOS.
http://aronduby.com/starting-node-forever-scripts-at-boot-w-centos/
Or use tmux
Just Enter a tmux screen run node server
Ctrl+b Hit D and you're done.
I am very late to join the thread and seems its basic problem with every newbie. Follow the below to setup properly your first server.
follow the step on the ec2 instance(before doing this make sure you have a start script for pm2 in your package.json file):
npm install pm2 -g
pm2 startup systemd
See the output and at the last line it must be like..
You have to run this command as root. Execute the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup
systemd -u sammy --hp /home/sammy
Take the last line command and run again with root privilege.
(before running the next command, Provide a new start script for pm2 in your package.json file e.g: "pm2-start": "pm2 start ./bin/www")
npm run pm2-start
for more info follow the link.
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
If you are using a Ubuntu EC2, better to use the following we have been using this for the past 6 years and have had no issues with this.
sudo npm i -g forever
Now start your main, example
forever start index.js
forever start src/server.js
To stop the server use the following command
forever stop index.js
To list multiple servers running forever
forever listall

Getting 502 error in browser for a NodeJS app run with Forever

I have a SailsJS app set up on a Webfaction server. Everything works nicely (site can be accessed through browser, console works) when I run the app via any of the following commands, with and without the --prod param:
sails console,
sails lift,
node app.js
However, when I try to run the app with forever using forever app.js I get a 502 error, as if nodejs server isn't even running. When I run forever list I can see app.js listed among running processes.
How can I have my app run with forever?
Forever is considered outdated by many in the Node community, and thankfully, has been replaced by several other fantastic (dare I say, better) tools.
If you're running a newer flavor of Ubuntu, you can always install systemd and kick off the application that way. If you're seeking something more streamlined, Phusion Passenger might be your ticket. It has a long track record of successes, and I wouldn't hesitate to toss it into production.
I managed to solve this issue; the problem occurred due to SailsJS migration prompt which shows up when you start the server. Running app.js with forever worked, but the server didn't start because the script hanged waiting for a prompt reply. If you encounter this issue just make sure you have your migrate option set in model config to avoid running into migration prompt.

Resources