How to Install a Node.js Application


Overview

Warning:
  • Because cPanel, L.L.C. doesn’t develop Node.js, cPanel Technical Support can’t help you with these steps.
  • We recommend that only experienced system administrators perform these steps.
  • We are not responsible for any data loss.

This document describes how to install a Node.js web application. Node.js is a JavaScript runtime that allows you to build scalable web applications.

Note:
  • We recommend that you perform the steps in this document via the command line as a cPanel user unless otherwise specified.
  • You can also perform these steps in cPanel’s Terminal interface (cPanel >> Home >> Advanced >> Terminal).
  • In this document, nodejsapp represents the application’s name and 3000 represents represents an open firewall port.

For more information, read the Node.js documentation.

Install a Node.js application

Pre-installation settings

Before you begin, make certain that your hosting provider installed the following EasyApache 4 RPMs on your server:

  • ea-ruby24-mod_passenger

    Note:

    This module disables Apache’s mod_userdir module.

  • ea-apache24-mod_env

  • ea-nodejs10

    Note:

    We also recommend that your hosting provider install the ea-ruby24-ruby-devel module.

Install the application

To install an application, perform the following steps:

  1. Log in to the server via SSH as a cPanel user.

  2. Create the application’s directory, relative to your home directory. To do this, run the following command:

    mkdir nodejsapp

  3. Change to the application’s directory.

  4. Create the app.js file with a text editor.

Important:

We strongly recommend that you create the file with this exact name because Passenger searches for this filename when it executes. If you create a startup file with a different name, you must specify the filename in the ‘httpd.conf’ file. To do this, follow the directions in the Create a custom startup file section below.

  1. Add the application’s configuration to the app.js file. This will resemble the following example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    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('Hello World! NodeJS \n');
    });
    
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });

Test the application

After you install the application, we recommend that you confirm it’s active. To do this, perform the following steps:

  1. Run the following command:

      /opt/cpanel/ea-nodejs10/bin/node app.js
    
    The output will resemble will the following example:
      Server running at http://127.0.0.1:3000
    

  2. Open another terminal window and log in to the server via SSH as the same cPanel user.

  3. Run the following command:

      curl http://127.0.0.1:3000
    
    The output will resemble the following example:
      Hello World! NodeJS
    

Note:

If you wish to export the /opt/cpanel/ea-nodejs10/bin path to your environment, add the following line to your .bashrc file:

<p>export PATH=/opt/cpanel/ea-nodejs10/bin/:$PATH</p>

Register the application

After you install the application, register it. To do this, use cPanel’s Application Manager interface (cPanel >> Home >> Software >> Application Manager).

After you register the application, you can access the application in a web browser with the following URL:

http://example.com/nodejsapp

Note:

In this example, example.com represents your website.

Restart the application

When you want your application to restart after you edit it, create the restart.txt touch file in the application’s tmp directory. This file directs mod_passenger to restart the application after you modify it. This action applies your changes to the application.

Important:
  • You must touch the restart.txt touch file each time that you want mod_passenger to restart the application.
  • You must manually create the tmp directory. For example:
    $appDir/tmp/restart.txt

Create a custom startup file

Passenger searches for the app.js filename when it executes. If you create a startup file with a different name, you must create an include file and specify the startup filename there. If you don’t do this, your application won’t function.

To specify the new filename, perform the following steps:

  1. Create the /etc/apache2/conf.d/userdata/ssl/2_4/user/domain.nodejs.conf file with a text editor.

  2. Add the PassengerStartupFile variable and the filename as its value in a virtual host container. This will resemble the following example:

    1
    2
    3
    4
    5
    6
    
    DocumentRoot /user/example.com/public
    # Use server.js as the startup file (entry point file) for
    # your Node.js application, instead of the default app.js
    PassengerStartupFile index.js
    PassengerAppType node
    PassengerAppRoot /nodejsapp/example.com

  3. Rebuild the httpd.conf file. To do this, run the following command:

    /usr/local/cpanel/scripts/rebuildhttpdconf

  4. Restart Apache. To do this, run the following command:

    /usr/local/cpanel/scripts/restartsrv_httpd

Additional Documentation