Execute PHP scripts within Node.js web server

Phpnode.js

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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);});});

Solution 2 - Php

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/

Solution 3 - Php

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.
Its biased for speed under small load, rather then saving resources.

To install in node:

npm install sphp

in you app:

var express = require('express');
var sphp = require('sphp');

var app = express();
var server = app.listen(8080);

app.use(sphp.express('public/'));
app.use(express.static('public/'));

For more information, look at https://github.com/paragi/sphp<br>

I'm slightly biased too because I'm the author :)

Solution 4 - Php

Take a look here: https://github.com/davidcoallier/node-php

From their read me:

> Inline PHP Server Running on Node.js > ==================================== > > Be worried, be very worried. The name NodePHP takes its name from > the fact that we are effectively turning a nice Node.js server into a > FastCGI interface that interacts with PHP-FPM. > > This is omega-alpha-super-beta-proof-of-concept but it already runs a > few simple scripts. Mostly done for my talks on Node.js for PHP > Developers this turns out to be quite an interesting project that we > are most likely be going to use with Orchestra > when we decide to release our > Inline PHP server that allows people to run PHP without Apache, Nginx or any webserver. > > Yes this goes against all ideas and concepts of Node.js but the idea > is to be able to create a web-server directly from any working > directory to allow developers to get going even faster than it was > before. No need to create vhosts or server blocks ore modify your > /etc/hosts anymore.

Solution 5 - Php

You can try to implement direct link node -> fastcgi -> php. In the previous answer, nginx serves php requests using http->fastcgi serialisation->unix socket->php and node requests as http->nginx reverse proxy->node http server.

It seems that node-fastcgi paser is useable at the moment, but only as a node fastcgi backend. You need to adopt it to use as a fastcgi client to php fastcgi server.

Solution 6 - Php

A simple, fast approach in my opinion would be to use dnode-php for that.

You can see a brief introduction here. Simple, quick and easy!

Solution 7 - Php

If php is in FPM mode node-phpfpm could be an option, check the documenation https://www.npmjs.com/package/node-phpfpm

Solution 8 - Php

You can use node-php to run php with node js: https://github.com/mkschreder/node-php

Solution 9 - Php

You can serve PHP directly with node WAS: https://github.com/paragi/was

Solution 10 - Php

You must check out node-php-fpm.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanielView Question on Stackoverflow
Solution 1 - PhpDave CauseyView Answer on Stackoverflow
Solution 2 - PhpjnbdzView Answer on Stackoverflow
Solution 3 - PhpSimon RigétView Answer on Stackoverflow
Solution 4 - PhpNathan J.B.View Answer on Stackoverflow
Solution 5 - PhpAndrey SidorovView Answer on Stackoverflow
Solution 6 - PhpMickView Answer on Stackoverflow
Solution 7 - PhpralixyleView Answer on Stackoverflow
Solution 8 - PhpMartinView Answer on Stackoverflow
Solution 9 - Phpuser3273253View Answer on Stackoverflow
Solution 10 - PhpIvan FilhoView Answer on Stackoverflow