Home » Nodejs » Execute PHP scripts within Node.js web server

Execute PHP scripts within Node.js web server

Posted by: admin November 7, 2017 Leave a comment

Questions:

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.

Answers:

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/

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

Questions:
Answers:

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.

Questions:
Answers:

Snappy PHP is a node module, for running server side PHP.
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
I should mention that I’m the author 🙂

Questions:
Answers:

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.

Questions:
Answers:

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!

Questions:
Answers:

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

Questions:
Answers:

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

Questions:
Answers:

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

Leave a Reply

Your email address will not be published. Required fields are marked *