Using Node.js on WHE

Tags Web


Node.js is a platform built on Chrome's JavaScript runtime for easily building fast and scalable network applications. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world. If you intend to host a Node.js site/application please let us know in your initial request. You will be placed on a server that has Node.js support. After you send us your request you will be given a unique port from the Infrastructure team. Please use only this port when serving your application.

Node.js installation guide

You can download and install Node.js and other associated applications following the guide below.

  1. Log in to your site via ssh: ssh
  2. Download Node.js Linux binaries. As of April 1, 2021 the most current version of Nodejs is 14.18.0. You may want to check for updates. wget
  3. Unzip the package: tar -xvf node-v14.18.0-linux-x64.tar.xz
  4. Move the package to a new directory named nodejs: mv node-v14.18.0-linux-x64 nodejs
  5. Change the PATH variable in your $HOME/.bash_profile to reference your version: PATH=$HOME/nodejs/bin:$PATH:$HOME/.local/bin:$HOME/bin
  6. Reload your profile for the changes to pick up: source ~/.bash_profile
  7. Next install an .htaccess file to redirect Apache requests to your Node.js application: cd $HOME/public_html

    This is required because you cannot publicly view your site site directly via the unique port; only certain ports are open for cPanel such as 80/443 for web for security purposes.
  8. Create (vim/nano) a .htaccess file (the . is required in the file name) that reads:
RewriteEngine On
RewriteRule ^(.*)$$1 [P,L]

where unique_port_number is your uniquely assigned port number. You may need to modify this .htaccess file depending on how your application functions.

If you don't know your unique port number please

At this point you could run your Node.js application manually using the node command such as in this example:

[~/example]# node index.js
Node app is running at localhost:unique_port_number

This is not preferable as you would need to stay logged in and make sure this process is running for your page to work. You should install a process manager such as pm2 or forever that will keep your application running. We provide instructions on installing pm2 below.

Installing pm2

Still logged into your site, use npm to install pm2: npm install pm2 -g

You can now use pm2 to manage your application: [~/example]# pm2 start index.js
[PM2] Applying action restartProcessId on app [index](ids: 0)
[PM2] [index](0) ✓
[PM2] Process successfully started
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ memory      │ watching │
│ index    │ 0  │ fork │ 13676 │ online │ 31      │ 0s     │ 12.230 MB   │ disabled │
Use `pm2 show <id|name>` to get more details about an app [~/example]#

Sample application

You can install Heroku's example helloworld application to practice deploying applications to your site.

  1. Change directories to your home directory and use git clone the node-js-sample from Heroku: cd ; git clone ; cd node-js-sample
  2. Modify the index.js file to make use of your unique port. In this example it means modifying the line app.set('port', (process.env.PORT || 5000)) to read: app.set('port', (process.env.PORT || unique_port_number)), where unique_port_number is your uniquely assigned port.
  3. Install the ‘express’ module as it is required for this sample app: npm install express
  4. Now you can start your test application using pm2:cd $HOME/node-js-sample ; pm2 start index.js


Article ID: 1614
Wed 5/27/20 8:26 AM
Mon 10/4/21 10:36 AM