在 Node.js Web 服务器中执行 PHP 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6542169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute PHP scripts within Node.js web server
提问by Daniel
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?
拥有 Node.js Web 服务器功能(如 Apache 执行 PHP 脚本)需要哪些步骤?有什么方法可以在 Node.js 中集成 PHP?
Note: I don't want to execute PHP scripts directly within Node.js but "routed" through an Apache instance or something similar.
注意:我不想直接在 Node.js 中执行 PHP 脚本,而是通过 Apache 实例或类似的东西“路由”。
采纳答案by jnbdz
Node.js only supports JavaScript. Here is a tutorial on how to have PHP running with Node.js on the side.
Node.js 仅支持 JavaScript。这是一个关于如何让 PHP 与 Node.js 一起运行的教程。
http://blog.mixu.net/2011/01/04/nginx-php-fpm-and-node-js-install-on-centos-5-5/
http://blog.mixu.net/2011/01/04/nginx-php-fpm-and-node-js-install-on-centos-5-5/
回答by Dave Causey
I had the same question. I tried invoking php through the shell interface, and it produced the desired result:
我有同样的问题。我尝试通过 shell 接口调用 php,它产生了想要的结果:
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:
我确信这在推荐的做法列表中并不高,但它似乎做了我想要的。另一方面,如果您不想直接从 Node.js 执行 PHP 脚本,而是想从另一个可以执行的 Web 服务器中继它们,这似乎可以解决问题:
var exec = require("child_process").exec;
app.get('/', function(req, res){exec("wget -q -O - http://localhost/", function (error, stdout, stderr) {res.send(stdout);});});
回答by Simon Rigét
You can run PHP as with any web-server, using the SPHP module for node.
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.
您可以像使用任何 Web 服务器一样运行 PHP,使用 node 的 SPHP 模块。
它是兼容的,但不依赖于 express。
它还支持 HTTP 端口上的 websockets 请求。
它偏向于小负载下的速度,而不是节省资源。
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
有关更多信息,请查看https://github.com/paragi/sphp
I'm slightly biased too because I'm the author :)
我也有点偏见,因为我是作者:)
回答by Nathan J.B.
Take a look here: https://github.com/davidcoallier/node-php
看看这里:https: //github.com/davidcoallier/node-php
From their read me:
从他们读我:
Inline PHP Server Running on Node.js
Be worried, be very worried. The name NodePHPtakes 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 Developersthis turns out to be quite an interesting project that we are most likely be going to use with Orchestrawhen we decide to release our Inline PHP serverthat 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.
在 Node.js 上运行的内联 PHP 服务器
担心,非常担心。该名NodePHP的名字来自一个事实,即我们正有效地将一个不错的Node.js服务器为FastCGI接口与PHP-FPM相互作用。
这是 omega-alpha-super-beta-proof-of-concept,但它已经运行了一些简单的脚本。主要是为了我关于Node.js for PHP Developers 的演讲,结果证明这是一个非常有趣的项目, 当我们决定发布允许人们在没有 Apache 的情况下运行 PHP 的内联 PHP 服务器时 ,我们很可能会与Orchestra一起使用, Nginx 或任何网络服务器。
是的,这与 Node.js 的所有想法和概念背道而驰,但其想法是能够直接从任何工作目录创建 Web 服务器,以允许开发人员比以前更快地开始工作。不再需要创建虚拟主机或服务器块或修改您的 /etc/hosts。
回答by Andrey Sidorov
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.
可以尝试实现直接链接节点-> fastcgi -> php。在前面的答案中,nginx 使用 http->fastcgi 序列化->unix socket->php 和节点请求作为 http->nginx 反向代理->节点 http 服务器来提供 php 请求。
It seems that node-fastcgi paseris 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.
似乎node-fastcgi paser目前可用,但只能作为 node fastcgi 后端。您需要采用它作为 fastcgi 客户端到 php fastcgi 服务器。
回答by Nivin
If php is in FPM mode node-phpfpm could be an option, check the documenation https://www.npmjs.com/package/node-phpfpm
如果 php 处于 FPM 模式 node-phpfpm 可能是一个选项,请检查文档https://www.npmjs.com/package/node-phpfpm
回答by Mick
回答by Martin
You can use node-php to run php with node js: https://github.com/mkschreder/node-php
您可以使用 node-php 运行带有 node js 的 php:https: //github.com/mkschreder/node-php
回答by Martin
You can serve PHP directly with node WAS: https://github.com/paragi/was
您可以直接使用节点 WAS 为 PHP 提供服务:https: //github.com/paragi/was
回答by Ivan Filho
You must check out node-php-fpm
.
你必须退房node-php-fpm
。