I would like to run Node.js/Express as a backend server mainly for handling WebRTC features in my app.
I was just discussing this with my hosting provider and they rejected this request saying that Node.js is heavy and requires Java thus making it impossible to run on Apache so I need to upgrade my account to VPS.

Google tells me the opposite and many people are running Node.js on Apache.

Is the above true or are they trying to take advantage of me?

  • 7
    Node.js does not require Java. There is no problem running a Node.js app on a sever that is also running Apache. BUT... despite your hosting provider being poorly informed about Node, I'd agree with their decision - for shared hosting, I don't think I'd allow node.js apps to run since they are running as server applications and a poorly designed one can drag a server down to a crawl. You'd be better with a VPS - and there are plenty of inexpensive VPS hosts available. – GrandmasterB Sep 22 '14 at 5:13
  • I see.. I will have look for an alternative, thank alot – Awena Sep 22 '14 at 5:30

Actually you can run node.js on a typical shared hosting with Linux, Apache and PHP. Even NPM, Express and Grunt work fine. Here are the necessary steps:

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

//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) Install your node app, e.g. jt-js-sample, using npm:

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

3) Run the node app from 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=81 node/bin/node ' . $file . ' >/dev/null 2>&1 & echo $!');
//Wait for node to start up
//Connect to node server using cURL
$curl = curl_init('');
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) {
    //Print body
    echo $body;
//Close connection
//Close node server
exec('kill ' . $pid);

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

EDIT: Here is my new project Node.php on GitHub.

| improve this answer | |
  • 1
    While technically this is a way of running node in a shared hosting environment, it's not a very good idea, because (1) it will run 1 process per request, which means you don't benefit from node's ability to handle multiple requests in a single thread using asynchronous io, (2) you get a lot of memory overhead from the jit but don't get to benefit from it because your app doesn't run for long enough, and (3) your host is quite likely to spot what you're doing and put restrictions on your account to prevent you doing it. – Jules Dec 10 '14 at 11:50
  • 1
    Also, the code you present is highly unlikely to run correctly in a shared hosting environment, because I've yet to see one that runs on Linux (as almost all do) and allows users to bind port 81, even on localhost. And even if they did, only one process can do so at a time, so two simultaneous requests would cause your script to fail. – Jules Dec 10 '14 at 11:53
  • 2
    @Jules I agree it's not the most efficient way, but anyway you cannot expect much from shared hosting. If binding to port 81 doesn't work, try higher ranges. Mine works fine as it is only bound to localhost. Otherwise one could experiment with named pipes. As for multiple requests, the script could check if node is already running (keeping its pid) and start it only if it is not. Anyway, this is a clever method to try node.js apps on dirt cheap shared hosting, without bothering with PaaS or expensive node.js dedicated hosting. – niutech Dec 10 '14 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.