How to run PHP in Node.js, and why you (probably) shouldn’t do that

Image for post
Image for post

I’ve seen a lot of requests lately, on how to run PHP in Node.js. Some requests have been because people don’t know how to do the same things in Node.js as they do easy in PHP, and other requests have been because they need to work together with other teams who only program in PHP.

In every case I’ve seen the response, that it can’t be done. Which is factually inaccurate. Since you can run PHP on the command line, and Node.js can easily exec programs through the command line, it’s very much possible.

What are the benefits?

  • Easy collaboration across teams with different knowledge
  • PHP is really good at throwing fast scripts together
  • No need to run several servers if both Node.js and PHP are needed

What are the drawbacks?

  • More possibilites for security breaches
  • Not very good performance
  • Easy to shoot yourself in the foot
  • If you already know PHP and are learning node, you will probably have a tendancy to program the “hard” things in PHP, thus not learning the node way of doing it.

Of course there’s probably more drawbacks and benefits, but let’s hear about those in the comments ;)

What is this article?

Basically this article is a step 1. It’s not a complete solution. It’s just to show it can be done and how.

Let’s get started

I’ll skip telling you how to install Node. There’s lots of tutorials about that.

First thing you need to do is install PHP (if you haven’t already).
Just follow the standard instructions on and you should be fine.

Also, in this example I use Express. You don’t have to use Express to run PHP in Node.js, but let’s go with it for now.

npm install express — save

Now let’s take a look at webserver.js which will be the first file in our project.

As you can see, we made a pretty simple webserver. If you’ve worked with Express before, you already know what most of the example does.

The stuff you haven’t seen before is the requireing of the “execphp.js” which is where we will do the PHP parsing.

We’re also defining a route that recieves all *.php requests, and runs them through the execPHP class.

Note: Change the variable execPHP.phpFolder to wherever you want to put your own php files.

Now let’s take a look at “execphp.js” which will be the second file in our project.

As you can see, this file contains a class, ExecPHP, with a single method, parseFile. This method basically takes a file, executes the command line PHP and calls the passed callback with the result.

Note: Change the variable this.phpPath to wherever you have PHP installed.

Now let’s make a simple PHP file. I’ve called this “test.php” and put it in the “phpfiles” folder (the one you edited in webserver.js).

As you can see this is a very simple example.

Now let’s run the webserver.

node — use_strict webserver.js

And visit http://localhost:3000/test.php in our browser.

Woohoo, hopefully, you should get the result of the php file.

Hello worldA simple counter:

Some further problems, we’ll leave as exercises for the reader.

If you just use this script without editing it, you’ll quickly run into limitations. For example $_GET, $_REQUEST, etc. is not set. If you wanted to use this, you’d probably have Node.js write some fake PHP setting variables to whatever you want them to be, and processing that before the file itself.

Generally good ideas for furture development:

  • Make the execPHP an express-middleware itself.
  • Sanitize the filename to make sure people aren’t writing stuff like ../../../evilfile.php to access stuff they shouldn’t.
  • Handle $_GET, $_SERVER, $_REQUEST, .etc. automatically in the execPHP class, so those will be available in the php scripts.

Written by Martin Mouritzen.

Written by

Product Manager @ Trustpilot

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store