Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute PHP scripts within Node.js web server

Tags:

php

node.js

What steps are necessary to have a Node.js web server function like Apache executing PHP scripts? Any way to integrate PHP within Node.js?

Note: I don't want to execute PHP scripts directly within Node.js but "routed" through an Apache instance or something similar.

like image 688
Daniel Avatar asked Jun 30 '11 23:06

Daniel


People also ask

Can we run PHP in node JS?

You can run PHP as with any web-server, using the SPHP module for node. It's compatible but not dependent on express. It also supports websockets requests on the HTTP port.

Can you execute PHP in JavaScript?

You can't. PHP is executed server side, and the finished content is sent to the browser. Javascript just sees the end result. All you can do is make an AJAX call and get the results from that.


2 Answers

I had the same question. I tried invoking php through the shell interface, and it produced the desired result:

var exec = require("child_process").exec; app.get('/', function(req, res){exec("php index.php", function (error, stdout, stderr) {res.send(stdout);});}); 

I'm sure this is not high on the recommended practices list, but it seemed to do what I wanted. If, on the other hand, you don't want to execute PHP scripts directly from Node.js but want to relay them from another web server that does, this seems to do the trick:

var exec = require("child_process").exec; app.get('/', function(req, res){exec("wget -q -O - http://localhost/", function (error, stdout, stderr) {res.send(stdout);});}); 
like image 65
Dave Causey Avatar answered Sep 22 '22 09:09

Dave Causey


Node.js only supports JavaScript. Here is a tutorial on how to have PHP running with Node.js on the side.

http://blog.mixu.net/2011/01/04/nginx-php-fpm-and-node-js-install-on-centos-5-5/

like image 26
jnbdz Avatar answered Sep 23 '22 09:09

jnbdz