How to host a Node.Js application in shared hosting [closed]
Asked Answered
C

5

124

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?

Cassandracassandre answered 16/7, 2014 at 9:55 Comment(4)
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 cliGrab
this worked - shame I can't post an answer... a2hosting.com/kb/installable-applications/manual-installations/…Troublous
@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.Troublous
On most shared hosts installing Node and NPM will fail (CentOS Not Supported); here is a solution (using Node v6.2.2) - medium.com/@yatko/…Frigidarium
S
179

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.

Symphony answered 10/12, 2014 at 1:4 Comment(21)
and installing mongoDB too?Virge
@Virge It should be possible since mongodb doesn't need root. Just follow the manual install guide.Symphony
@Symphony What about in Windows with XAMPP?Amenable
What if Host provider know's this trick and removes our node instance an other files?Fuhrman
@user2118784: Windows doesn't have wget, kill, tar, etc. I may work with cygwin, but it's not supported. @Nirus: It depends on hosting Terms of Service, but usually your files belong to you and they cannot delete them.Symphony
Really Good One, But how to install Phantomjs using node.phpBillie
Be advised you shall update the download URL accordinglyKlemm
node.php is very good!Juvenility
@niutech, the page says to switch to admin mode, how do i do this?Heymann
@smerny By setting ADMIN_MODE to true on line 13 of node.phpSymphony
Error requesting node_modules/bower/: Failed to connect to 127.0.0.1 port 49999: Connection refusedKokoschka
@MiomirDancevic It means the node process didn't start. Try going through troubleshooting.Symphony
So I can use socket.io too? wow nice triks. hahaRemotion
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...Ichor
Fatal error: Call to undefined function curl_init() Aww. Do I have any chances?Posse
@kim366 See github.com/niutech/node.php/issues/32Symphony
won't shared services have restrictions against curl?Axon
@NevilleNazerane node.php uses PHP cURL, whch is enabled by most sharing hosts.Symphony
@knnhcn yes, I can confirm it worksDigestion
usleep(500000); half second of wait on each request?Venom
God this error when running http://example.org/node.php?npm=install jt-js-sample : Running: node/bin/npm --cache ./.npm -- install jt-js-sample Failed. Error: 127. See npm-debug.logIllume
C
72

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.

Cadet answered 22/11, 2015 at 16:49 Comment(12)
Hi, thanks for the answer. Although, I am running into trouble, it says that node is not found, once I do the install using nvm. Any help?Lorri
Maybe should you try https://mcmap.net/q/182147/-npm-not-found-when-using-nvm if you have that node not found error with nvmCadet
Most shared hosting providers don't give SSH access. At least not mine.Retraction
This worked on a shared host on hostgatorScuttlebutt
Worked perfectly!! ThanksPaske
hi...after i follow the first step...and then i try to run "nvm install stable"...but it said "nvm: command not found" ...what is the problem?Marvelous
Nodejs and Npm install complete. How to run app on domain?Jarmon
@SyamsoulAzrien if you got nvm:command not found you can run this: command source ~/.nvm/nvm.shKaroline
This config change to public_html/.htaccess worked: a2hosting.com/kb/installable-applications/manual-installations/…Troublous
the wget command failed to work for me on shared GoDaddy hosting, I had to download the install.sh file to my local system and manually upload to the server and then run bash install,sh @SyamsoulAzrien maybe try thisVaria
if the give command with wget doesn't work for you, try using curl >> curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bashMalleolus
LOLLL thanks millions of times !Hyperactive
S
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 [email protected]: wanted: {"node":"0.10.x"} (current: {"node":"0.12.4","npm":"2.10.1"})
[email protected] node_modules/jt-js-sample
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])

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.

Spirituous answered 31/5, 2015 at 18:11 Comment(3)
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?Ploy
that port is probably blocked, you should use the standard 80 portDysfunction
~/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:3Ivetteivetts
D
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.

Duchamp answered 12/9, 2015 at 5:49 Comment(4)
Thank you! Just what I was looking for.Lashay
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 hereEuphrates
does react and front end run well on shared node js hosting plan ? any ideaFlathead
Wrote a blog on the same. medium.com/@pampas93/…Detachment
S
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.

Skim answered 16/7, 2014 at 10:0 Comment(4)
do you mean it is not possible to host Node.js application using shared hostingCassandracassandre
@Cassandracassandre It is possible - see my answer.Symphony
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...Ichor
technically possible as @Symphony 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 soonSkim

© 2022 - 2024 — McMap. All rights reserved.