Recently, we experienced a crash of hard drive in one of our server. Thanks to the RAID, data weren't lost, but this incident was a great impetus to move our NodeJS-based chatbots to the VPS server of a reliable cloud provider. As there is always a lack of time, the challenge was set to fit in one hour. The task was exacerbated by me being totally Windows guy, with only basic knowledge of Linux. I need to google almost every command. Even ls
. I'm not joking.
The first step was to set up NodeJS. Obvious apt install nodejs
got me stable but ancient version 12.x. I turned to the internet and learned about Snapcraft application store. There is a snap
command to install NodeJS, which turned out to be a costly mistake, as I had lost half of the allotted time just to realize that snap somehow breaks the ability of PM2 to span new processes.
Eventually, I had found the command to download and install the latest version. Quite unpronounceable, though, from a Windows perspective:
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
apt install nodejs
Then came the Yarn, our package manager of choice:
npm install -g yarn
The whole folder of the chatbot was zipped (excluding node_modules), transferred via FTP and unpacked. A bit of correction was needed to replace system environment variables with dotenv
package and .env
text file.
As Windows and Linux line endings differ, git was told to ignore it:
git config --global core.autocrlf true
On Windows, we used system Task Scheduler for launching chatbot server at system startup. Here, PM2 process manager comes to the help.
npm install -g pm2
I tried to compel it working with yarn start
command, but quickly abandoned futile attempts and retreated to calling compiled JS instead.
pm2 start dist/server.js --name chat-bot --watch
Strange, that of two NodeJS servers, one refused to work with --watch
flag. I didn't have enough time to dive into the problem and just switched off watching for the project. It is not updated often, anyway.
Don't forget to start up projects automatically upon VPS reboot, as it could happen at any moment:
pm2 startup
That is it. Transferring NodeJS servers from Windows to Linux turned out to be not so difficult and long.
Top comments (4)
I can't help but wonder:
Why not just choose a serverless setup?
Or docker so you can ensure builds are reproducible.
To be honest I would encourage you to look into terraform rather than setting this up by hand.
I think dockers will be the next step. The decision was made to be as close to bare metal as possible, as I'm new to Linux.
I get that.
However I believe you'll achieve everything you want by going with the container route.
Given i know nothing about your software nor anything about your infrastructure so my comment is as such.
If the vps is running only one application I would go that route with an always on cloudrun instance.
Thanks for the advice. I will try dockers as soon as squeeze some time for it ))