PHP with NodeJS


#1
  • Pretty much the title. How can I run PHP and Node.js at the same time?

I want to put this here.. Please help!


#2

Hey @RazorApple I’m not 100% sure I understand what you’re trying to accomplish, but the most basic answer is “you can’t”.

You might be able to construct a bash start script that could start up apache running php similarly to the way https://glitch.com/~lamp-poc does it and also start a node server running your NodeJS code as well, but you won’t be able to have both of those serving requests from the internet - only one server will be able to listen to process.env.PORT at a time, and Glitch will only serve requests from the server listening there.

Depending on what you’re trying to do there may be another workaround you could try. For instance, you could probably serve https://ohyeahyeah.ga/ inside an <iframe> on https://paulbot.glitch.me/, although depending on what https://ohyeahyeah.ga/ is trying to do you may run into cross-origin problems.

Hope this helps!


#3

You can manually download the php binaries for Linux, make sure to extract the binaries inside your app directory (home I think). Then use http, or express to listen for incoming requests, execute the php binary and extract the result from the execution then send that as a response.

You can use child_process api for executing php and fetching the data from the binary.


#4

You can make express execute .php files if someone requests it. e.g. (https://someproject.glitch.me/page.php). Otherwise, PHP binaries are pre-installed in the Glitch container you can access them with the php file/command.

I have an express server that can execute PHP pages/files.

Here’s the code I used:

Note: The express and php-express NPM/PNPM packages must be installed

const express = require('express');
var app = express();

var phpExpress = require('php-express')({

  // php is in your PATH
  binPath: 'php'
});

app.engine('php', phpExpress.engine);
app.set('view engine', 'php');

var listener = app.listen(process.env.PORT, function () {
  console.log('Your app is listening on port ' + listener.address().port);
});

If the example isn’t working for some reason let me know :smiley:


#6

Thanks, Ill go to school and I will test this later when I have time!


#7

i’m sorry for being inactive! i dont need this anymore but will still bookmark this for when i need it.