My environment is ubuntu 16.04 with npm 5.0.0 and node 8.0.0 installed. I created an express application and started it with nohup npm start & in the application root directory. However, a couple of minutes later the app became unreachable and the process was lost. I tried restarting several time but the process always automatically exits. How can I start a long-running nodejs app?
Use pm2 daemon for setting Up a Node.js Application for Production on Ubuntu 16.04
Follow commands
sudo npm install -g pm2
pm2 start server.js //Yor main node server.js
pm2 stop server //no need of giving .js , .js only required at start of process
pm2 restart server //for any changes are done to restart
For more You can follow digital ocean tutorials for more details from below link
Digital_Ocean_Link
Related
I'm very new to server related stuff, but I'm trying to have a node.js server start automatically in a TrueNAS jail upon booting up (instead of starting the server manually through the terminal).
The most promising thing I could find is adding some kind of script to run at startup in the /etc/rc.d directory, but I couldn't find any specific information on running a node.js server from it. Sorry if that doesn't make any sense at all.
M'kay I found a solution. For anyone else who's curious there's an npm package called pm2 that does exactly what I was trying to do. Here's how I got it working:
Install pm2: npm install pm2 -g
Make pm2 run on startup: pm2 startup
If you get an error that says Faliure when trying to write startup script then enter the command: mkdir /usr/local/etc/rc.d to create the directory it's looking for, then run pm2 startup again and it should work
Then start your node.js app: pm2 start app.js
Finally run pm2 save to automatically start your app whenever the jail boots up
I'm deploying my node express project with iis. pm2 don't restart after then server rebooting. can I npm start or pm2 start on webconfig file? what should I do?
when you deploy your project use at the end
pm2 save
pm2 startup
based on documentation
pm2 Save
Once you started all the applications you want to manage, you have to save the list you wanna respawn at machine reboot
Pm2 Startup
PM2 can generate startup scripts and configure them in order to keep your process list intact across expected or unexpected machine restarts
to run the node js application after the user logged out you could try to use Forever and or install forever-win using npm -g install forever-win and do then forever start app.js.
I am new for Nodejs. I have developed an application using Expressjs. Now I want to deploy my app to the server and I already bought a server from digitalocean.
I don't have any idea that how to deploy my app via PM2.
Please help..
if you have installed nodejs and pm2 on your server. you just need to run pm2 with your express js application instance.
pm2 start bin/www
You can find your express js application instance in the package.json file. see in the screenshot below.
after that, you can run the command to check the running instance of the application.
pm2 ls
After you install nodejs to your server, first you need to install pm2 globally via npm:
sudo npm install pm2 -g
Then, just run the start command:
pm2 start "/path/app.js"
Thats it. You can see the project with: pm2 status
For more pm2 code I suggest you to take a look at https://www.npmjs.com/package/pm2
See directly the PM2 deployment page : http://pm2.keymetrics.io/docs/usage/deployment/
You just set the configuration of your server host,user,git etc... and pm2 will deploy it remotly automatically
I know I can run Node.js on server with command line node app.js.
But when I am out of control server, the session will be close and end my command. I don't know how to make a service run Node.js 24/7 like another in Server.
I follow this post, but I'm not using express.
run it using forever , it helps the server to restart whenever the node server get crashed. https://www.npmjs.com/package/forever
You can make it happen by many ways.
You can append & in the command line to make the node server run in background.
node app.js > stdout.txt > stderr.txt &
Via process manager pm2, It gives more features, you can monitor all the processes pm2 monit, auto restart, etc
npm install pm2 -g
pm2 start app.js
or, using following npm packages
Nodemon - nodemon app.js
Forever - forever start app.js.
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.