Running Node.js in apache?

We have an Apache web server installed on a computer that also serves pages using Perl.

For the project, I decided to use Node.js instead of Perl / Ruby. Just wondering, can I use Apache as my web server (to serve the pages) and use Node.js to dynamically create web pages (this is for the web application being created)?

In other words, they can work hand in hand, like Apache / Perl or Apache / PHP, etc.

+54
javascript apache webserver
Jan 16 '13 at 23:35
source share
5 answers

No. NodeJS is not available as an Apache module in the mod-perl and mod-php methods, so it is impossible to run node "on top of Apache. As the hexist pointed out, you can run node as a separate process and organize the connection between them, but this is not at all the way you already use on the LAMP stack.

As a replacement, Apache node offers performance benefits if you have many concurrent connections. There is also a huge ecosystem of modules for everything you can think of.

From your question it is not clear whether you need to dynamically generate pages for each request or periodically generate new content for caching and maintenance. If this is the last, you can use a separate node task to generate content in a directory that Apache will serve, but again, which is completely different from PHP or Perl.

Node is not the best way to serve static content. Nginx and Varnish are more efficient at this. They can serve static content, and node processes dynamic data.

If you plan to use node for a web application in general, Express should be high on your list. You can implement the web application exclusively in Node, but Express (and similar frameworks like Flatiron, Derby and Meteor) are designed to take a lot of pain and boredom. Although the Express documentation may seem a bit sparse at first, take a look at the screen throws that are still available here: http://expressjs.com/2x/screencasts.html They will give you a good idea of ​​what express offers and why is it useful. The github repository for ExpressJS also contains many good examples for everything from authentication to organizing your application.

+41
Jan 18 '13 at 4:43
source share

Hosting a nodejs host through apache can be organized using the apache proxy module.

Better run nodejs server on localhost with default port 1337

Enable proxy using the command:

sudo a2enmod proxy proxy_http 

Do not allow proxying with ProxyRequests until you secure your server. Open proxies are dangerous for both your network and the Internet as a whole. Setting ProxyRequests to Off does not disable the use of the ProxyPass directive.

Configure / etc / apche2 / sites-availables with

 <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 the apache2 service.

+39
May 28 '14 at 11:08
source share

Although there are many good tips here, I would like to answer your question:

In other words, they can work hand in hand, like Apache / Perl or Apache / PHP, etc.

YES , you can run Node.js on Apache on the side of Perl and PHP IF , you run it as a CGI module. At the moment I can’t find the mod-node for Apache, but check out: CGI-node for Apache here http://www.cgi-node.org/ .

The interesting part of cgi-node is that it uses JavaScript just like you would use PHP to create dynamic content, serve static pages, access to an SQL database, etc. You can even share core JavaScript libraries between the server and the client / browser.

I think the switch to the same language between the client and server is happening, and JavaScript seems to be a good candidate.

A quick example from 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> 

It is output:

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

You also have full access to the HTTP request. This includes forms, uploaded files, headers, etc.

I am currently running Node.js through the cgi- node module on Godaddy.

CGI- node.org has all the documentation to get you started.

I know that I raved about it, but finally made it easier to use something other than PHP. In addition, to be able to encode JavaScript on both the client and the server.

Hope this helps.

+17
Oct 08 '14 at 16:06
source share

The general way to carry out your actions is to run them next to each other and proxy requests from apache to node.js based on domain / url or just have node.js content you can pull from the node.js port This later method works very well to make socket widgets visible on your site. IIO.




If you do all your dynamic content creation in node, you can just use node.js as the main web server, it does a great job serving both static and dynamic HTTP requests.

See:

http://expressjs.com/

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

+5
Jan 16 '13 at 23:48
source share

When doing my own server side JS experiment, I ended up using teages . It corresponds to common.js , is based on V8 and is the only project that, as I know, provides the mod_teajs apache server module.

In my opinion, the Node.js server is not ready for production and does not have too many functions - Apache is tested for battle and the correct SSJS method.

0
Dec 15 '13 at 20:49
source share



All Articles