简体   繁体   English

在 apache 中运行 Node.js?

[英]Running Node.js in apache?

We have an Apache Webserver installed on a machine which also serves pages using Perl.我们在一台机器上安装了一个 Apache Webserver,它也使用 Perl 提供页面。

For a project I've decided to use Node.js instead of Perl/Ruby.对于一个项目,我决定使用 Node.js 而不是 Perl/Ruby。 Just wondering if it's possible to use Apache as my webserver (so it serves the pages) and use Node.js to dynamically create the web pages (this is for a web app I am creating)?只是想知道是否可以使用 Apache 作为我的网络服务器(因此它为页面提供服务)并使用 Node.js 动态创建网页(这是我正在创建的网络应用程序)?

So in other words can they work hand in hand just like Apache/Perl or Apache/PHP etc..因此,换句话说,它们可以像 Apache/Perl 或 Apache/PHP 等一样携手合作。

Hosting a nodejs site through apache can be organized with apache proxy module.可以使用 apache 代理模块组织通过 apache 托管 nodejs 站点。

It's better to start nodejs server on localhost with default port 1337最好在本地主机上使用默认端口 1337 启动 nodejs 服务器

Enable proxy with a command:使用命令启用代理:

sudo a2enmod proxy proxy_http

Do not enable proxying with ProxyRequests until you have secured your server.在您保护服务器之前,不要使用 ProxyRequests 启用代理。 Open proxy servers are dangerous both to your network and to the Internet at large.开放代理服务器对您的网络和整个 Internet 都是危险的。 Setting ProxyRequests to Off does not disable use of the ProxyPass directive.将 ProxyRequests 设置为 Off 不会禁用 ProxyPass 指令的使用。

Configure /etc/apche2/sites-availables with配置 /etc/apche2/sites-availables

<VirtualHost *:80>
    ServerAdmin admin@site.com
    ServerName site.com
    ServerAlias www.site.com 

    ProxyRequests off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    <Location />
        ProxyPass http://localhost:1337/
        ProxyPassReverse http://localhost:1337/
    </Location>
</VirtualHost>

and restart apache2 service.并重新启动 apache2 服务。

No. NodeJS is not available as an Apache module in the way mod-perl and mod-php are, so it's not possible to run node "on top of" Apache.不可以。NodeJS 不能像 mod-perl 和 mod-php 那样作为 Apache 模块使用,因此不可能在 Apache 的“之上”运行节点。 As hexist pointed out, it's possible to run node as a separate process and arrange communication between the two, but this is quite different to the LAMP stack you're already using.正如 hexist 指出的那样,可以将 node 作为单独的进程运行并安排两者之间的通信,但这与您已经使用的 LAMP 堆栈完全不同。

As a replacement for Apache, node offers performance advantages if you have many simultaneous connections.作为 Apache 的替代品,如果您有许多同时连接,node 会提供性能优势。 There's also a huge ecosystem of modules for almost anything you can think of.还有一个庞大的模块生态系统,几乎可以满足您的任何想法。

From your question, it's not clear if you need to dynamically generate pages on every request, or just generate new content periodically for caching and serving.从您的问题来看,不清楚您是否需要针对每个请求动态生成页面,或者只是定期生成新内容以进行缓存和服务。 If its the latter, you could use separate node task to generate content to a directory that Apache would serve, but again, that's quite different to PHP or Perl.如果是后者,您可以使用单独的节点任务将内容生成到 Apache 将服务的目录中,但同样,这与 PHP 或 Perl 完全不同。

Node isn't the best way to serve static content. Node 不是提供静态内容的最佳方式。 Nginx and Varnish are more effective at that. Nginx 和 Varnish 在这方面更有效。 They can serve static content while Node handles the dynamic data.它们可以提供静态内容,而 Node 处理动态数据。

If you're considering using node for a web application at all, Express should be high on your list.如果您完全考虑将 node 用于 Web 应用程序,Express 应该在您的列表中名列前茅。 You could implement a web application purely in Node, but Express (and similar frameworks like Flatiron, Derby and Meteor) are designed to take a lot of the pain and tedium away.您可以完全在 Node 中实现 Web 应用程序,但 Express(以及类似的框架,如 Flatiron、Derby 和 Meteor)旨在消除很多痛苦和乏味。 Although the Express documentation can seem a bit sparse at first, check out the screen casts which are still available here: http://expressjs.com/2x/screencasts.html They'll give you a good sense of what express offers and why it is useful.尽管 Express 文档一开始看起来有点稀疏,但请查看此处仍然可用的屏幕转换: http : //expressjs.com/2x/screencasts.html他们会让您对 express 提供的内容以及原因有很好的了解它是有益的。 The github repository for ExpressJS also contains many good examples for everything from authentication to organizing your app. ExpressJS 的 github 存储库还包含许多很好的示例,从身份验证到组织您的应用程序。

Although there are a lot of good tips here I'd like to answer the question you asked:虽然这里有很多很好的提示,但我想回答您提出的问题:

So in other words can they work hand in hand just like Apache/Perl or Apache/PHP etc..因此,换句话说,它们可以像 Apache/Perl 或 Apache/PHP 等一样携手合作。

YES , you can run Node.js on Apache along side Perl and PHP IF you run it as a CGI module.是的,你可以,如果你运行它作为一个CGI模块上沿侧Perl和PHP运行Apache Node.js的。 As of yet, I am unable to find a mod-node for Apache but check out: CGI-Node for Apache here http://www.cgi-node.org/ .到目前为止,我无法找到 Apache 的 mod-node,但请查看:CGI-Node for Apache http://www.cgi-node.org/

The interesting part about cgi-node is that it uses JavaScript exactly like you would use PHP to generate dynamic content, service up static pages, access SQL database etc. You can even share core JavaScript libraries between the server and the client/browser. cgi-node的有趣之处在于它使用 JavaScript 就像您使用 PHP 生成动态内容、服务静态页面、访问 SQL 数据库等一样。您甚至可以在服务器和客户端/浏览器之间共享核心 JavaScript 库。

I think the shift to a single language between client and server is happening and JavaScript seems to be a good candidate.我认为客户端和服务器之间正在转向单一语言,JavaScript 似乎是一个很好的候选者。

A quick example from cgi-node.org site:来自 cgi-node.org 站点的一个快速示例:

<? include('myJavaScriptFile.js'); ?>
<html>
   <body>
      <? var helloWorld = 'Hello World!'; ?>
      <b><?= helloWorld ?><br/>
      <? for( var index = 0; index < 10; index++) write(index + ' '); ?>
   </body>
</html>

This outputs:这输出:

Hello World!
0 1 2 3 4 5 6 7 8 9

You also have full access to the HTTP request.您还可以完全访问 HTTP 请求。 That includes forms, uploaded files, headers etc.这包括表单、上传的文件、标题等。

I am currently running Node.js through the cgi-node module on Godaddy.我目前正在通过 Godaddy 上的 cgi-node 模块运行 Node.js。

CGI-Node.org site has all the documentation to get started. CGI-Node.org 站点包含所有入门文档。

I know I'm raving about this but it is finally a relief to use something other than PHP.我知道我对此赞不绝口,但使用 PHP 以外的东西终于让我松了口气。 Also, to be able to code JavaScript on both client and server.此外,为了能够在客户端和服务器上编写 JavaScript。

Hope this helps.希望这可以帮助。

The common method for doing what you're looking to do is to run them side by side, and either proxy requests from apache to node.js based on domain / url, or simply have your node.js content be pulled from the node.js port.做你想做的事情的常用方法是并排运行它们,或者根据域/url代理从 apache 到 node.js 的请求,或者简单地从节点中提取你的 node.js 内容。 js端口。 This later method works very well for having things like socket.io powered widgets on your site and such.后面的方法非常适合在您的网站上使用 socket.io 驱动的小部件之类的东西。


If you're going to be doing all of your dynamic content generation in node however, you might as well just use node.js as your primary webserver too, it does a very good job at serving both static and dynamic http requests.但是,如果您打算在 node 中生成所有动态内容,那么您也可以使用 node.js 作为您的主要网络服务器,它在处理静态和动态 http 请求方面做得非常好。

See:看:

http://expressjs.com/ http://expressjs.com/

https://github.com/joyent/node/wiki/modules https://github.com/joyent/node/wiki/modules

You can always do something shell-scripty like:你总是可以做一些 shell-scripty 的事情,比如:

#!/usr/bin/node

var header = "Content-type: text/plain\n";
var hi = "Hello World from nodetest!";
console.log(header);
console.log(hi);

exit;

While doing my own server side JS experimentation I ended up using teajs .在做我自己的服务器端 JS 实验时,我最终使用了teajs It conforms to common.js , is based on V8 AND is the only project that I know of that provides 'mod_teajs' apache server module.它符合common.js ,基于 V8 并且是我所知道的唯一提供 'mod_teajs' apache 服务器模块的项目。

In my opinion Node.js server is not production ready and lacks too many features - Apache is battle tested and the right way to do SSJS.在我看来,Node.js 服务器还没有做好生产准备并且缺乏太多功能——Apache 已经过实战测试,并且是执行 SSJS 的正确方法。

If you're using PHP you can funnel your request to Node scripts via shell_exec, passing arguments to scripts as JSON strings in the command line.如果您使用 PHP,您可以通过 shell_exec 将您的请求传递给 Node 脚本,在命令行中将参数作为 JSON 字符串传递给脚本。 Example call:示例调用:

<?php
    shell_exec("node nodeScript.js"); // without arguments
    shell_exec("node nodeScript.js '{[your JSON here]}'"); //with arguments
?>

The caveat is you need to be very careful about handling user data when it goes anywhere near a command line.需要注意的是,当用户数据进入命令行附近时,您需要非常小心处理用户数据。 Example nightmare:噩梦示例:

<?php
    $evilUserData = "'; [malicious commands here];";
    shell_exec("node nodeScript.js '{$evilUserData}'");
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM