Skip to main content

In this tutorial we will see how to install Node.js on Linux shared hosting environment.

There are many tutorials out there which will guide you on how to get started with Node.js on your shared hosting platform. But bits and pieces are scattered all around in web universe so my post is a small contribution to bring everything at one place in one tutorial. Get up and running Node.js in minutes

This is just for educational purpose, I do not take any authority of any sort.

So Let's Get Started

Difficulty Level

Intermediate.

Time Required

15 – 20 Minutes.

Result

You will be able to host your Node.js application on your Linux shared hosting server

Tutorial Type

Linux Server

Step: 1 – Make sure you have access to Linux shared hosting using SSH

Login to your shared hosting with SSH, to do this you might want to refer this blog, For the sake of brevity, I put How to access Linux Shared Hosting with SSH – PuTTY in different post.

figure 1.1 – SSH Terminal – PuTTY

Step: 2 – Download & Install NVM (Node Version Manager)

Now we will be installing NVM (Node Version Manager) this tool allows you to download and install Node.js to install use the following command in your SSH terminal [see figure 2.1]

wget –qO https://cdn.rawgit.com/creationix/nvm/master/install.sh | bash

figure 2.1 – install NVM

This is how it should look post NVM installation. Now after installation restart your SSH session by closing the putty terminal and re-connecting to the server again [see figure 2.2].

figure 2.2 – post NVM installation

After you re-connect to your Linux shared hosting server on PuTTY try the following command.

nvm --version

 

This should give you a version of NVM, confirming that NVM has installed successfully [see figure 2.3]

figure 2.3 – NVM version

Step: 3 – Installing Node.js

Once we have installed now we can proceed to install Node. To install latest stable version of node use the following command in your SSH Terminal.

nvm install stable

This is how it should look, [see figure 3.1]

figure 3.1 – install Node

You can confirm if node is installed correctly by checking the version of node:

[cc lang=”bash” escaped=”true” width=”100%”]
node –version
[/cc]

You can also play around with built in calculator in Node:

node
> 2 + 2
4
> 10 – 2.4
7.6
>[Ctrl + C]
(To exit, press ^C or type .exit)
>[Ctrl + C]

 

Node has installed correctly [see figure 3.2],
now let us do some configuration to host one example node application. As this is our main goal, that we should be able to run a node application from a Linux shared hosting platform.

figure 3.2 – check Node installation

Step: 4 – Configuring Host to Support Node Applications

Using your file manager in cPanel or FTP or SSH Terminal create a folder called nodeApp this folder should be made inside your http_docs or public_html i.e. this folder should be be accessible from http://[yourDomainName]/nodeApp

Note: you can name your folder whatever you want, for this tutorial I have named it nodeApp

Inside this folder create a file named .htaccess.  To create the file open your SSH terminal and connecting to your website and navigate to folder \www\nodeApp.  [see figure 4.1]

>cd www/nodeApp/ <↵>

>touch .htaccess <↵>

figure 4.1 – create .htaccess file

We have just created our .htaccess file we need to edit it, there must be a default editor already installed use that to edit your .htaccess file for this case I am using nano for editing the file.[see figure 4.2]

figure 4.2 – nano .htaccess

Make the following changes inside your .htaccess file. [see figure 4.3]

RewriteEngine On
RewriteRule ^$ http://127.0.0.1:9000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:9000/$1 [P,L]

Please note: In both RewriteRule lines, replace XXXXX with the port on which your Node.js application listens.

:
RewriteRule ^$ http://127.0.0.1:9000/ [P,L]
:
:
RewriteRule ^(.*)$ http://127.0.0.1:9000/$1 [P,L]

To run a Node.js application on a managed server, you must select an unused port.

Save the changes to the .htaccess file, and then exit the text editor.

figure 4.3 – .htaccess content

Step: 5 – Running Basic Node.js Server

Now let us create a basic node server. We have to write our node server inside the folder which we created in previous steps i.e. nodeApp.

So in the /www/nodeApp folder create a file called nodeServer.js you can do this either using SSH or you can even use file manager from cPanel.

Copy the following code: [I presume that you have basic working knowledge of node]

const http = require('http');
let objServer = http.createServer(function(request,response){
    response.writeHead(200,{ 'Content-Type' : 'text/plain' });
    response.write("Node Server Working!!");
    console.log("New connection made on: " + Date.now());
    response.end();
}).listen(9000);

console.log("Node Server started on");

 

Please note the port used in nodeServer.js & the .htaccess file should be same.

Before staring our Node application, navigate to folder \www\nodeApp in your SSH terminal using:

>cd www/nodeApp/ <↵>

Now start the node application using:

[../www/nodeApp]> node nodeServer.js <↵>

[see figure 5.1]

figure 5.1 – starting nodeServer app

if you followed the steps, in the web-browser you should be able to see the output [see figure 5.2]

figure 5.2 – output in web-browser

So now our node application is up and running, but node application will continue to run till the SSH terminal window is on. When we exit our SSH terminal or terminate our SSH session the node sever will also terminate and therefore we need some automation to keep our node server running continuously. To solve this issue we are going to install nohup. [see the Step:6 for complete guidance]

Step: 6 – Continuously Running Node.js Server

Again in your SSH terminal, First navigate to your nodeApp folder and install nohup using the following command.

[../www/nodeApp]> npm install nohup <↵>

Installing nohup on SSH terminal should look something like this [see figure 6.1]

figure 6.1 – install nohup

Now let us start our node server again, but this time after exiting terminal our node server should be running in the background. to achieve that enter the following command to start and run node server in the background.

[../www/nodeApp]> nohup node nodeServer.js & <↵>

Now you can exit the SSH terminal, and check your browser. You will find that your node server is still running.

If you wish to stop your node server following command will help you.

> ps aux | grep node <↵ | find the process ID>

> kill -15 [process ID]

 

how to find the process ID of the running node server,[see figure 6.2]

figure 6.2 – kill node server proccess

The node server still can crash if it’s stdout and stderr is still pointed at the terminal. This means that if the node server writes to console.log or console.error it will receive a broken pipe error and crash. This can be avoided by piping the output of your process i.e. you should use following command to start your node server.

[../www/nodeApp]> nohup node nodeServer.js > stdout.txt 2> stderr.txt & <enter>

 

 

This will create the two files stdout.txt & stderr.txt in /www/nodeApp which will hold the logs and error from the node Application. If you make any changes to your nodeApplication you need to kill the nodeApplication first and then start it again.

Leave a Reply

You can contact me on +91 7021090754 for new project.