Future Nate Note:

This is a terrible hack. You shouldn’t do this. You should use the apache passenger module mod_passenger to run passenger behind apache.

This post rescribes how to proxy all traffic for a vhost to an internal port running a passenger standalone server. This is a bad way to do things. Follow this guide instead.

Original Post

I suspect many people don’t have this problem but I ran into it so I thought I’d document it. In order to minimize my AWS bill I wanted to run a website built in rails (this one!) on the same AWS instance that was already running apache. So if you need to do the same here’s what you do:

Run your rails app on a port other than port 80, anything works even if it’s not exposed to the public. The traffic will flow like this:

User -> Port (80) -> Apache -> Port (xyz) --> Rails

First you’ll need to install the necessary apache modules

a2enmod proxy proxy_ajp proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_html

Then, add a virtual host in your /etc/apache2/sites-enabled/something-something.conf file or wherever you specify your virtual hosts. It should look like this:

<VirtualHost *:80> 
    Host On 
    Redirect permanent / https://www.n8ta.com/ # Forward to https 
    ServerName site.com 
    ServerAlias www.site.com 
</VirtualHost> # and a second for ssl, 
<VirtualHost *:443> 
     ProxyPreserveHost On 
     ProxyPass / # apache --> rails connection is not secured as it's local to the machine 
     ProxyPassReverse / 
     ServerName site.com 
     ServerAlias www.site.com 
     SSLEngine on 
     SSLCertificateFile /path/to/a.crt 
     SSLCertificateKeyFile /path/to/a.key 

Restart apache with

service apache2 restart

or whatever tool you use to manage services.

And you’re golden, you now have a rails app and an apache server running simultaneously.