Running your node & express apps on your local machine is easy. However, once you put them in production there are some new problems.
What if the app crashes? What if the server restarts? What if some other failure happens? You need your apps to be running forever, no matter what happens.
Unfortunately applications crash in unexpected ways and servers restart due to bugs or power or hardware failures. In all cases, you need to ensure that your app can return to operating normally, all by itself.
Here in this post, we aim to help you setup up an Ubuntu server to run Node.js applications, including apps based on Express. These instructions will help you avoid some security mistakes, as well as provide some surprising benefits such as:
What if the app crashes? What if the server restarts? What if some other failure happens? You need your apps to be running forever, no matter what happens.
Unfortunately applications crash in unexpected ways and servers restart due to bugs or power or hardware failures. In all cases, you need to ensure that your app can return to operating normally, all by itself.
Here in this post, we aim to help you setup up an Ubuntu server to run Node.js applications, including apps based on Express. These instructions will help you avoid some security mistakes, as well as provide some surprising benefits such as:
- You will not run your app as root; therefore, your app will be more secure.
- Your application will restart if it crashes, and it will keep a log of unhandled exceptions.
- our application will restart when the server starts - i.e. it will run as a service.
1. forever npm
This is a simple CLI tool for ensuring that a given script runs continuously.sudo npm install -g forever
and then start your application with:
Forever restarts your app when it crashes or stops for some reason.
To restrict restarts to 5 you could use:
forever server.js
or as a service:
forever start server.js
To restrict restarts to 5 you could use:
forever -m5 server.jsTo list all running processes:
forever listNote the integer in the brackets and use it as following to stop a process:
forever stop 0Restarting a running process goes:
forever restart 0If you're working on your application file, you can use the
-w
the parameter to restart automatically whenever your server.js
file changes:forever -w server.js
2. Run your app using PM2, and ensure that your node.js application starts automatically when your server restarts.
You can use PM2, it's a production process manager for Node.js applications with a built-in load balancer. Install PM2npm install pm2 -gStart an application
pm2 start app.jsIf you using express then you can start your app like
pm2 start ./bin/www --name="app"Listing all running processes:
pm2 listIt will list all processes. You can then stop/restart your service by using the ID or Name of the app with the following command.
pm2 stop all
pm2 stop 0
pm2 restart all
To display logs
pm2 logs ['all'|app_name|app_id]
3. You could simply use "nohup"
You can use nohup and supervisor to make your node app run in the background even after you log out. This will keep the application running and to shut it down you will have to kill it. For that, you could install and then searchhtop
for node and then kill itFor Linux Use terminal and enter in superuser mode, and try these code.
nohup node <location of of js file> & exitNote: This '&' is just before you press enter or for npm command, just go to the location by cd command where your package.json is stored. Then
nohup npm start & exit
Note: This '&' is just before you press enter To stop it
topYou can see process id here, then use following code
kill -9 <PROCESS_ID>