How To Install/setup Node.JS with cPanel?


How To Install/setup Node.JS with cPanel? 1


NODE.JS support is extremely important in hosting and even in shared hosting.  It is needed to build sites that need real-time asynchronous interaction.  Node.js is particularly useful to build fast scalable network applications that are perfect if the application will  be run on distributed devices.  Here are steps to install Node.js with cPanel.

 

  1. Login to your WHM account using SSH
  2. At the command prompt, type following commands
  1. cd ~
  2. wget https://nodejs.org/dist/v6.9.1/node-v6.9.1-linux-x64.tar.xz (or find newer version)
  1. Type the following command to extract node.js
  • tar xvf node-v6.9.1-linux-x64.tar.xz
  1. You need to rename the folder as nodejs using the following command
  • mv node-v6.9.1-linux-x64 nodejs
  1. To  install node and npm binaries, type the following commands
  1. mkdir ~/bin
  2. cp nodejs/bin/node ~/bin
  3. cd ~/bin
  4. ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm

The Node.js installation is now complete. The next step is to start Node.js application

 

Steps to start Node.js application

 

The steps to run Node.js app depends mainly on its configuration.

Method 1  – Use NPM

Many production ready applications and 3rd party apps use the npm program to launch the application. type the following command to start such applications.

  • nohup npm start –production & 

Note- the & puts the application process in the background and the application will continue to run even after you log out of current terminal session. For this method to work,  there should be a valid package.json file for the application which contains metadata that is required by the npm program to learn how to start the application

Method 2  – Run Node Directly

Simple apps do not require a package.JSON file and  you can run the note executable directly  by specifying the application filename

  • nohup node my_app.js &

Note– Simple apps also run in the background and continue running even after you log out of current terminal session.

 

Steps to stop node.js application

 

Type the following command to stop all node.js application

  • pkill node

if you want to access the node.js application from a web browser, you need to find an unused port that node.js application can use to listen.  To achieve this you need to integrate the node.js application with the web server.


Steps to integrate node.js application with the web server 

  1. Open a text editor and the  following line to the htaccess  file in the/home/username/public_html directory, where username refers to your account username
  1. RewriteEngine On
  2. RewriteRule ^$ http://127.0.0.1:XXXXX/ [P,L]
  3. RewriteCond %{REQUEST_FILENAME} !-f
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteRule ^(.*)$ http://127.0.0.1:XXXXX/$1 [P,L]
  1. In the 2nd and 5th, RewriteRule lines replace XXXXX with the port on which your Node.js application listens.
  1. As mentioned above, you will be required to select an unused port for listening and the port number should be between 49152 and 65535(inclusive).
  1. Save the .htaccess file and close the text editor.
  1. If your application fails to start, the port you have selected might be already in use. You can check the application for error codes like EADDRINUSE that tell you about ports in use. If the port is already in use, you need to select a port number, update the .htaccess file and your application configuration and try again.

Leave a Reply

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

Security code * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.