Installing Multiple Node Apps on VPS
Digital Ocean has a guide on installing node, and another guide on installing multiple node apps on 1 instance.
This is my version, after setting up apache and nginx together on the same instance.
Install node
Firstly, install nvm
(node version manager), and also git
.
The example use nvm version 0.7.0. You can update accordingly.
1 2 3 4 |
|
Then install node
.
1 2 3 4 |
|
Finally, you probably want to copy the active version into /usr/local
.
1
|
|
Deploying your app
I have choose to deploy using SFTP.
I use Cyberduck (Mac app), and simply copy the node app into eg /var/www/nodeapp.com/
Note: There are better ways. And using dokku seems to be very popular, but dokku does not support running multiple apps.
Setup Nginx
If you followed the tutorial in setting up apache alongside nginx, you would want to edit the server block for the node app like this (I have changed the name to nodeapp
instead of nginxapp
):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Restart nginx with sudo service nginx restart
.
Install Forever (Globally)
Forever is to ensure your node app gets run forever. That means if it crashes, it will restart for you.
1
|
|
Start with Forever
1 2 3 4 5 6 7 8 |
|
Start with Forever, truly forever
Your app is not truly running forever.
If your VPS crash, then forever is not being started..
Hence, you need a cronjob and this starter.sh
in your home folder. You can nano ~/starter.sh
and copy the following:
1 2 3 4 5 6 7 |
|
Then crontab -e
, and add the following:
1
|
|
To know where your path to starter.sh
is, use pwd
at the directory.
That’s it!
To test your script is correct, you can:
1 2 3 4 5 6 |
|
Or reboot your VPS to see that it gets truly run forever and ever :)
Multiple Node Apps
To deploy multiple node apps, repeat (except for installing node, git, forever, crontab).
Assuming I have another app call nodeapp2
, these are the changes:
/var/www/nodeapp2.com/
– contains the node appnodeapp2.com.conf
– for nginx configuration (with a different port)starter.sh
– add another line for the new node app to forever start