Knowledge Base

Develop Node.js Application In cPanel With Node.js Selector

The Node.js selector allows you to easily set up Node.js applications with the cPanel graphical interface or at the command line.

Throughout this tutorial these variables are used:
  • app is used for the application root directory and application uri
  • example is used for the user name
  • example.com is used for the domain
  • app.js is used for the application startup file
  • My app is used as the description
  • 10 or 11 is the node.js major version number

You should change these values to match the values required for your account and application.

Seting up a Node.js application with the cPanel interface

Create the application

  1. Log in to cPanel.
    If you do not know how to log in to your cPanel account, please see this article.
  2. In the SOFTWARE section of cPanel, click on the Setup Node.js App icon to open the Node.js selector.

  3. On the Node.js selector page, click on the Create Application button to start the application setup.

  4. Fill in the fields on the application setup form.

    • Node.js version - select your perferred version from the drop down list
    • Application mode - chose Development or Production from the drop down list. Development may be chosen initially and changed to production later.
    • Application root - the file system location for application files. The entry will be appended to /home/username to form the complete path to the application files in the cPanel home directory.
    • Application URL - the address or the application on the internet.
    • Application startup file - the initial file that will be processed when launching the application.
  5. When the form is complete, click the Create button.

  6. After the application is created, an information box is displayed explaining the package.json is required to continue.

  7. At this time the application will launch and display a test page. Click the Open button to see the test page.

  8. Click the Cancel button to continue.

Now that a working application is installed, the environment can be enhanced with the package.json settings file and the npm package manager. To install package.json and npm, follow the seps in the next two sections.

Create the package.json

  1. In the FILES section of cPanel, click the File Manager icon to open the File Manager.

  2. In the left hand column of File Manager, click the text of the application root folder.

  3. Click the +File button to create a new file.

  4.  In the New File dialog box, enter the file name package.json and then click the Create New File button.

  5. Right-click or secondary click on the package.json file in the right-hand column of File Manager and then click Edit. An edit dialog box is displayed.

  6. In the Edit dialog box, click the OK button.
  7. Enter the following text in the editor screen

    { "name": "app", "version": "1.0.0", "description": "My App", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
  8. Click the Save Changes button to save the file.

  9. Click the Close button to close the editor.

Install npm

  1. In the SOFTWARE section of cPanel, click on the Setup Node.js App icon to open the Node.js selector.

  2. In the Actions column of the Web Applications list, click the pencil icon to edit the application.

  3. Click the Run NPM Install button.

  4. The NPM installation runs and presents a success indicator when complete.

To install packages with NPM and do other command line tasks related to the application, log in via SSH and enter the virtual environment for the applcation using the command shown in the information box at the top of the application setup page.

 

Setup via command line

Users familiar with using SSH and the bash shell may find the command line process faster and easier than navigating the cPanel interface. To set up a node.js application from the command line, follow these steps:

  1. Log in to your cPanel account via SSH.
  2. Create the application with the following command:
    cloudlinux-selector create --json --interpreter nodejs --version 11 --app-root app --domain example.com --app-uri app
  3. Change to the application directory

    cd app
  4. Open the vi editor and create the package.json file

    vi package.json
  5. press i to change to insert mode and paste the following text into the editor.

      { "name": "app", "version": "1.0.0", "description": "My App", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
    
  6. Press escape followed by : to enter command mode.
  7. Press x followed by Enter to save and exit the editor.
  8. Install npm by entering the following command.

    cloudlinux-selector install-modules --json --interpreter nodejs --user example --app-root app

To install packages with NPM and do other command line tasks related to the application, log in via SSH and enter the virtual environment for the applcation using the command

source /home/example/nodevenv/app/11/bin/activate && cd /home/example/app