Anytime you run a program in your computer, it can fail for many reasons: maybe a server that it depends on is turned off or, even worst, an unhandled exception can tear down the running process. That's terrible for production applications as you leave your users without the server until you notice that it is not working.
This is where the process manager comes in, you can run your code behind a process manager and it will ensure that the process is always running. If something fails and crashes the program, the process manager will reset the entire application automatically.
There are two popular process managers for Node: forever
and pm2
, both work in a similar way; however, pm2
seems to be more popular and provides more utility tools than ever. For this reason, we will use pm2
for the stack.
Ubuntu comes with an integrated process manager known as Upstart. You can use the operative system process manager; however, pm2
is focused on node applications, it allows you to run more than an instance of your process instead of running a single process.
You can install pm2
with the npm
tool, do not forget to install it as global package:
$ npm install -g pm2
After the installation is complete, you can run your process behind pm2
with the start
command:
$ pm2 start app.js [PM2] Starting app in fork_mode (1 instance) [PM2] Done. ┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐ │ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │ ├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤ │ app │ 0 │ fork │ 16427 │ online │ 0 │ 0s │ 15.801 MB │ disabled │ └──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘ Use `pm2 show <id|name>` to get more details about an app
After you do this, the script is running and you can see the output of the process with the logs command and the application ID:
$ pm2 logs 0 [PM2] Tailing last 20 lines for [0] process app-0 (out): Server running app-0 (out): GET / 200 5.911 ms - 8908 app-0 (out): GET /css/vendor.css 200 2.740 ms - 131 app-0 (out): GET /js/app.js 200 3.821 ms - 396191 app-0 (out): GET /api/contacts 200 2.528 ms - 55 [PM2] Streaming realtime logs for [0] process
Instead of using the raw ID of the process, you can name your running processes:
$ pm2 start app.js --name app
You can also run more than one instance so that you can have two running instances of the same application and pm2
will load and balance requests between them:
$ pm2 start app.js --name app –i 2
It's a good idea to run more than one instance of a Node server as the Node blocks is making I/O operations. If you run more than one instance, then the other processes can continue serving the incoming requests while the other is blocked.
You can save the application parameters in a JSON file and use it instead of putting all the options in the command line:
{ "apps": [{ "name": "contacts-app", "script": "/path/to/the/application/app.js", "cwd": "/path/to/the/application", "watch": false, "instances": 2, "error_file": "/path/to/your/home/contacts-app/app-err.log", "out_file": "/path/to/your/home/app-out.log", "pid_file": "//path/to/your/home/app.pid" "env": { "NODE_ENV": "production" } }] }
With the JSON file, you have the advantage that you don't have to remember how to run the application as the file contains all the required configurations and the same environment is easily reproducible on different hosts.
If you are fine with your settings and everything is working as you expected, the next step is to persist the pm2
process to run it as daemon each time that the server is restarted; this is always a good idea as if the server restarts for some reason such as maintenance, your processes will spawn automatically.
Fortunately, pm2
provides an easy way to daemonize your configuration with the startup
command for many operative systems, as follows:
$ pm2 startup -h Usage: startup [options] [platform] auto resurrect process at startup. [platform] = ubuntu, centos, redhat, gentoo, systemd, darwin, amazon $ sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u production [PM2] Generating system init script in /etc/init.d/pm2-init.sh [PM2] Making script booting at startup... [PM2] -ubuntu- Using the command: su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults" System start/stop links for /etc/init.d/pm2-init.sh already exist. [PM2] Done.
The first command shows the available operative systems. As the startup
command writes on the /etc/
path, we need to run this command as the root user and that's the reason why we are using sudo command.
To run the daemon, you need to run the following:
$ service pm2-init.sh start
However, before running this command, you will need to dump your current configuration in the daemon configuration and if you skip this step, the service will not start any process:
$ pm2 start process.json $ pm2 save [PM2] Dumping processes
With this command, the current configuration of pm2
will be used every time the server restarts or you manually restart the service.
That's how you can run your node application in a production environment; run a real HTTP server and run your node processes behind it with the help of a process manager such as pm2
.
18.223.196.146