How to Setup Node.js?

By on November 8th, 2018

How to Setup Node.js?

Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside the web browser. It came to existence when the original developers of JavaScript elaborated it from something which is only runnable on the browser to something that can be used as a standalone application on your system. Node.js is mainly used for hosting different kinds of applications like the Ghost blogging platform etc. As we all know by default cPanel does not support node.js. In this tutorial let’s have a look into how to integrate Node.js to a cPanel server running on EA4.

1) Login to your server via SSH as root.

#  ssh root@yourserverIPadress

2) Now navigate to /root directory and download the node.js file.

#  cd ~

#  wget https://nodejs.org/dist/v6.9.1/node-v6.9.1-linux-x64.tar.xz

3)  After the downloading completes, extract the downloaded file.

#  tar -xvf node-v6.9.1-linux-x64.tar.xz

4) Rename the extracted directory to the name of nodejs.

# mv node-v6.9.1-linux-x64 nodejs

5) After the completion of the above steps, let’s move to the next procedure of installing node and npm binaries, you can do the same by executing the following commands.

#  mkdir ~/bin

#  cp -r nodejs/bin/node ~/bin

#  cd ~bin

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

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

6) Finally, the node.js and npm have been successfully installed on the server. You can verify the same by running the below mentioned commands.

#node –version

#npm –version

7) To start the node you can choose two methods, ie either by using npm or by running node directly.

Using npm-

# nohup npm start –production &

The & stands for the command in the background, and the term “nohup” ensures that the application will run even if you log out from the current terminal session.

Running node directly-

# nohup node my_app.js &

Please don’t forget to replace “my_app.js” with your application file name.

8) To stop the node.js application, only need to execute the following command.

# pkill node

The above-mentioned command will stop the node.js immediately.

By enabling the node.js application to an unused port on the server you can access the application through your web browser. Let’s see how we can perform these.

Open the .htaccess file of the account which you run node.js with your favorite text editor.

# vi /home/username/public_html/.htaccess

Place the following code in it.

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]

Please don’t forget to replace the “XXXXX” with the port number which your node.js application runs. Now save the changes and exit the text editor. After these changes made the visitors of your website will be redirected to node.js application listening on the specified port.

Hence we have learned how to integrate node.js on cPanel server.

 

If you have any doubt regarding this, please do reach us via support ticket or live chat.

 

One Response to “How to Setup Node.js?”

  1. Shailendra sen says:

    process of deploying of nodejs application for inter server

Leave a Reply