Javascript – Running Node.js in apache

apachejavascriptnode.jswebserver

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

Best Answer

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