123

How to host a Node.Js application in a shared hosting

I want to host a node.js application in shared hosting. Does anyone have any reference or documentation to refer to?

YetAnotherBot
  • 1,937
  • 2
  • 25
  • 32
somesh
  • 3,508
  • 4
  • 16
  • 10
  • The way i figured it by running npm build will generate js files which you can minify and then include then in your script tags. Example when building with vuejs or even angular2 cli – Geoff May 21 '17 at 12:49
  • 2
    this worked - shame I can't post an answer... https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts – eddyparkinson Mar 07 '18 at 02:10
  • 1
    @Dan-Cornilescu can the be re-opened. It is valuable. It would be better in superuser. Also found an answer that I would like to add. – eddyparkinson Mar 07 '18 at 02:18
  • On most shared hosts installing Node and NPM will fail (CentOS Not Supported); here is a solution (using Node v6.2.2) - https://medium.com/@yatko/install-node-js-on-shared-hosting-65fb08deffad – Yatko Sep 26 '18 at 17:07

5 Answers5

178

You can run node.js server on a typical shared hosting with Linux, Apache and PHP (LAMP). I have successfully installed it, even with NPM, Express and Grunt working fine. Follow the steps:

1) Create a new PHP file on the server with the following code and run it:

<?php
//Download and extract the latest node
exec('curl http://nodejs.org/dist/latest/node-v0.10.33-linux-x86.tar.gz | tar xz');
//Rename the folder for simplicity
exec('mv node-v0.10.33-linux-x86 node');

2) The same way install your node app, e.g. jt-js-sample, using npm:

<?php
exec('node/bin/npm install jt-js-sample');

3) Run the node app from PHP:

<?php
//Choose JS file to run
$file = 'node_modules/jt-js-sample/index.js';
//Spawn node server in the background and return its pid
$pid = exec('PORT=49999 node/bin/node ' . $file . ' >/dev/null 2>&1 & echo $!');
//Wait for node to start up
usleep(500000);
//Connect to node server using cURL
$curl = curl_init('http://127.0.0.1:49999/');
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//Get the full response
$resp = curl_exec($curl);
if($resp === false) {
    //If couldn't connect, try increasing usleep
    echo 'Error: ' . curl_error($curl);
} else {
    //Split response headers and body
    list($head, $body) = explode("\r\n\r\n", $resp, 2);
    $headarr = explode("\n", $head);
    //Print headers
    foreach($headarr as $headval) {
        header($headval);
    }
    //Print body
    echo $body;
}
//Close connection
curl_close($curl);
//Close node server
exec('kill ' . $pid);

Voila! Have a look at the demo of a node app on PHP shared hosting.

EDIT: I started a Node.php project on GitHub.

niutech
  • 28,923
  • 15
  • 96
  • 106
70

Connect with SSH and follow these instructions to install Node on a shared hosting

In short you first install NVM, then you install the Node version of your choice with NVM.

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

Your restart your shell (close and reopen your sessions). Then you

nvm install stable

to install the latest stable version for example. You can install any version of your choice. Check node --version for the node version you are currently using and nvm list to see what you've installed.

In bonus you can switch version very easily (nvm use <version>)

There's no need of PHP or whichever tricky workaround if you have SSH.

vinyll
  • 11,017
  • 2
  • 48
  • 37
13

I installed Node.js on bluehost.com (a shared server) using:

wget <path to download file>
tar -xf <gzip file>
mv <gzip_file_dir> node

This will download the tar file, extract to a directory and then rename that directory to the name 'node' to make it easier to use.

then

./node/bin/npm install jt-js-sample

Returns:
npm WARN engine jt-js-sample@0.2.4: wanted: {"node":"0.10.x"} (current: {"node":"0.12.4","npm":"2.10.1"})
jt-js-sample@0.2.4 node_modules/jt-js-sample
└── express@4.12.4 (merge-descriptors@1.0.0, utils-merge@1.0.0, cookie-signature@1.0.6, methods@1.1.1, cookie@0.1.2, fresh@0.2.4, escape-html@1.0.1, range-parser@1.0.2, finalhandler@0.3.6, content-type@1.0.1, vary@1.0.0, parseurl@1.3.0, serve-static@1.9.3, content-disposition@0.5.0, path-to-regexp@0.1.3, depd@1.0.1, qs@2.4.2, on-finished@2.2.1, debug@2.2.0, etag@1.6.0, proxy-addr@1.0.8, send@0.12.3, type-is@1.6.2, accepts@1.2.7)

I can now use the commands:

# ~/node/bin/node -v
v0.12.4

# ~/node/bin/npm -v
2.10.1

For security reasons, I have renamed my node directory to something else.

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
iiboone.com
  • 141
  • 1
  • 6
  • 1
    I tried `npm start` in the `jt-js-sample` and I went to mysite.com:5000 but it said page not available. Do I need to go to the IP address instead? – zachdyer Jun 05 '15 at 17:54
  • that port is probably blocked, you should use the standard 80 port – RicardoE Nov 13 '17 at 17:48
  • ~/node/bin/npm -v gives me an error saying: let notifier = require('update-notifier')({pkg}) SyntaxError: Unexpected identifier at exports.runInThisContext (vm.js:69:16) at Module._compile (module.js:432:25) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:349:32) at Function.Module._load (module.js:305:12) at Function.Module.runMain (module.js:490:10) at startup (node.js:123:16) at node.js:1027:3 – codeinprogress Jun 08 '18 at 05:37
12

A2 Hosting permits node.js on their shared hosting accounts. I can vouch that I've had a positive experience with them.

Here are instructions in their KnowledgeBase for installing node.js using Apache/LiteSpeed as a reverse proxy: https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts . It takes about 30 minutes to set up the configuration, and it'll work with npm, Express, MySQL, etc.

See a2hosting.com.

aap
  • 897
  • 11
  • 15
  • 1
    Thank you! Just what I was looking for. – Pedro Ferreira Apr 27 '17 at 00:20
  • 1
    Almost two years later, A2 Hosting is still one of the very few hosting providers _officially_ supporting Node.js on their shared servers. A recent getting started guide can be found [here](https://www.ionicrun.com/running-a-node-js-application-on-shared-hosting/) – Dalie Jan 16 '18 at 19:21
  • 1
    does react and front end run well on shared node js hosting plan ? any idea – Samkit Shah May 04 '18 at 13:33
  • Wrote a blog on the same. https://medium.com/@pampas93/host-your-node-js-app-on-shared-hosting-go-beyond-localhost-73ab923e6691 – Abhijit S May 27 '20 at 19:49
3

You should look for a hosting company that provides such feature, but standard simple static+PHP+MySQL hosting won't let you use node.js.

You need either find a hosting designed for node.js or buy a Virtual Private Server and install it yourself.

Marek
  • 1,413
  • 2
  • 20
  • 36
  • do you mean it is not possible to host Node.js application using shared hosting – somesh Jul 16 '14 at 10:04
  • 3
    @somesh It is possible - see my answer. – niutech Dec 10 '14 at 01:04
  • 2
    It's a cool trick, though keep in mind a lot of shared hosting providers will kill this process and consider it misuse of a shared hosting account... – Tiago Feb 06 '17 at 17:16
  • 2
    technically possible as @niutech said, but I definitely wouldn't consider it production safe, other colleague (cannot mention more than one user) is right, I believe most hosting providers will kill the node process or you would run out of processor cycle time very soon – Marek Feb 06 '17 at 23:15