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.
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.
Step: 2 – Download & Install NVM (Node Version Manager)
Step: 3 – Installing Node.js
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.
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 <↵>
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.
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]
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
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.
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.