NodeJS Hosting on cPanel server

Posted on

by

in

,

NodeJS Hosting with cPanel, Deploy and Manage Nodejs Applications

NodeJS hosting with cPanel made it simple and easy to deploy nodejs applications using Setup Nodejs App tool. Basically this tutorial will cover step-by-step procedure to deploy and manage simple nodejs application. While writing this example we used NodeJS Hosting plan provided MediaStroke as it comes with Setup Nodejs App tool. In case, you don’t have you can buy suitable plan from here.

Step By Step Procedure To Deploy and Manage Simple Nodejs Application with NodeJs Hosting

By following steps mentioned in this tutorial you can deploy and manage your Nodejs applications on NodeJS Hosting.

There are two types of NodeJS applications:

  • Common JS Applications
  • ES6+ Applications

NodeJS Hosting Application Deployment

To deploy Nodejs application on NodeJS hosting, firstly you have to create the application. You can create application using cPanel graphical interface by selecting Setup Nodejs App under Software section of cPanel home screen.

Software UI for setup

Create Nodejs Application in NodeJs Hosting

Create the application using following steps:

  1. Log in to cPanel.
  2. Select Setup Nodejs App under Software section of cPanel home screen.
  3. Click CREATE APPLICATION to begin the application setup on the Setup Nodejs App page:
Create nodejs application in nodejs hosting
  1. On the CREATE APPLICATION form, fill out the following fields:
  • Node.js version : Choose your preferred version from the drop-down list. Latest version is recommended.
  • Application mode : Select either Development or Production from the list. One may initially choose Development and then switch to Production.
  • Application root : It is for the location of application files in the file system. To form the full path to the application files in the cPanel home directory, the value will be appended to /home/<username>/ e.g. /home/<username>/<applicationroot>.
  • Application URL : It is to form a web URL of the application.
  • Application startup file : The very first file that the application will process once it has been launched. Note: For ES6+ applications, use .cjs as file extention (for startup file only)
  • Passenger log file : You can define the path along with the filename e.g. /home/<username>/<applicationroot>logs/logs.log.
  1. Click CREATE after finishing the form.
  2. After created successfully an application starts automatically. Click OPEN to view a test page for the application.

The message “It Works!” with NodeJs version should appear.

NodeJs Hosting package.json file generation

You can now customize the environment using the package.json settings file and the npm package manager. Now you can upload the project files to the application root including package.json file or create package.json file from scratch.

Method A: Create package.json file from scratch

Create package.json window for NodeJs Hosting
  1. To access the File Manager, click the File Manager icon under the FILES area of cPanel.
  2. Select root folder of application under /home/<username>/ in File Manager’s left-hand column.
  3. To create a new file, click +File from Menu bar.
  4. Enter package.json as New File Name in the New File dialog box and then click Create New File.
  5. Use a right-click or secondary-click on the package.json file in the right-hand column of File Manager and select Edit. An edit dialog box appears.
  6. Click Edit to close the Edit dialog box and open file editor screen.
  7. Enter the following package.json file content into the editor screen.
  8. Save the file by clicking Save Changes.
  9. To close the editor, click Close.

package.json file content for commonjs applications:

{
  "name": "nodejsapp",
  "version": "1.0.0",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: not test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description":""
}

package.json file content for ES6+ application:

{
  "name": "nodejsapp",
  "version": "1.0.0",
  "main": "app.js",
 "type": "module",
 "scripts": {
    "test": "echo \"Error: not test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "description":""
}

Method B. Upload package.json file

Upload window for package json file
  1. To access the File Manager, click the File Manager icon under the FILES area of cPanel.
  2. Select root folder of application under /home/<username>/ in File Manager’s left-hand column.
  3. To create a new file, click Upload from Menu bar.
  4. Click on Select File button and choose package.json file to upload.
  5. After complete message, your package.json file uploaded to the folder. You may now close the tab.

Run NPM Install

Take the following actions to install npm:

  1. Select Setup Nodejs App in the SOFTWARE section of the cPanel homepage.
  2. Click the pencil icon in the Web Applications list’s Actions column to edit the application.
  3. To begin the NPM installation, click Run NPM Install.
NPM installation window with Run NPM Install button
  1. Displays a success message, once the NPM installation completes.
NPM install window in Nodejs Hosting

ES6+ modules code issues handle in NodeJs hosting

Modern applications are built with ES6 module, and as of now NodeJs hosting application does not supports ES6 modules. So we have to handle this issue with additional configuration in nodejs hosting. As you have added the .cjs (commonjs) eg. app.cjs file as entry file, you have to edit the file.

  1. To access the File Manager, click the File Manager icon under the FILES area of cPanel.
  2. Select root folder of application under /home/<username>/ in File Manager’s left-hand column.
  3. Use a right-click or secondary-click on the app.cjs file in the right-hand column of File Manager and select Edit. An edit dialog box appears.
  4. Click Edit to close the Edit dialog box and open file editor screen.
  5. Remove any predefined code and add the following content into the editor screen.
(async ()=>{
    try {
        const app = await import('./app.js');
        if (app.default && typeof app.default.listen === 'function') {
            app.default.listen();
        }
    } catch (err){
        console.log('Failed to start ESM application:', err);
        process.exit(1);
    }
})();

Here, update the import function with parameter as path to the main file of the project

  1. Save the file by clicking Save Changes.
  2. To close the editor, click Close.

Manage Nodejs Applications in NodeJs Hosting

To manage Nodejs applications created use following procedures:

The Web Applications page shows the list of Nodejs applications.

all nodejs hosting applications list

Start Node.js Application

By following the steps listed below, you can start nodejs application that had stopped:

  1. Click the Start icon in the row of the stopped application and in the Actions column.
  2. Start icon changes to Stop icon on completion of action.

Stop Nodejs Application

By following the steps listed below, you can stop nodejs application that has already started:

  1. Click the Stop icon in the row of an already-running application in the Actions column.
  2. Stop icon changes to Start icon, on completion of action.

Re-Start Node.js Application

By following the steps listed below, you can re-start nodejs application that has already started:

  1. Click the Restart icon in the row of an already-running application in the Actions column.
  2. An already-running application gets stopped and started again, on completion of action.

Delete Node.js Application

By following the steps listed below, you can delete nodejs application:

  1. Click the Bin icon in the row of an application that to be deleted in the Actions column.
  2. In the confirmation pop-up appeared, click AGREE to proceed with deletion or CANCEL to close it.
  3. An application will be deleted from the Web Applications list once the action is completed.

Edit Nodejs Application

By following the steps listed below, you can edit nodejs application:

  1. Click the Pencil icon in the row of an application you want to edit, in the Actions column.
  2. Application details will be opened in new tab for editing.
NodeJs Hosting nodejs application edit window
  1. You can modify below details of that applications:
    • Nodejs version : To change the Nodejs version, pick one from a drop-down list.
    • Application mode : To change the application mode, choose either Production or Development available in drop-down options.
    • Application root : A physical address for the application on a server that corresponds to its URI must be mentioned in order to specify the application root.
    • Application URL : Provide an HTTP/HTTPS URL link to the application.
    • Application startup file : Enter the name of the application startup file as <filename>.js.
    • Passenger log file : Specify the path to the log file.
    • Environment variables : You can add or modify environment variable by specifying name and value to it. Also you can delete environment variable by clicking Bin icon in that row in Actions column. The confirmation pop-up appeared, click AGREE to proceed with deletion or CANCEL to close it.
  2. Following actions are available :
    • RESTART : Click the RESTART button to restart the Nodejs app.
    • STOP APP : Click the STOP APP button to stop the Nodejs app.
    • Run NPM Install : To install the package(s) specified in the package.json file, click the Run NPM Install button.
    • Run JS Script : When you click the Run JS Script button, the command(s) specified in the Scripts section of the package.json file will be executed. Click “Ok” after selecting the script’s name and specifying any other necessary parameters.
    • Edit package.json : Click on edit to edit contents of package.json. The contents of package.json will be opened in editor in pop-up window. You can edit contents and then click the SAVE button to save it or CANCEL to cancel & close editor.

Conclusion:

This article summing up covering step-by-step procedure to deploy and manage Nodejs applications on server using cPanel with NodeJS Hosting. This article is written considering simple Nodejs applications particularly. For Nodejs applications with MySQL database refer another article here.

Leave a Reply

Your email address will not be published. Required fields are marked *