Integrating Ruby on Rails with existing PHP or Perl Based Sites

So, you want to add some new features to your site using Ruby on Rails?  That's not a bad idea.  I've found Rails to be one of the quickest ways to develop small feature-rich sites quickly.  The problem is getting those new components integrated with the existing PHP, Perl, JSP, .NET or whatever other language codebase.  I've found that it's really not that hard so long as you know the right tricks.

The big difference between a Rails app and PHP and Perl is that Rails apps run on an application server.  An application server is always running and needs its own dedicated port.  PHP and Perl are more commonly run as CGI, which is hosted by the webserver which is usually Apache. 

It's not that difficult to add the rails app in with the existing site by using mod_proxy.  Mod_proxy is an apache module that intercepts HTTP calls on your regular web port (80), rewrites them and redirects to the port and URL of your choosing.  An example of this would be  That is actually the rails portion of the site running on a different port via apache's mod_proxy.  If I add another rails app to the site, I'll just use another port and another proxy mapping.

My mod_proxy settings look like this:

ProxyPass /garage http://localhost:3001/garage
ProxyPassReverse /garage http://localhost:3001/garage
ProxyPreserveHost On

What this does is maps hits to /garage and /garage/* to port 3001/garage on the localhost.  You could use this same technique to proxy the traffic to another application server, which is nice for scalability.  One important thing to note is that when you are developing your app, you will want to use that same base URL instead of just / because it won't be on / when deployed to your production server.

Also, if your site needs to handle more than just a small amount of traffic, you will need to run multiple mongrel servers to handle the load.  These can be on the same machine, just like apache runs many child processes, and fortunately there is a gem called mongrel_clusters which will handle this for you.  Here is a tutorial on using mongrel_clusters.

Often times for integration pieces you will need to piggyback on one database while maintaining data in another.  This is a very common integration problem and there are a few different ways to accomplish it with Rails.  I had a relatively small number of tables I needed to work with from the integration database so I followed this method for mapping model classes to different databases.

After that was done I still had one more problem to solve.  I needed a way to include the output of just one rails URL in my PHP app.  The easiest way I found was through using a client-side AJAX call, just like Google's Adsense does.  It may not be ideal for many situations, but for the ease of implementation, I had to try it and I think it worked out well.  Here is a method for doing client-side ajax includes and if you look at the "random car from the garage" box up toward the top, you will see how I was able to include the Rails call into a vBulletin forum which is PHP.

To finish it all off, I simply put the different site components into navigation links in the header, copied much of the style between the 3 different pieces of software and I had a fully integrated custom feature written in Ruby on Rails for my site.

Here is the end result: 

Main Site (PHP) - (the garage box on top is an integrated component)
Rails app (RoR) -
Site Blog (Perl) -

And with a little elbow grease, you have one site written in 3 languages.

Update - 8/6/2008:
I installed Phusion Passenger and redeployed my apps with it.  I highly recommend using this method as it's very easy to build and install and deployment couldn't be more simple.


Post a comment here or discuss this and other topics in the forums

vBulletin /Rails integration

Hey Robert,
How are you dealing with Rails/vB integration on your site? Are you sharing sessions/sso?
Would appreciate some tips if you get the time!

re: Integrating Ruby on Rails with existing PHP or Perl Based S

Karl,You are totally correct. I am planning on switching my sites to use passenger in the future.Thanks

re: Integrating Ruby on Rails with existing PHP or Perl Based S

There is no need to mess with mongrel clusters anymore. ModRails ( aka Phusion Passenger makes deploying Rails apps just as simple as PHP.