How to run node servers in production with an Infomaniak VPS
Infomaniak is one of the best hosting provider in Switzerland. They suggest to run node servers with
forever starts to have some issues with newer versions of node. In fact at the time of writing it has not been updated for more than 10 months. In this small tutorial I want to show the setup I use in production to run node servers in production (clustered and non-clustered servers), how to monitor them, and how to restart everything if the VPS restarts.
Pm2 is an “advanced, production process manager for Node.js”. It let’s start processes “forever”. Node.js being single threaded a single un-handled error will crash the process. The process needs to be restarted. Typically you would run:
npm start or
npm start myapp.js
This is where
forever for that matter) kicks in.
pm2 will restart the process every time it is killed, thus insuring that your app is always running.
Pm2 comes with many added advantages over forever. Most notably monitoring, cluster mode, logs management, startup scripts, etc. There are many more features that are well documented.
Pm2 at Infomaniak
For now (On its FAQ Infomaniak promisses a better way soon), log into your server by SSH then run the following command:
npm install pm2 -g
then start your app:
pm2 start myapp.js
So far so good. Your app should be running you can watch logs etc. If your app crashes it will restart. This environment works well with the latest node versions.
With pm2 you can go further, you can monitor and manage your server remotely. This means that remotely you can send commands to your server and you can:
- roll back some code (with git)
- restart your server
- monitor your app, db(mongo, redis, etc.)
This is possible but requires some configuration, first you need to open an account at Keymetrics, that is free, then you need to connect pm2 with your dashboard. Recuperate your keys from your keymetrics dashboard and from your server run the following:
$ pm2 link [private_key] [public_key]
That is as easy as that…You should now see something similar in your dashboard:
But to gain all the features you need to give access to your server to Keymetrics. Fortunately this is not complicated. In the admin console of your server, go to the hosting where your app is hosted, the in the left hand side menu, choose
tools > opening ports . Click on the
adding a port button and add an entry for the following IPs for the port
When this is set up, the dashboard can control your node.js app.
But what happens if for some reason the Infomaniak servers restarts? There are planned maintenances, for those Infomaniak sends email notifications, but sometimes the servers restarts, and by experience, we have seen this happen once every 2 weeks…
I manage to make this work by modifying the infomaniak proposed .sh script, that is called every minute by a cron. I have tried “cleaner” ways of doing this, but rights are quite limited on the managed VPS.
Here is what I did following the Infomaniak FAQ. Create a start script:
touch start.sh // you can name this as you wish…
vi/vim start.sh add the following:
I source my
.bashrc file as it automatically sets some default, I then make sure I use the version of node that I want to run my apps with, I manage two apps and I am checking individually that they are running. If they run, I do nothing. This is done by running the command
pm2 list and pipping it to grep to check if the API or the Assets are present in the list.
I am then going to the folder and starting the servers. I have found that to be more reliable, I have tried with absolute paths, relatives ones, etc, but it did not work as well.
I am starting the apps with some flags:
-i 4is uses the cluster mode, the the API uses 4 processes
- the name flags allows to give a name to your process which makes it easier to identify the process with the monitoring and the name is used in the condition
- the colors flag, is just to have nice logs
The script does two more things:
- it writes a line to a log file
- it sends an email when the server restarts
Now we need to create the cron:
and to add our script to it:
* * * * * /home/clients/<client id>/start.sh 1 >/dev/null
This script will run every minute, do nothing, or restart the servers if they are not running.
That is it! Now you can run forever your apps on Infomaniak, monitor them, control them remotely and get notifications when your server restarted. Let see now how Infomaniak plans to simplify this process!