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.
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.
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.
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);});});
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With