0

I come from the LAMP world so I am a bit confused on how to serve an ExpressJS application from my webserver.

I have multiple Apache virtualhosts that all point to various directories and specify various domain names. They all have configuration files in /etc/apache2/sites-available/ on my Ubuntu.

Anyway, I installed an ExpressJS instance using node, it sits in a directory in my /var/www/ directory, with my other web (Apache) stuff.

How do I serve it with a specific domain name without having to do IP:3000 (which doesn't even work because of my firewall). I see this: Apache HTTPD: URL resolution for a virtual host with proxies and directory alias, but I'm not sure that it is correct because I read elsewhere you want to avoid Apache hosts for Node as it disrupts its performance? Just looking for the best answer here.

It should use port 80 and be mapped to a specific domain, while minimizing performance impact. I'm also not sure what to point it to exactly in the node files, I'm guessing the /var/www/nodeapp/node_modules/express/index.js file?

Thanks for your gracious assistance!

Summer Developer
  • 160
  • 1
  • 13

1 Answers1

0

Thanks for the link Michael Hampton, here is a complete/edited answer for future reference:

Apache VirtualHost/Config File:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName example.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

    ProxyPreserveHost On
    ProxyPass "/" "http://localhost:3000"
    ProxyPassReverse "/"= "http://localhost:3000"
</VirtualHost>

Note: To get the above working, you need to install Apache's mod_proxy packages, see the Apache documentation, or do this:

a2enmod proxy
a2enmod proxy_http
a2enmod proxy_ajp
a2enmod rewrite
a2enmod deflate
a2enmod headers
a2enmod proxy_balancer
a2enmod proxy_connect
a2enmod proxy_html

3000 in the config file is because that is what my Node app is running on, see here:

/*!
 * express
 * Copyright(c) 2009-2013 TJ Holowaychuk
 * Copyright(c) 2013 Roman Shtylman
 * Copyright(c) 2014-2015 Douglas Christopher Wilson
 * MIT Licensed
 * index.js
 */

'use strict';

var express = require('./lib/express');
var http = require('http');

var app = express();
var server = http.createServer(app);

app.get('/', function(req, res) {
    res.send("Hello World!");
});

server.listen(3000, 'localhost');
server.on('listening', function() {
    console.log('Express server started on port %s at %s', server.address().port, server.address().address);
});

To run it, just do node index.js

To keep it running even after you exit that command, add an &, like so:

node index.js &

^ Note that only keeps it running while your Unix session is active, for example if you close your SSH client node would stop running even with the &.

Look here to get it to run permanently: https://stackoverflow.com/questions/12701259/how-to-make-a-node-js-application-run-permanently

Summer Developer
  • 160
  • 1
  • 13