Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stagas/754303 to your computer and use it in GitHub Desktop.
Save stagas/754303 to your computer and use it in GitHub Desktop.
How to run Apache and Node.js together (the right way)

Step 1

Get a VPS that offers 2 or more IP addresses.

Step 2

From the WHM cPanel, find the menu item Service Configuration, select Apache Configuration and then click on Reserved IPs Editor.

Step 3

Tick the IP address you DON'T WANT Apache to listen to, and write it down so you can use it in the next step. Click Save.

Step 4

Install Node.js, and create a server like this:

var http = require('http');

var server = http.createServer(function(req, res) {
  res.writeHead(200);
  res.end('Hello, world!');
});

server.listen(80, '111.111.111.111');

Replacing 111.111.111.111 with the IP address you previously reserved from the WHM cPanel.

Step 5

Stop wasting your time and never listen to those telling you to use mod_rewrite to proxy Node.js again.

@mojaray2k
Copy link

Hey I made a Youtube Video that implements your gist. https://www.youtube.com/watch?v=3U1AkjlWymI&feature=youtu.be

@wathmal
Copy link

wathmal commented Jun 14, 2016

following article also describes a way to do this without mod_rewrite on same VPS.
https://wathmal.me/blog/run-apache-with-node-js-reverse-proxy/

@yenleidong
Copy link

yenleidong commented Dec 23, 2016

solve my problem, and @mojaray2k video implement was really clear.
we dont need to rebuild or install another mod on WHM and mod_rewrite concept, I can't take the risk if my Cpanel going error.

FYI if you want to express require module together with http module, simply using this code
/*****************************/
var app = express();
var http = require('http');
/* pur some apt.set here /
/
pur some apt.use or express static configurations here /
/
pur some apt.get here */
app.server = http.createServer(app);
app.server.listen(80, '111.111.111.111'); // change 111.111.111.111 with your reserve IP

@lancasterjones
Copy link

If you have apache you can add this in your Apache config file:
ProxyPass /node http://localhost:8000/
Asuming 8000 is the port where node is listening.

@AbdelhamidAhmed
Copy link

Thank you so much for this, I've been trying to get this to work for a long while now especially after buying a very expensive dedicated server and found myself not able to run node.js without a port because of apache.

@fdorantesm
Copy link

The disadvantage to use proxyPass is that you can't use host property to do something related with subdomains, base uri namespace, etc. This will be localhost:{port}

@saman2ny
Copy link

this wont works for https 443 port also gets blocked

@akilawickey
Copy link

akilawickey commented Jun 5, 2018

Thank you @lancasterjones your way worked for me :-).
This can be added to apache /etc/apache2/sites-enabled/000-default.conf before the ending.

Eg

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/iot-platform-frontend

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
ProxyRequests on
ProxyPass /node http://localhost:8080
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

@infinitbility
Copy link

Created tutorial on How to deploy nodejs application subdirectory level in apache ubuntu

https://infinitbility.com/how-to-deploy-nodejs-application-subdirectory-level-in-apache-ubuntu

@amarjit-singh
Copy link

amarjit-singh commented Jul 18, 2021

what if you don't have 2 ip addresses?

Then follow this article
https://www.devopinion.com/run-apache-and-nodejs-on-the-same-port/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment