Hostiso - hosting for you!How to install/setup Node.js with cPanel

homepage - blog


From now Hostiso support Node.js on any hosting package, even shared with easy to use interface inside cPanel. Node.js is used for hosting diffrent apps, like Ghost blogging platform, etc.

Bellow is instruction how to quickly and easily setup Node.js on your account if you do not host with Hostiso.

Installing Node.js and npm

Here several easy steps to quickly install Node.js and npm (the Node.js package manager). To do this, follow these steps:

  1. Log in to your account using SSH (if is not enabled for your account contact our support).
  2. At the command prompt, type next commands:

    cd ~

    wget https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.gz

    Command above will download v16.13.0 version, in case you need diffrent version you can find latest here: https://nodejs.org/en/download/
  3. To extract the Node.js files, type the following command:

    tar --strip-components 1 -xzf node-v16.13.0-linux-x64.tar.gz

  4. Now we will rename folder to nodejs name, to do this type the following command:
    mv node-v16.13.0-linux-x64 nodejs

  5. Now install node and npm binaries, type the next commands:

    mkdir ~/bin

    cp nodejs/bin/node ~/bin

    cd ~/bin

    ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm

    That is all, Node.js and npm are installed on your account. To verify, type the following commands:

    node --version

    npm --version

    The ~/bin directory is in your path by default, which means you can run node and npm from any directory in your account.

How to start Node.js application

Now after installing Node.js, you are ready to run Node.js app. However, the exact steps to do this vary depending on the app configuration.

Method #1: Use npm

Many third-party and “production-ready” applications (such as Ghost) use the npm program to start the application, as shown by the following command:

nohup npm start --production &

The & places the command in the background, and the nohup command ensures that the application continues running even if you log out of the current terminal session.

For this method to work, there must be a valid package.json file for the application. The package.json file contains project metadata that the npm program reads to determine how to start the application, manage its dependencies, and more.

To view the official npm documentation for the package.json file, please visit https://docs.npmjs.com/files/package.json.
Method #2: Run node directly

For simple apps, or for any application that does not have a package.json file, you can run the node executable directly and specify the application filename. For example:

nohup node my_app.js &

However, you lose the benefits of using npm to manage the application.

As above, the & places the command in the background, and the nohup command ensures that the application continues running even if you log out of the current terminal session.

Stopping a Node.js application

To stop Node.js app, just type the following command:

pkill node

Above command will immediately stops all running Node.js apps.

Integrating a Node.js application with the web server

Depending on the type of Node.js application you are running, you may want to be able to access it using a web browser. To do this, you need to select an unused port for the Node.js application to listen on, and then define server rewrite rules that redirect visitors to the application. The following steps demonstrate how to do this:

  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:
    RewriteEngine On
    RewriteRule ^$ [P,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$$1 [P,L]
  2. In both RewriteRule lines, replace XXXXX with the port on which your Node.js application listens.
    To run a Node.js application on a managed server, you must select an unused port, and the port number must be between 49152 and 65535(inclusive).
  3. Save the changes to the .htaccess file, and then exit the text editor. Visitors to your web site are redirected to the Node.js application listening on the specified port.
    If your application fails to start, the port you chose may already be in use. Check the application log for error codes like EADDRINUSE that indicate the port is in use. If it is, select a different port number, update your application’s configuration and the .htaccess file, and then try again.

Wish to know more …

You can find all about Node.js, on http://nodejs.org.

Request Tutorial
Need tutorial, contact us ...