Using Node.js on WHE

Summary

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.

Environment

LSA cPanel environment

Directions

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 username@site.name.lsa.umich.edu
     
  2. Download Node.js Linux binaries. As of October 2, 2023 the most current version of Nodejs is 18.16.1 (LTS stable) or 20.3.1 (latest features). You may want to check for updates. wget https://nodejs.org/dist/vversion/node-vversion-linux-x64.tar.xz
     
  3. Unzip the package: tar -xvf node-vversion-linux-x64.tar.xz
     
  4. Move the package to a new directory named nodejs: mv node-vversion-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
DirectoryIndex
RewriteRule ^(.*)$ http://127.0.0.1:unique_port_number/$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.

Please note: If you don't know your unique port number please contact LSATechnologyServices@umich.edu.

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:

username@site.name.lsa.umich.edu [~/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
username@site.name.lsa.umich.edu [~/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 https://github.com/heroku/node-js-sample.git ; 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

Details

Article ID: 1614
Created
Wed 5/27/20 8:26 AM
Modified
Mon 10/2/23 6:28 AM