How to install and configure Node.js on cpanel server

    Posted in Whm/Cpanel on Jul 30, 2019

    In order to install and configure Node.js on cpanel, two prerequisites are needed -

    • Normal shell must be enabled in your account. In order to determine the type of shell, type the following command -
    echo $SHELL
    • Node.js currently only works on servers running CentOS 6 or CentOS 7. To determine the CentOS version, type the following command -
    uname -r

    INSTALLING NODE.JS AND NPM

    Once you determine that your account meets the installation prerequisites, follow these steps in order to download and install Node.js and npm -

    1. Log in to your account using SSH.
    2. At the command prompt, type the following commands:
    cd ~
    wget https://nodejs.org/dist/v10.5.0/node-v10.5.0-linux-x64.tar.xz
    1. To extract the Node.js files, type the following command:
    tar xvf node-v10.5.0-linux-x64.tar.xz
    1. To rename the extracted folder to the more convenient nodejs name, type the following command:
    mv node-v10.5.0-linux-x64 nodejs
    1. To install the node and npm binaries, type the following commands :
    mkdir ~/bin
    cp nodejs/bin/node ~/bin
    cd ~/bin
    ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm

    After this, verify the installation process -

    node --version
    npm --version

    STARTING A NODE.JS APPLICATION

    METHOD 1 - Use npm

    Use the npm program to start the application by typing the following command :

    nohup npm start --production &

    package.json file is required in order to make this method work.

    METHOD 2 - Run node directly

    For simple applications, you can run the node executable directly and specify the application filename. For example:

    nohup node my_software.js &

    STOPPING A NODE.JS APPLICATION

    To immediately stop all currently running Node.js applications, type the following command:

    pkill node

    INTEGRATING A NODE.JS APPLICATION WITH THE WEB SERVER

    In order to integrate a node.js application with the web server, follow the below steps :

    1. In a text editor, add the following lines to the .htaccess file in the /home/username/public_html directory, where username represents your account username:
    DirectoryIndex disabled
    RewriteEngine On
    RewriteRule ^$ http://127.0.0.1:XXXXX/ [P,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ http://127.0.0.1:XXXXX/$1 [P,L]
    1. Replace XXXXX with the port on which your Node.js application listens in both RewriteRule lines.
    2. Save the changes to the .htaccess file, then exit the text editor.