1

I have a nodejs application runned on heroku. I have to run console PHP script in it. I want to execute the script by child process - http://nodejs.org/api/child_process.html.

My question is - how can i enable both nodejs and php interpreter in heroku hosting?

this is index.js file of my application (simplified)

var http = require('http'),
  exec = require('child_process').exec;

http.createServer(function (req, res) {


  exec('php script.php',
    function (error, stdout, stderr) {
      if(error){
        throw error;
      } else {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end(stdout);
      }
    });

}).listen(process.env.PORT || 3000);

this is script.php file

<?php
echo "Hello, world!";

When i try to start my application, i has this response in logs

Sep 23 06:44:48 blooming-woodland-9158 app/web.1:  Error: Command failed: /bin/sh: php: not found 
Sep 23 06:44:48 blooming-woodland-9158 app/web.1:      at ChildProcess.exithandler (child_process.js:648:15) 
Sep 23 06:44:48 blooming-woodland-9158 app/web.1:      at ChildProcess.emit (events.js:98:17) 
Sep 23 06:44:48 blooming-woodland-9158 app/web.1:      at maybeClose (child_process.js:756:16) 
Sep 23 06:44:48 blooming-woodland-9158 app/web.1:      at Socket.<anonymous> (child_process.js:969:11) 
Sep 23 06:44:48 blooming-woodland-9158 app/web.1:      at Socket.emit (events.js:95:17) 
Sep 23 06:44:48 blooming-woodland-9158 app/web.1:      at Pipe.close (net.js:465:12) 

so it looks like it do not have the PHP binary installed

UPD: they have the buildpack for php https://github.com/heroku/heroku-buildpack-php i'll try to use it and share the result here

UPD1: buildpack do not allows to start nodejs for it I decided to build php binary like described here and ship it with my project - probably i can build the PHP binary and ship it with my project - Compile PHP into Static Binary

7
  • Why do you want php console on node.js? Commented Sep 23, 2014 at 13:44
  • 3
    because i has some really BIG legacy PHP script (2000 lines of code), that performs some wierd calculations, and i do not posses urge to rewrite it all in nodejs, i just need to run it by console, and capture the stdout Commented Sep 23, 2014 at 13:48
  • Why do you need to launch the php console from inside node.js so? Make a REST API from your php, make it answer in JSON, and use node.js to call the php server, parse the JSON and use it Commented Sep 23, 2014 at 13:51
  • for you approach i need to host the PHP script on the separate server, then make REST api from it... anyway, thanks for answer, but this is not the path i want to follow Commented Sep 23, 2014 at 13:56
  • 1
    You can do it on the same server, just listening a different port. Also, what happen if you start your code as if? Do you get Hello World? A time out? Nothing? A error? Commented Sep 23, 2014 at 13:57

2 Answers 2

2

I have fixed this issue by myself. https://github.com/vodolaz095/nodejs-php-heroku-example

I have build PHP binary, than i have included it into nodejs project and pushed to server. And it is running.

I do not imply, that this is the only and the best solution, but it do exactly what i want

So, i have posted an example how can i do it

Sign up to request clarification or add additional context in comments.

1 Comment

That seems awful... But glad to know you found your answer.
0

No php binary on a server you don t control, you cannot install it, so your code just can t work, you have the choice between:

  • Move out of heroku, install node.js and php on your server, and use this code (if you have a server)
  • Make your php rest-ful, started on another server, and get the result in another form (if you have a server and time)
  • Rewrite your php script in a format supported by heroku (if you have time)

There is no other choice, each have drawback, but unless you have a lot of money, I doubt heroku will be willing to install php on their server just for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.