How to execute PHP scripts with Node.JS

1. CLI Mode

You can run CLI commands to execute a PHP script. You can pass data to the PHP script by sending the $_ENV variable. Example:

var spawn = require('child_process').spawn;
var php = spawn('/usr/bin/php',['/path/to/php/script.php'],
		foo: 'bar'
php.stdout.on('data', function(data)

In your PHP script, you can get the data by $_ENV variable

2. Use PHP-FPM 

PHP-FPM is a fastcgi process manager of PHP. Fastcgi is a standard communication protocol. So we can use PHP to connect to a port, then send php script information, then PHP-FPM will run the script for you. 

I've created a NPM module `node-phpfpm`  to connect PHP-FPM server

var PHP = require('node-phpfpm');
var php = new PHP({ host:'', port:9000 });'test.php', function(err, output, errors)

Checkout more information of this module:

Why do we need PHP-FPM?

Because starting a php process by CLI mode is a really heavy CPU operation. From my test results, it can only run 30 PHP scripts at the same time on my Macbook Pro. 

But with PHP-FPM, the number of scripts executing at the same time rise to 600 !

-- END --