Run node.js on NearlyFreeSpeech.Net

July 14, 2015

NearlyFreeSpeech.net (NFSN) is a very inexpensive web host, DNS provider and domain registrar. NFSN added support in 2014 for NodeJS, Django, and tons of other languages through persistent processes. This guide, based on the Django tutorial provided by NFSN, will demonstrate the setup I used for creating a node.js daemon.


NFSN Configuration

If you’re creating a new website, select the Custom domain type:

Choose custom domain option for new site

If you have an exisiting website, the domain can by changed to Custom:

  1. Select your domain under Sites Site selection
  2. Go to the Config Information section and on click the Edit button on Server Type Config information
  3. Select the Custom domain type: Choose custom domain

Project Setup

Next, use SSH to setup your node service on NFSN’S server. Your username, password, and SSH hostname can be found in your site’s information:

SSH information

  1. Connect to your server with SSH. See this section to set up a SSH key on NFSN.
    > ssh mopsled_mycustomdomain@ssh.phx.nearlyfreespeech.net
    mopsled_mycustomdomain@ssh.phx.nearlyfreespeech.net's password:
    [mycustomdomain /home/public]$
  2. Go into the /home/protected folder to set up your node website. This will allow node.js to run as the web user.
    [mycustomdomain /home/public]$ cd /home/protected
  3. Upload or clone your node.js code into this folder. In this example, I'm going to use the node-js-sample repository.
    [mycustomdomain /home/protected]$ git clone https://github.com/heroku/node-js-sample.git
    [mycustomdomain /home/protected]$ cd node-js-sample
  4. Install npm dependencies for your project
    [mycustomdomain /home/protected/node-js-sample]$ npm install
  5. Create run.sh script for running your node process. A NFSN daemon is given a file to run. This file will encapsulate the npm run start command and any other setup your process needs. See environment variable setup in the Additional Details if your project requires environment variables.
    [mycustomdomain /home/protected/node-js-sample]$ nano run.sh
    In run.sh, type the command that starts your node.js process:
    npm run start
    <CTRL>+X, Y, <Enter> to save and exit from nano.

  6. Test running your startup script:
    [mycustomdomain /home/protected/node-js-sample]$ chmod +x run.sh
    [mycustomdomain /home/protected/node-js-sample]$ ./run.sh
    
    > node-js-sample@0.2.0 start /home/protected/node-js-sample
    > node index.js
    
    Node app is running at localhost:5000

Make sure your node.js service works from run.sh before continuing to the next step. Press CTRL+X to kill your service on SSH before the next section.

Note: It’s okay if your node app runs at a high-level port like 5000, 8080, or 10080. NFSN doesn’t give root permissions to allow for low-level port access, but instead uses Proxies to give your server access to low level ports. See Configure Proxy later in the guide.

Configure NearlyFreeSpeech.net Daemon

  1. Select your domain under Sites Site selection
  2. Go to the Daemons section and on click the Add a Daemon button Add a daemon button
  3. Change configuration:
    • Tag: node Note: This tag is used to locate logs for your process
    • Command Line: /home/protected/<NODE_FOLDER>/run.sh
    • Working Directory: /home/protected/<NODE_FOLDER>
    • Run Daemon As: web

    Daemon configuration

  4. Click the Add Daemon button

Configure Proxy

  1. Go back to your site configuration

  2. Go to the Proxies section and on click the Add a Proxy button Add a proxy button

  3. Configure proxy:

    • Protocol: http
    • Base URI: /
    • Document Root: /
    • Target Port: 5000 Note: Change to your node port if different
    • Direct: ☑ Bypass Apache Entirely (Checked)

  4. Click the Add Proxy button

Check your work! Go to your domain to see if your daemon and proxy are working.


Troubleshooting

If you website isn’t working yet, you can use SSH to consult your process’s logs. Output from the daemon process is stored in /home/logs/daemon_node.log.

mycustomdomain /home/protected/node-js-sample]$ cd /home/logs
[mycustomdomain /home/logs]$ ls
daemon_node.log
[mycustomdomain /home/logs]$ tail -f daemon_node.log

> node-js-sample@0.2.0 start /home/protected/node-js-sample
> node index.js

Node app is running at localhost:5000

To restart your daemon process, go to your site’s configuration and click Stop on your daemon:

Afterward, click Start on your daemon to start it again:


Additional Details

Set up an SSH key on NearlyFreeSpeech.net

Setting up an SSH key isn’t difficult, but uses the NFSN web interface instead of the filesystem. See NearlyFreeSpeech.net’s reasoning here.

  1. Go the Profile tab

  2. Click Add SSH Key in the sidebar

  3. Paste your key into the input box, and click Add SSH Key

Setting environment variables on NearlyFreeSpeech.net Daemon

NFSN doesn’t have a environment variable editor like heroku or other cloud services, but environment variables can easily be set in the run.sh script file.

For example, if my node process required the PORT, AUTH_KEY and NZ (timezone) environment variables set, my run.sh script would look like this:

PORT="8080" AUTH_KEY="XXX_XXXXXXXXXX" NZ="America/Denver" npm run start