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.

cd ~
curl | sh
source ~/.profile
apt-get install git

Then install node.

# List the versions
nvm ls-remote
# Assuming we want 0.10.28
nvm install 0.10.28

Finally, you probably want to copy the active version into /usr/local.

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

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/

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):

```nginx /etc/nginx/sites-available/
# Node server block
server {
listen 80;

        root  /var/www/;
        index   index.html index.htm;


        # Proxy to node app
        location / {
          proxy_pass http://localhost:8021;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
} ```

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.

npm install forever -g

Start with Forever

cd /var/www/

# Install the packages, if you haven't
npm install

# Start forever, with minimum uptime 10 sec between launches if restart
# Assuming app.js is your application main script
forever start --spinSleepTime 10000 app.js

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 in your home folder. You can nano ~/ and copy the following:


if [ $(ps -e -o uid,cmd | grep $UID | grep node | grep -v grep | wc -l | tr -s “\n”) -eq 0 ]
export PATH=/usr/local/bin:$PATH
forever start –spinSleepTime 10000 –sourceDir /var/www/ app.js » /var/www/ 2>&1

Then `crontab -e`, and add the following:

@reboot /path/to/

To know where your path to is, use pwd at the directory.

That’s it!

To test your script is correct, you can:

forever stopall
# Make sure the script can be executed
chmod +x
forever list
# Should see 1 running process

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/ - contains the node app
  • - for nginx configuration (with a different port)
  • - add another line for the new node app to forever start




Back to Home