How to host a Node.Js application in shared hosting
node.jsWeb Hostingnode.js Problem Overview
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?
node.js Solutions
Solution 1 - node.js
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:
-
Create a new PHP file on the server with the following code and run it:
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');
-
The same way install your node app, e.g. jt-js-sample, using npm:
-
Run the node app from PHP:
/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.
Solution 2 - node.js
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.
Solution 3 - node.js
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.
Solution 4 - node.js
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.
Solution 5 - node.js
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.