2

I am quite new to node.js programming. I am using linux dedicated server. I already have installed node.js in my server(linux).

I am using follwing php program to run the linux command so i can run node.js program by these linux command

test.php:

exec('node test.js &', $a1,$a2);
print_r($a1);
echo $a2;

And after running this program it showing following o/p:

Array ( ) 0

But not returns any value in $a1 variable where it should display "Hello World"

test.js:

var http = require("http");
http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8888);

I also tried with port 3000 and 8000.

5
  • why you are running nodejs with php script? Commented Jul 3, 2014 at 8:56
  • what gives you var_dump(function_exists('pcntl_fork')); ? Commented Jul 3, 2014 at 8:59
  • where's the part that talks to the node http server and catches the response? Commented Jul 3, 2014 at 9:00
  • My project has such requirement. Actually I want presentation application, there will be a controller and n no of users at the same slide (say 10k or above). any other suggestions? Commented Jul 3, 2014 at 9:02
  • @dandavis:As I said I am new to nodejs. The same code runs on my local machine (Windows OS) but not on server(Linux) Commented Jul 3, 2014 at 9:04

1 Answer 1

3

For once I cannot see how the same code runs on Windows, but not on Linux. Especially since your php contains UNIX syntax.

Please do not use PHP as a bridge to Node.js, it just kills all the point of having Node.js in first place.


What you need to do is run a command in the Shell of that linux machine:

node test.js

It will start a process, do not exit it.

Once the process is running, you shall be able to navigate to your Hello World through a browser, the URL will be based on your Server's IP and port you chosen, example:

http://1.2.3.4:8888/

If that does not answer your question, then clarify please, on what exactly you want to achieve.

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

4 Comments

I did it on linux local server and it worked. But I need to get it run on linux dedicated hosting server where I cannot access shell. Is there any other way to run it via PHP? I am developing a live presentation web application (something like presentation.io), presentor will control the slides and users will be on the same slide. I read node.js can do this job. Let me know if there is any other option. Please advise.
How did you install node.js without shell access?
It installed by linux server tech-support team.
If I run follwing php program then it gives o/p as: – Array ( [0] => Hello World ) 0 (It mean this Hello World program is running successfully) PHP Program: exec('node test.js &', $a1,$a2); print_r($a1); echo $a2; test.js program console.log("Hello World");

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.