How to install node js and setup a "hello world" website on a shared linux hosting?

1k views Asked by At

I am new with Node JS, I was able to install and setup node js on my local windows machine and also a simple http server which returned "hello world" text by reading on the internet - (blogged about it here).

Now I want to setup and install node on my linux hosting and set up a hello world page there too.

I am not sure how to go about this. Is there any feature in cPanel which allows for this ?

Please advice.

P.S I use shared hosting.

2

There are 2 answers

0
Mayur On

It can be done if you have ssh access with root priviledges. See the link below:

http://outofcontrol.ca/blog/comments/installing-node-bower-and-gulp-on-a-cpanel-box

0
nebulr On

You don't need root privileges to do this. I have successfully been able to set it up a couple different ways. I think the second is probably what you want :

1. cgi-node http://www.cgi-node.org/home

Basically this replaces PHP on the lamp stack. You can run javascript through node like you would run PHP. This has all the same functionality of node js but is only really geared towards template rendering.

    <html>
    <body>
     <?
       var helloWorld = 'Hello World!'; 
       write(helloWorld + '<br/>'); 
     ?>
     <?= helloWorld ?>
    <br/>
    <b>I can count to 10: </b>

    <?
      for (var index= 0; index <= 10; index++) write(index + ' ');  
    ?>
      <br/>
      <b>Or even this: </b>
    <?  
      for (var index= 0; index <= 10; index++) { 
    ?>
        <?= index ?> 
    <? } ?>

    </body>
</html>

OR

2. Standalone Server (this works with NameCheap hosting and GoDaddy shared hosting)

In your shared hosting account you will need SSH in order to do this. So you may need to upgrade or request SSH access from their customer support. Download the latest NodeJS https://nodejs.org/en/download/. The shared hosting is probably in linux 64 bit. You can check this on linux or unix by running :

uname -a

Download the Linux binaries and put the bin/node (and the bin/npm file if you want to use npm on the server) file from the download in /home/username/bin/ (create the bin folder if it doesn't exist) on the server. Put permissions 755 on the node binary. So you should have a new file here :

/home/username/bin/node

Open up the .htaccess file in /home/username/public_html and add the following lines :

RewriteEngine on
RewriteRule  (.*)  http://localhost:3000/$1  [P,L] 

Create a file in /home/username/public_html and just call it app.js. Add the following lines in that file :

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('NodeJS server running on Shared Hosting\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

SSH into the server run these commands :

cd /home/username/public_html
which node # this should return ~/bin/node
node app.js & # This will create a background process with the server running

If you can get this set up right this will save you a ton of money in the long run as opposed to using something like AWS or Heroku etc.