Deploying a Node app on Digital Ocean using Nginx.

July 4th, 2015

do_node

This post will show you how to deploy to Digital Ocean, if you need to sign up click the link there! First things first, select an image to use, I have been using Ubuntu and I find it nice and easy to use, so this post will be focused around that system.

Once you get an image selected you will now need to login to it. You will get an email with the IP and the password for the server. You need to use shh to login to this server. This is not as hard as it sounds. In your terminal:


    ssh root@ipaddress_here

It will prompt you to you enter the provided password and then ask you to create a new password from there. A note, when creating a new server you have the option to add and ssh key, so you can prevent the need for passwords. You might also want to create a new user and not use the root account, in this case we will just stick with the root user.

Once you are in there are a few things you will want to do. I prefer to use Nginx as a server and reverse proxy my node app through there.


apt-get update
apt-get install nginx

This will install Nginx on the computer, there is a few locations we need to worry ourselves with here. The first is /usr/share/nginx/html, this is where you will put your node app files.


cd /
cd /usr/share/nginx/html

Inside this folder I would use git clone URL . so you only get the contents of the git repo not a new folder with the repo name.

The next folder you will need to be concerned with /etc/nginx/sites-available, inside of here there is a file called default. This is our sever configuration file. Open it up in your editor of choice from the command line, I prefer nano.


nano default

There will be a bunch of configuration options already set up, but we need to concern ourselves with only a few, if you want you can also delete all the options here and apply just these.


server {
    listen 80;
		
    server_name yoursite.com;

    location / {
        proxy_pass http://127.0.0.1:4500;
        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;
    }
}

Inside of the server block we need to make sure that our Nginx sever is listening on port 80, set up the server name based on your url.

And then set up the location, the / will be the root location. Inside here we set up our proxy_pass variable to point to the port our node server is on. In this case we just use the internal IP for the server. A more secure method would to actually have two servers, one for the proxy and one for our app, and you would use the ip for that server in the proxy_pass variable. We will just us the one at the moment. To exit out of nano press ctrl x type Y and then enter. Make sure that nginx is up by running


    service nginx start

If the server was running and you made changes to the config file you can run nginx -s reload and this will reload the config file for the server.

Now lets set up the the node app. cd into /usr/share/nginx/html. First things first, make sure we have node installed.


apt-get install nodejs
apt-get install npm

ln -s /usr/bin/nodejs /usr/bin/node

This allows us to just use the `node` command. Run `npm install` to install all of your packages, and also run `npm install -g forever`.

A side note, you can update Node using npm([source](http://davidwalsh.name/upgrade-nodejs)).


npm cache clean -f
npm install -g n
n stable

At the time of writing this there was a security patch set in 0.12.6 that is important, so I suggest you update if you are running an earlier version.

We will use the forever package for running our app, I would also suggest looking at the pm2 package.


forever -w  --watchDirectory . start server.js

Using forever and the -w and the --watchDirectory . flag we can watch the file system for any changes. Running forever list you can see that the servers running from there.

And that is all you need for a simple application set up, there are more sophisticated deploys out there, but this is great for simple applications! I hope that helps you getting started deploying a node app on Digital Ocean.


Warning: count(): Parameter must be an array or an object that implements Countable in /home/ryanch6/public_html/wp-includes/class-wp-comment-query.php on line 405
  • Pingback: Setting up SSL on Digital Ocean with Node and Nginx - Ryan Christiani - Front-End Developer - Ryan Christiani – Front-End Developer()

  • sudeep

    thanks but we can use PM2 insted of forever

    • http://google.com/+Nkansahrexford Nkansah Rexford

      I think you can use whatever you want, just get comfortable with the commands for the specific tool you wish to use. I’m gonna use supervisor or upstart for this.

  • Alvin Lin

    Both forever and pm2 have issues with mangling the response data from the server. I had to deal with this obscure tidbit when I was writing a news summarizer for terminal. Apparently, forever and pm2 strip out ANSI color codes from the response data. If you’re curious, my project is hosted at nycurl.sytes.net
    If you:
    curl nycurl.sytes.net
    in your terminal, you should get formatted news headlines with color. I solved this by hosting the server using screen and node instead. Although it doesn’t have the features that forever and pm2 offer, I figure it’s worth a mention.
    screen
    cd path/to/server
    node server.js
    Ctrl-A D