Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Node.js in apache?

We have an Apache Webserver installed on a machine which also serves pages using Perl.

For a project I've decided to use Node.js instead of 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)?

So in other words can they work hand in hand just like Apache/Perl or Apache/PHP etc..

like image 208
JackSparrow123 Avatar asked Jan 16 '13 23:01

JackSparrow123


People also ask

Can we run nodejs in Apache?

Hosting a nodejs site through apache can be organized with apache proxy module. Do not enable proxying with ProxyRequests until you have secured your server. Open proxy servers are dangerous both to your network and to the Internet at large. Setting ProxyRequests to Off does not disable use of the ProxyPass directive.

Can Apache run JavaScript?

While Javascript is a client-side executing coding language, it still has to have permission from the Apache Web server in order to run properly. If Apache is configured incorrectly, the Javascript on your Web server may not work properly.

Can I run Node js on Tomcat?

You can run Nodejs such as react or angular on JavaPipe's Tomcat service.


3 Answers

Hosting a nodejs site through apache can be organized with apache proxy module.

It's better to start nodejs server on localhost with default port 1337

Enable proxy with a command:

sudo a2enmod proxy proxy_http 

Do not enable proxying with ProxyRequests until you have secured your server. Open proxy servers are dangerous both to your network and to the Internet at large. Setting ProxyRequests to Off does not disable use of the ProxyPass directive.

Configure /etc/apche2/sites-availables with

<VirtualHost *:80>     ServerAdmin [email protected]     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.

like image 163
Evgeny Karpov Avatar answered Sep 29 '22 07:09

Evgeny Karpov


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. 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.

As a replacement for Apache, node offers performance advantages if you have many simultaneous connections. 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.

Node isn't the best way to serve static content. Nginx and Varnish are more effective at that. They can serve static content while Node handles the dynamic data.

If you're considering using node for a web application at all, Express should be high on your list. 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. 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. The github repository for ExpressJS also contains many good examples for everything from authentication to organizing your app.

like image 41
Darren Avatar answered Sep 29 '22 06:09

Darren


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..

YES, you can run Node.js on Apache along side Perl and PHP IF you run it as a CGI module. 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/ .

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.

I think the shift to a single language between client and server is happening and JavaScript seems to be a good candidate.

A quick example from cgi-node.org site:

<? 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. That includes forms, uploaded files, headers etc.

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

CGI-Node.org site has all the documentation to get started.

I know I'm raving about this but it is finally a relief to use something other than PHP. Also, to be able to code JavaScript on both client and server.

Hope this helps.

like image 36
Uei Richo Avatar answered Sep 29 '22 06:09

Uei Richo