PhillipBlanton.com

Phillip H. Blanton invites you and a guest to talk about him.

Preparing a GoDaddy Shared Hosting Environment for a Node.JS Application

I am working on a Node.JS project for a friend and he decided to use a GoDaddy shared hosting package to host the site once it's done. So... as part of my development and testing, I went and bought the same package and set up my CICD to deploy the project to it so that I'd have no ugly surprises when I went to ultimately deploy the project to production.

Unfortunately the GoDaddy shared hosting packages don't support Node.JS apps natively and without the ability to sudo and install very many tools, many people think they are stuck and cannot use GoDaddy shared hosting for Node.JS. That's not necessarily true and if you carefully follow this guide, you should have node.js installed and hosting apps in about 15 minutes - barring any unforseen problems.

  1. Either set up SSH to your GoDaddy shared hosting package, or just use the "terminal" available in your CPanel.
  2. Install Node.JS on your instance
    1. download the latest version of Node.JS to your local machine and unpack it.
      https://nodejs.org/en/download/
    2.  Then zip it back up as a "zip" file and upload the zip file to your Shared Hosting installation using the CPanel File Manager.
    3. Using the CPanel's terminal or a terminal connected via SSH, unzip the file using the "unzip" program and rename the result to just "nodejs"...
      $ unzip <filename>.zip
      $ mv node-v14.5.0-linux-x64 nodejs
    4. If one doesn't exist, create a "bin" folder in the home directory on your shared hosting home directory.
      $ cd ~
      $ mkdir bin
    5. Move the extracted Node.JS binaries to the bin directory and set its permissions...
      $ mv nodejs/bin/node ~/bin
      $ chmod 755 node
    6. switch to the bin directory and add  a symlink for the npm program...
      $ cd ~/bin
      $ ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
    7. Check that everything is working ...
      $ node -v
      $ npm -v
      $ nvm --version
      If each command returns with no error then you are good. If however, node, or npm throws an error, then you'll need to roll back to an earlier Node.JS version that GoDaddy supports. At the time of this writing, anything below version 12.0.0 will work. Versions 12.0.0 and up will not. so...
    8. Install Node.JS version 11.15.0
      $ nvm install v11.15.0
      and test it again...
      $ node -v

      $ npm -v
  3. Once you have Node.JS installed on the server, using the terminal, navigate to /home/<your username>/public_html and check for the .htaccess file. If it doesn't exist then create it and put this content in it...
    RewriteEngine on
    RewriteRule  (.*)  http://localhost:3000/$1  [P,L] 
  4. Create a new file in the public_html directory called "app.js" and add this content to it...
    const http = require('http');
    const hostname = '127.0.0.1';
    const port = 3000;
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('NodeJS server running on Shared Hosting\n');
    });
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
  5. From the public_html directory, run these commands...
    $ which node
    This should return the the path to node.js, ie: "~/bin/node". If so then you're good to go. If not, figure out why and fix it.
    $ node app.js &
    This will start the app.js file as a node.js server and the "&" will instruct the platform to run it in the background.
  6. Browse to the domain name associated with your GoDaddy hosting package and you will see the following line from your app.js file...
    NodeJS server running on Shared Hosting
    If so, then your shared hosting package has node.js installed on it!