This page looks best with JavaScript enabled

Install Node.js and NPM on hosting server

 ·  ☕ 3 min read  ·  ✍️ Adesh

This blog will show, how to setup Node.js and NPM on your managed hosting server. We are going to host in Linux server.

Before moving forward, please keep your SSH credentials in hand. We are going to use SSH tool to install Node.js and NPM.

Prerequisites

  • Your hosting account must have SSH (not jailshell) enabled. To check which shell your account is using, login your SSH account and type the following command:
1
2
zeptobook@domain [~]# echo $SHELL
/bin/bash
  • Node.js currently only works on servers running CentOS 6 or CentOS 7. To check the version of CentOS on your server, log in to your account using SSH, and then type this command:
1
2
zeptobook@domain [~]# uname -r
3.10.0-714.10.2.lve1.5.19.7.el7.x86_64

If the output from this command contains el6 (for example, 2.6.32-531.17.1.lve1.2.60.el6.x86_64) or el7 (for example, 3.10.0-714.10.2.lve1.5.12.el7.x86_64) , then your server is running CentOS 6 or CentOS 7.

However, if you see el5h (for example, 2.6.32-531.23.3.lve1.2.66.el5h.x86_64), then your server is running CentOS 5 and does not support Node.js. Please contact your hosting provider.

Installing Node.js and NPM

  • After your account meets the installation prerequisites, you can download and install Node.js and NPM (the Node.js package manager). To do this, follow these steps:
1
2
zeptobook@domain [~]# cd ~
zeptobook@domain [~]# wget https://nodejs.org/dist/v10.5.0/node-v10.5.0-linux-x64.tar.xz

This will save node.js package on your server directory.

1
2
3
4
zeptobook@domain [~]# ls
access-logs@  node-v10.5.0-linux-x64.tar.xz  public_html/  tmp/
etc/          perl5/                         srbackups/    www@
mail/         public_ftp/                    ssl/
  • To extract the Node.js files, type this command:
1
zeptobook@domain [~]# tar xvf node-v10.5.0-linux-x64.tar.xz
  • To rename the extracted folder to the more convenient nodejs name, type this command:
1
2
3
4
5
6
zeptobook@domain [~]# mv node-v10.5.0-linux-x64 nodejs
zeptobook@domain [~]# mv node-v10.5.0-linux-x64 nodejs
zeptoboo@az1-ss6 [~]# ls
access-logs@  nodejs/                        public_ftp/   ssl/
etc/          node-v10.5.0-linux-x64.tar.xz  public_html/  tmp/
mail/         perl5/                         srbackups/    www@
  • To install the node and npm binaries, type these commands (may not be a required step in your setup):
1
2
3
4
zeptobook@domain [~]# mkdir ~/bin
zeptobook@domain [~]# cp nodejs/bin/node ~/bin
zeptobook@domain [~]# cd ~/bin
zeptobook@domain [~/bin]# ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
  • After you run these commands, Node.js and NPM are installed on your account. To verify this, type these following commands:
1
2
3
4
zeptobook@domain [~/bin]# node --version
v10.5.0
zeptobook@domain [~/bin]# npm --version
6.1.0
The ~/bin directory is in your path by default, which means you can run node and npm from any directory in your account.

Creating your Node.js application on server

After successful installation of Node.js and NPM on your service, next step is create a node.js application. So, I have created a subdomain services.zeptobook.com, and put the node.js app there.

Create a server.js file on local, and upload it to the subdomain directory.

server.js

1
2
3
4
5
6
7
8
var http = require('http');

    http
        .createServer(function (req, res) {
            res.writeHead(200, { 'Content-Type': 'text/plain' });
            res.write('Hello Node.js Server @ A2 Shared Hosting!');
            res.end();
        }).listen(49500);

Now, go to SSH terminal, and go to your subdomain directory, and run this command.

1
zeptobook@domain [~/services.zeptobook.com]# node server.js

Now, go to the PostMan tool, and access your service url.

GET http://services.zeptobook.com:49500/

service call

This is how, you can setup Node.js and NPM on your linux server.

Summary

In this blog, we learned about setting up Node.js and NPM on Linux hosting server.

Read more:

Share on

Adesh
WRITTEN BY
Adesh
Technical Architect