MalteseFalcon MalteseFalcon - 4 months ago 14
Node.js Question

Best way to deploy node api on linux server for production

By the past, I always used the Heroku platform to host and run any API I develop.
I recently decided to transfer the last one I am developping, on my Linux (Debian) dedicated server.

As it is more powerfull than any of my Heroku instances, I think I will increase the performances of my app.

Now I am looking for tools, or scripts, to deploy and run my app in background, without having to manually restart-it in case of crash or any problem.

This is basically what heroku does.

I am aware of the

thing, and also the
I am more looking for a "ready-to-use" and configurable solution, that would save my time.

So, what is the best way to deploy an app, and make sure it will always be running properly ?


First you need to have a persistent background service that will keep NodeJS processes (server instances) running, across crashes and reboots.

Pm2 is a popular node process manager. It's mostly operates via CLI:

npm -g install pm2
pm2 start --name my_server ./server.js
pm2 list
pm2 stop my_server
pm2 restart my_server
pm2 logs my_server

You can have it auto install init.d or upstart etc scripts with:

pm2 startup

So, it can restart across reboots of host machine.

Process management is one part of automation. The other is getting latest code on server host and restarting the server.

One way is to clone the code repository on server. Then every time you need to deploy, you'll need to do on host machine:

pm2 stop my_server
git pull
pm2 restart my_server

The other, more automated way is to setup a git server on host machine (GitLab), and automate restarts when code is pushed to project repository (Git Hooks : Post Receive). You can place pm2 restart script in post receive hook of hosted project repository.

With this, the deploy will be as simple as:

git push server branch

There are also more powerful solutions available, for eaxmple: Docker.