Git Product home page Git Product logo

node-php's Introduction

NodeCGIEmbedded - run php scripts like wordpress, drupal, etc with node and cgi counter parts

With Node PHP Embedded you can leverage the speed of node js and run all of the widely available php scripts directly inside your express site.

This is a fork of http://github.com/mkschreder/node-php and has been modified for making this take dynamic PHP pathing, so that it can run without a PHP distribution installed on a machine and can work with an embedded PHP binary distribution.

Installation

npm install phpcgijs --save

Usage

To run php scripts with node js and express create the following script like below:

var express = require('express');
var php = require("./main");
// var php = require("phpcgijs"); 
var path = require("path");

var app = express();
var p = path.join("test/php");

// Following is the structure for providing the declaration of paths and options:

// app.use("/", php.cgi(
//             "/path/to/phpscript.php", 
//             {
//                 "options": {"-c": "/to/php/ini/path/php.ini"}
//             }
//         ));

// Following works without a local PHP-CGI path and tries to 
//          use PHP-CGI installed in system by default:

// app.use("/", php.cgi("/path/to/phpscript")); 

// Following uses a path in second argument defining the local copy of 
//          PHP-CGI that you want to use for the application

// app.use("/", php.cgi(
//             "/path/to/phpscript.php", 
//             {
//                 "cgi_path":"to/php/cgi/path/php-cgi",
//                 "options": {"-c": "/to/php/ini/path/php.ini"}
//             }
//         ));

// options are PHP-CGI command line options and can be found in documentation
// It can also be found in readme-php-options.txt (check for update in docs)
// options ignore -h and --help

app.use("/", php.cgi(p, { cgi_path: '/usr/bin/', options: { "-c": "/etc/php.ini" } }));
app.listen(9090, '127.0.0.1');
console.log("Server listening at 9090!");

Explanation

The script will pipe all files that end in the .php extension through the php parser. All other files will be served as static content.

Basic permalinks are supported but the support for them can probably be improved.

Dependencies

php-cgi

  • You need to have the interpreter installed in the system in order to use this extension.
  • Alternatively, You can specify the full path of locally available php-cgi path.
  • If custom path not specified in express, it tries to find the system installed php-cgi executable. If still unavailable, the server errors out.
  • TODO:
    • Add php.ini path config
    • app.use("/", php.cgi("/path/to/phpscript", "to/php/cgi/path", '/path/to/php.ini'));

License

Copyright © 2019 - till it works Ganesh B [email protected]

The MIT License (MIT) - See LICENSE for further details.

node-php's People

Contributors

addyh avatar alangecker avatar ganeshkbhat avatar mkschreder avatar nhubbard avatar philippsimon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-php's Issues

php CLI

supports php cli environment?

Typo in Usage example of your README

No biggie but I believe in your example on your README where you have:

 var php = require("php");

It really should be:

 var php = require("node-php");

Use without express?

I need a quick CLI to run WordPress for testing and had already hacked https://github.com/brodybits/node-php-serve which uses node-php-server. I found this project through https://github.com/alangecker/express-php, which seems to be a more compact rewrite in CoffeeScript. (It references your project as the source of the "env" headers fixing code.) It would be nice if I could use this project without express.

A couple of other issues:

For some reason, I had to modify my copy of main.js to use /usr/local/bin/php-cgi instead of php-cgi. I wonder if the php-cgi path (absolute path and/or search path) should be an option (with a default value)?

Another minor issue is that when I run this project with WordPress, I get the following warnings:

express deprecated res.send(body, status): Use res.status(status).send(body) instead node_modules/node-php/main.js:115:13
express deprecated res.sendfile: Use res.sendFile instead node_modules/node-php/main.js:120:12

I did not get these errors with express-php.

I suspect this will need some modifications-please correct me in case I am mistaken. I can think of the following possibilities, assuming that I am right, in increasing order of preference:

  • I keep my own fork without the express part
  • Make the express part optional in this project
  • Drop the express support from this project and either give some shim code or point to express-php for those who need the express support

only last cookie is sent

if php's setcookie is called multiple times per response, only the last cookie will be included in the response.

for example, the response from the following php code will include only the "bbb" cookie.

<?php
setcookie("aaa", "xxx");
setcookie("bbb", "zzz");

for contrast, the following express code works as expected.

var express = require('express');
var app = express();
app.get('/', function(req, res){
  res.cookie('aaa', 'zzz');
  res.cookie('bbb', 'xxx');
  res.send('z');
});
app.listen(9090);

this prevents wordpress from functioning as its authentication process sets multiple cookies in a single request. (cf wp_set_auth_cookie in wp-includes/pluggable.php.)

i have tried using the version of node-php currently available from npm as well as the current head on github with the same results. am i doing something wrong?

edit: in case it's relevant, here is the code that i am using to start the server:

var express = require('express');
var php = require("node-php");
var app = express();
app.use("/", php.cgi(__dirname+"/cookietest"));
app.listen(9090);

edit2: and just in case this is relevant...

$ php-cgi --version
PHP 5.5.9-1ubuntu4.20 (cgi-fcgi) (built: Oct  3 2016 13:02:14)
...

edit3: i've also confirmed that the above php code works as expected when served by invoking the php executable as follows

$ php -S 127.0.0.1:9090 -t ./cookietest/

Express Deprecated Errors

Hi, I seem to be getting some errors from express indicating that some of the functions your code is accessing are deprecated.

express deprecated res.sendfile: Use res.sendFile instead node_modules\node-php\main.js:120:12
express deprecated res.send(body, status): Use res.status(status).send(body) instead node_modules\node-php\main.js:115:13

Cannot find module 'php'

nwgat@sever:~/test/node-php/tests$ nodejs express.js

module.js:340
throw err;
^
Error: Cannot find module 'php'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (/home/nwgat/test/node-php/tests/express.js:2:11)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)

ERR_CONTENT_DECODING_FAILED

I keep getting the error ERR_CONTENT_DECODING_FAILED in my local development area when view a page. Searching around the internet says to set zlib.output_compression = On in php.ini so that its on. I also made sure that there is only one variant of php.ini in the folder, but I'm still getting the error. Help?

Deprecated express method

node-php is using a deprecated express method.

express deprecated res.send(body, status): Use res.status(status).send(body) instead node_modules\node-php\main.js:115:13                       
express deprecated res.sendfile: Use res.sendFile instead node_modules\node-php\main.js:120:12                                                  

Error: spawn php-cgi ENOENT

I am getting this error message when I tried run a php script with this;

{ Error: spawn php-cgi ENOENT
    at exports._errnoException (util.js:1026:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:182:32)
    at onErrorNT (internal/child_process.js:348:16)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
  code: 'ENOENT',
  errno: 'ENOENT',
  syscall: 'spawn php-cgi',
  path: 'php-cgi',
  spawnargs: [] }

Appreciate if one has an idea what cause this error. :)

EPIPE error

Can't seem to get module to work, I always get:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: write EPIPE
    at errnoException (net.js:901:11)
    at Object.afterWrite (net.js:718:19)

Multi-Threaded (Worker API) Architecture instead of child process Spawn

There is a new worker multi-threading feature that is in experimental stage. Do you want to explore changing the child process spawn into multi-threaded architecture for the package instead of multi-process architecture. I would love to collaborate if you want. I am sure it will be more performant. Right now, applications like drupal work with the package. But it gives a choppy feeling due to the lag/cost of spawning and getting the result. The process is basically getting the entire result and once received it sends the data - performance hit. It is spawning a child process for every request also which will move into a performance issue soon - second performance hit.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.