How to install forever command without sudo - node.js

I have a node server on a VPS through Dreamhost. I want to run my node process forever. Currently, I can run the process but when I close my terminal it ends the process and the website no longer works. What I have read online is that I need to do forever node app.js but I am not given sudo access and can not install the command forever globally. How can I either install forever without sudo access or keep my node process running even after I close my terminal?

You should be able to activate Passenger on your VPS and have that manage your nodejs. Details on DreamHost KB https://help.dreamhost.com/hc/en-us/articles/215769578-Passenger-overview

Related

Deploying my node.js app from Github to a VPS

I have an node.js application on Github. I have never done any VPS deployment before and I am learning on the go.
I am using the VPS by Hostinger.in, the OS being used is Ubuntu 14.04. So far this is what I have done:
Connected to their SSH successfully from my Terminal
Installed node.js on the server [https://www.hostinger.com/tutorials/vps/how-to-install-node-js-on-ubuntu]
Installed Git on the server [https://www.hostinger.com/tutorials/how-to-install-git-on-ubuntu]
I could not find any online resources for deploying my node.js to Hostinger VPS so I am following the ones written for DigitialOcean.
The one tutorial I followed is this: https://code.tutsplus.com/tutorials/setting-up-continuous-integration-continuous-deployment-with-jenkins--cms-21511
I cloned my repository doing:
git clone https://github.com/myusername/node-project.git
and it seems it was deployed (didnt give me any errors).
All the installations I did on the server I did as the root/admin user. So far I have not created any separate user to perform any of these tasks.
The server hostname given to me is dangerous-pigs.com. Now I am assuming my node.js application is deployed, but when I go to dangerous-pigs.com it shows me server not found error.
I also installed forever for my node app and when I run
forever start app.js
it says:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: app.js
error: Cannot start forever
error: script /root/app.js does not exist.
Which means the app is either not installed or installed somewhere other than the root folder.
There is a lot going on and I am confused where to start fixing issues.
How can I deploy the app to running it on the dangerous-pigs.com?
Update
So it seems I have to go inside the project folder in root and do the
npm install --production
after which I did
node app.js
The server seems to be running but, I can only access my application if I do to the actual IP provided by the service.
So if I type http://93.188.163.249:8000 --> that's my application.
How do I change it to point to a domain?
After some more research this is what I found:
Currently by default Apache2 runs on port 80. To run nodejs on port 80 first I need to install libcap2-bin in my Ubuntu server by doing:
sudo apt-get install libcap2-bin
after which I do
sudo setcap cap_net_bind_service=+ep /usr/bin/nodejs
the above command works if you have a mac, for windows the command maybe
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node
but please confirm before doing it.
Also your nodejs server needs to be stopped before you make these changes else it will not work. In my case I had forgotten to stop my node server and kept running the sudo setcap command but it did not changed the port (for obvious reasons).
If you are using forever to run node then do:
forever stopall

Permanent socket.io server on Ubuntu Linux

I'm starting a socket.io server by entering a command:
node server.js
But that sometimes stops, or I need to have a terminal window open for it to run.
How can I set this up on a Linux server (Ubuntu) so there is a permanent server running in the system (like Apache) and if it stops it restarts automatically?
You can use PM2
after install the npm package you can use pm2 command line :
pm2 start server.js
You can use too nodemon or forever to detect when your server files changed. It will restart automatically your server and you don't need anymore to stop / start your node application.
Note than pm2 is used for production and nodemon for development
You can also use tmux (no need for any installations) by writing the following in the command line:
tmux
cd /path/to/application
node server.js
To exit the session while keeping the application running use:
Ctrl+b
d
You can also use upstarter and turn your node application into an ubuntu service. That's the thing I use in production.
To install:
npm install -g upstarter
To use:
sudo upstarter
And the remainder is just user prompts. In order to start/stop/restart your upstarter-generated service:
sudo start/stop/restart <name-of-the-service>
Upstarter also has this one big advantage over PM2/Forever: It can be used with non-node applications.

nodejs 'forever' just doesn't do anything

I am trying to run my nodejs app on an Amazon EC2 instance using forever. I installed nodejs npm, and then ran sudo npm install forever -g.
The installation didn't return any errors, so i go ahead and try running my app using forever start server.js, but it won't do anything - no output what-so-ever, not even an error.
I also tried forever --help and just forever, but none of them giving me any response...
When running my app regularly using nodejs - nodejs init.js then it works as expected, but i need to get it running using forever so it won't shut down when i disconnect from the server.
Edit :
Since the only problem i was having was that nodejs was closing when i closed the terminal session to my EC2 server, I solved this by using the linux nohup command like this :
nohup sudo nodejs server.js &
This kept nodejs running in a child process even after I closed the terminal window.
Thanks for the help though! :)
I was also not receiving any stdout input from any forever command and this fix nailed it:
sudo ln -s /usr/bin/nodejs /usr/local/bin/node
Hope that helps someone.
I don't know if this will help, but as an alternative, you can use upstart. Create a script like this:
#this is the upstart service task. move it to /etc/init/nodeapp.conf
description "server for Node App"
author "Neels Grobler"
chdir /root/servers/nodeapp
exec node init.js 1>stdout.log 2>stderr.log
respawn
And run it by typing start nodeapp. To stop type stop nodeapp. You can also change the script so that the node app starts on system boot etc. More info at upstart.ubuntu.com
You should be using forever start server.js not just forever server.js
Several guesses.
If you install using sudo npm install forever -g, you might need to sudo to run forever.
Put a full path of server.js when you run forever such as forever start /home/you/source/server.js or sudo forever start /home/you/source/server.js
On windows, when you run forever like:
forever start app.js
you can find generated log file in file system at:
C:\Users\USERNAME\.forever\SomeLogFileHere.txt
The log files are regenerated for every running script with unique identicator of each file. You can always check which file belongs to which process by:
forever list

How to make a node.js application run permanently?

On a Debian server, I installed Node.js. I understand how to launch an app from putty with this command line:
node /srv/www/MyUserAccount/server/server.js
and get to it on the address 50.51.52.53:8080 (IP and port).
But as soon as I close putty, then I cannot reach the address 50.51.52.53:8080 anymore.
How to make a Node.js application run permanently?
As you can guess, I am a beginner with Linux and Node.js.
You could install forever using npm like this:
sudo npm install -g forever
And then start your application with:
forever server.js
Or as a service:
forever start server.js
Forever restarts your app when it crashes or stops for some reason. To restrict restarts to 5 you could use:
forever -m5 server.js
To list all running processes:
forever list
Note the integer in the brackets and use it as following to stop a process:
forever stop 0
Restarting a running process goes:
forever restart 0
If you're working on your application file, you can use the -w parameter to restart automatically whenever your server.js file changes:
forever -w server.js
Although the other answers solve the OP's problem, they are all overkill and do not explain why he or she is experiencing this issue.
The key is this line, "I close putty, then I cannot reach the address"
When you are logged into your remote host on Putty you have started an SSH linux process and all commands typed from that SSH session will be executed as children of said process.
Your problem is that when you close Putty you are exiting the SSH session which kills that process and any active child processes. When you close putty you inadvertently kill your server because you ran it in the foreground. To avoid this behavior run the server in the background by appending & to your command:
node /srv/www/MyUserAccount/server/server.js &
The problem here is a lack of linux knowledge and not a question about node. For some more info check out: http://linuxconfig.org/understanding-foreground-and-background-linux-processes
UPDATE:
As others have mentioned, the node server may still die when exiting the terminal. A common gotcha I have come across is that even though the node process is running in bg, it's stdout and stderr is still pointed at the terminal. This means that if the node server writes to console.log or console.error it will receive a broken pipe error and crash. This can be avoided by piping the output of your process:
node /srv/www/MyUserAccount/server/server.js > stdout.txt 2> stderr.txt &
If the problem persists then you should look into things like tmux or nohup, which are still more robust than node specific solutions, because they can be used to run all types of processes (databases, logging services, other languages).
A common mistake that could cause the server to exit is that after running the nohup node your_path/server.js & you simply close the Putty terminal by a simple click. You should use exit command instead, then your node server will be up and running.
You can use PM2, it's a production process manager for Node.js applications with a built-in load balancer.
Install PM2
$ npm install pm2 -g
Start an application
$ pm2 start app.js
If you using express then you can start your app like
pm2 start ./bin/www --name="app"
Listing all running processes:
$ pm2 list
It will list all process. You can then stop / restart your service by using ID or Name of the app with following command.
$ pm2 stop all
$ pm2 stop 0
$ pm2 restart all
To display logs
$ pm2 logs ['all'|app_name|app_id]
I'd recommend looking for something such as Forever to restart Node in the event of a crash, and handle daemonizing this for you.
If you just want to run your node app in the terminal always, just use screen.
Install on ubuntu/ debian:
sudo apt-get install screen
Usage:
$ screen
$ node /path/to/app.js
ctrl + a and then ctrl + d to dismiss
To get is back:
One screen: screen -r
If there's more than one you can list all the screens with: screen -ls
And then: screen -r pid_number
You could simply use this
nohup node /srv/www/MyUserAccount/server/server.js &
This will keep the application running and to shut it down you will have to kill it.
For that you could install htop and then search for node and then kill it
Forever is a very good NodeJs module to do exactly that.
Install forever by typing in the command line
$ npm install forever -g
Then use the following command to run a node.js script
$ forever start /path/to/script.js
You are good to go. Additionally you can run
$ forever list
to see all the running scripts. You can terminate any specific script by typing
$ forever stop [pid]
where [pid] is the process ID of the script you will obtain from the list command. To stop all scripts, you may type
$ forever stopall
Installation
$ [sudo] npm install forever -g
You can use forever to run scripts continuously
forever start server.js
forever list
for stop service
forever stop server.js
During development, I recommend using nodemon. It will restart your server whenever a file changes. As others have pointed out, Forever is an option but in production, it all depends on the platform you are using.
You will typically want to use the operating system's recommended way of keeping services up (e.g. http://www.freedesktop.org/wiki/Software/systemd/).
nohup working i checked in AWS Ubunto vm follow the correct syntax
ubuntu#ip-172-00-00-00:~/ms$ nohup node server.js &
then press enter you will see this line
ubuntu#ip-172-00-00-00:~/ms$ nohup: ignoring input and appending output to ‘nohup.out’
then type this
rm nohup.out
Here's an upstart solution I've been using for my personal projects:
Place it in /etc/init/node_app_daemon.conf:
description "Node.js Daemon"
author "Adam Eberlin"
stop on shutdown
respawn
respawn limit 3 15
script
export APP_HOME="/srv/www/MyUserAccount/server"
cd $APP_HOME
exec sudo -u user /usr/bin/node server.js
end script
This will also handle respawning your application in the event that it crashes. It will give up attempts to respawn your application if it crashes 3 or more times in less than 15 seconds.
First install pm2 globally
npm install -g pm2
then start
pm2 start bin/www
I’ve found forever to do the job perfectly fine.
Assuming you already have npm installed, if not, just do
sudo apt-get install npm
Then install forever
npm install forever --global
Now you can run it like this
forever start app.js
https://codingweb.io/run-nodejs-application-background/
No need to install any other package.
Run this command
node server.js > stdout.txt 2> stderr.txt &
server.js is your server file or it can be api.js
After that hit "exit" to close terminal
exit
Another way is creating a system unit for your app. create a "XXX.service" file in "/etc/systemd/system" folder, similar to this:
[Unit]
Description=swagger
After=network.target
[Service]
ExecStart=/usr/bin/http-server /home/swagger/swagger-editor &
WorkingDirectory=/home/swagger
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
A benefit is the app will run as a service, it automatically restarts if it crashed.
You can also use sytemctl to manage it:
systemctl start XXX to start the service, systemctl stop XXX to stop it and systemctl enable XXX to automatically start the app when system boots.
Try pm2 to make your application run forever.
npm install -g pm2
and then use
pm2 start server.js
to list and stop apps, use commnds
pm2 list
pm2 stop 0
I hope this will help you.
At the command line, install forever:
npm install forever -g
Create an example file:
sudo nano server.js
You can edit the file and get results directly in your browser.
You can use filezilla or any editor to edit the file.
Run this command to run the file:
forever start --minUptime 1 --spinSleepTime 1000 -w server.js
forever package worked for me, just one thing, it depends on deep-equal, so if you had issue with installing it like:
npm -g install forever
Try:
npm -g install forever deep-equal#1.1.1
instead.
As we know that there are many options to do this. Here is a pure Shell solution, with no need for extra programs / packages.
This solution will restart server.js if it crashes for some reason / errors.
Let's say this is a run.sh:
#!/usr/bin/env sh
while :; do
node server.js
echo "Restarting..."
sleep 1
done
Make sure to make the run.sh file executable:
chmod +x run.sh
And to run it:
./run.sh
If you want to run it in the background:
./run.sh &
Run in the background super-silently (detached, without any output):
( ./run.sh > /dev/null 2>&1 & )
I recommend use PM2, which is a process manager for Node.js applications. PM2 provides an easy way to manage and daemonize applications (run them as a service).
refer this link - https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-centos-7

when should I run node.js with forever?

I'm writing an express app in node. I'm trying to crash it but can't... whenever I throw exceptions the app is still running (even when no error handler is configured). I'm using express-resource too if that's related and also streamline.js.
When will something like forever be usefull to me? Is it only in more serious crash like system out of resources?
Forever is useful on development stage. When you hard work on youre code and don't want to handly restart node app. In production it can save your live, when you meet some unexpected error and all project crash ;)
Installation
$ [sudo] npm install forever -g
Note: If you are using forever programmatically you should install forever-monitor.
$ cd /path/to/your/project
$ [sudo] npm install forever-monitor
Usage
There are two ways to use forever: through the command line or by using forever in your code. Note: If you are using forever programatically you should install forever-monitor.
Command Line Usage
You can use forever to run scripts continuously (whether it is written in node.js or not).
Example
forever start app.js

Resources