Running Node.js on a cPanel VPS

Node.js is an open source javascript server environment. It allows you to use Javascript for back-end web applications.

Installation

Node.js is not installed on a cPanel VPS by default. Installation requires root server access and can be installed using 2 different methods described below.

Installing from WHM

  1. Login to WHM to manage your VPS
  2. Open Easy Apache 4 (Home >> Software >> EasyApache 4)
  3. Next to “Currently Installed Packages” click on Customize
  4. Under Additional Packages section, select nodejs10
  5. Click on Next and then Provision and the Node.js packages will be installed

Installing on the command line

  1. SSH into your VPS as root
  2. Run this command:
yum install ea-nodejs10

This installs these packages:

  • Node.js
  • NPM — A Node process manager.
  • NPX — A Node process runner.

The system stores these applications in the /opt/cpanel/ea-nodejs10/bin/ directory.

Once installed, you run your own Node.js apps by running this command in SSH:

/opt/cpanel/ea-nodejs10/bin/node /home/user/path_to_app/app.js

Custom Node.js application example

  1. Create a directory for your Node.js application, in this example we will use my_app
  2. In the my_app directory, create a file named app.js and copy and paste the following
const http = require('http')
const hostname = '127.0.0.1';
const port = 8080;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello! NodeJS is awesome! \n');
});

server.listen(port, hostname, () => {
  console.log(`Node.js App Server at http://${hostname}:${port}/`);
});
  1. Now to run this simple application, run:
/opt/cpanel/ea-nodejs10/bin/node app.js
  1. Now to test this and see it in action, from another terminal window run this:
curl http://127.0.0.1:8080
  1. You should see this output from the Node.js application:
[email protected] [~]# curl http://127.0.0.1:8080
Hello! NodeJS is awesome!