Last Updated: February 25, 2016
·
800
· maumagau

Run node and apache sites from one ip

So I'm moving my blog over to Ghost, which runs on node. I also host a load of dev sites on the server, which run off plain old php and apache. The trouble is, how do we route port 80 for our server's single IP address to two separate servers?

First off, I'm assuming you have node.js and Apache set up on a linux server (debian-based). You'll also probably have something like Forever to keep your node server running erm, forever.

Secondly, I'll assume you want to access your sites via different domain names (or sub-domains), which are all set up and pointing at your servers IP.

Thirdly, you've already got your regular Apache sites set up using vhosts. For simplicity's sake, let's say you have one regular site called example.com already running, and you want a new site - blog.com - to show off your blog.

We'll use Apache's proxy module to tell Apache to route any traffic to blog.com to our node server.
n.b. There's a pretty strong warning in the Apache docs about using proxy_mod. If you're running a valuable production server, make sure you know what this implies.

1 - Enable proxymod and proxymod_http:

$ sudo a2enmod proxy
$ sudo a2enmod proxy_http

2 - Create a virtual host for blog.com and copy the following, replacing 2368 with whatever port you've configured your node site to listen on (2368 is Ghost's default)

$ sudo vim /etc/apache2/sites-available/blog
<VirtualHost *:80>
    ProxyRequests off

    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>

    <Location />
            ProxyPass http://localhost:2368/
            ProxyPassReverse http://localhost:2368/
    </Location>

</VirtualHost>

3 - Enable the site and reload Apache

$ sudo a2ensite blog
$ sudo service apache2 reload

4 - That's it. You should now be able to view both your Apache site at example.com, and your node site at blog.com.