Run Node on a standalone server proxying to port 80

Most if not all node applications were meant to run on standalone HTTP servers which proxy to port 80 via Nginx or Apache. I am going to show you how to do this on a DreamHost VPS and also set up Forever. I will also show you how to set up a cronjob to start Forever on a server reboot. Keep in mind that this can only be done on a DreamHost VPS (or dedicated) since shell users are unable to bind to ports due to security/performance reasons on a shared server.

Prerequisites:

First read up on how to set up NVM on the Dreamhost KB here https://help.dreamhost.com/hc/en-us/articles/217185397-Node-js-overview. Once you have your user and site up you can begin to install you application. For this tutorial we are sticking to a simple hello world application that will run on port 8080 which we will daemonize with forever. There are other methods to daemonize, but forever is probably the most popular and easiest. Do NOT enable passenger for this tutorial since we are not using it. If passenger is enabled, please disable it in the panel for this particular domain.

While cloudflare does support some non standard ports (https://support.cloudflare.com/hc/en-us/articles/200169156-Which-ports-will-CloudFlare-work-with-) it may be best to disable services like cloudflare and sucuri during the tutorial.

Basic Set Up

First let’s SSH into our site and user and then cd into the main site directory.

In this directory we will create an app.js file and paste the following contents.

In the above code we added a port number within the listen function. Now we can start the server using the following:

Next you can visit the site by appending the port in browser.

You should see a basic hello world page. But we do not want our site visitors to have to type “:8080”, so we will set up a proxy within the DreamHost panel to proxy port 8080 to the default HTTP port 80.

In the DreamHost panel, visit the Mongrel/Proxy page and set up at https://panel.dreamhost.com/index.cgi?tree=domain.proxy&. Under the “Set Up A Proxy Server Port:” section we will fill out the fields to tell Apache what we wish to proxy.

For the URL to proxy, we select the domain from the drop down we set this app up on. You can leave the field next to the domain name blank as we set up on the root and not sub a directory. For the “Proxy number to port” field, enter in the port the server was set up to run under in our app.js (8080 in our example). Save your settings and allow about 10 minutes for the changes to go through.

Now you can visit the domain without appending the port at the end as Apache is taking care of this for you.

Daemonizing and setting up a cron to start Forever on server reboots

Next we need to daemonize the application so that it runs in the background and add some fault tolerance for when it goes down. This is most easily done by using Forever. In order to install Forever correctly, you must be able to install it globally under the applications user. If NVM was set up correctly, global NPM installations will work.

First CD into your root directory.

Then run your NPM install command.

To check that it was installed globally run the following:

/home/username/.nvm/versions/node/v5.0.0/lib
├── forever@0.15.2
└── npm@3.3.6

Next, from the main Node app directory run the following:

Have some patience as it takes a couple of minutes for forever to start up the application.

Setting up a cronjob for Forever

Now we need to make sure forever starts up after a reboot so our app is started up again. We can accomplish this with a cron job. In the DreamHost panel visit https://panel.dreamhost.com/index.cgi?tree=goodies.cron& and click the button to add a new cron job. In the command to run we are going to add the following:

The first part “/bin/bash -l exec” will load your local bash environment variables so you do not have to put the full path to the forever binary. But you can explicitly set the absolute path. The binary file (if you used NVM) would be located in a path similar to the following:

Let us test out our cron by rebooting the server via the DreamHost panel. Visit https://panel.dreamhost.com/index.cgi?tree=vps.dashboard& and click the Restart button. It should only take a minute or two to completely reboot and a couple of more minutes for forever to start the application back up.

If all went correctly, the site will be up and running again. You can do this with any node application and is the only way to run node if the application needs to bind to a socket or utilize server sent events.