Skip to content

Running PHP within Rails

So this has been something I’ve been putting off because it wasn’t readily apparent how to make it happen when I first tried over a year and a half ago.

I wanted to manage a group of users and passwords for a community with an existing Ruby on Rails application – and then have both a PHP-based PunBB forum and PHP-based wiki running inside the Rails app. Of course, more interestingly, I wanted to make the sessions and cookies all align and share a single sign-on.

And this past week, I poked enough at the right pieces to make it happen.

I first found this post at macdiggs.com but found the solution to lack directory mapping in addition to allowing the PHP to execute inside the Rails /public/ directory. I wanted http://example.com/forum to render and not throw a Rails error. As it stood, only http://example.com/forum/index.php and URLs like it would come up correctly.

Then, I started seeing references to turning off Apache’s RewriteEngine…

The solution I’ve now got in place has two parts:

1) Edit the Apache configuration file for your virtualhost. This tells Apache not to push any requests that start with /forum or /wiki to the Rails app (here, a mongrel cluster) – and to just handle them by itself (via the PHP processor).

RewriteEngine On
# send all /forum traffic to php
RewriteCond %{REQUEST_URI} ^/forum.*
RewriteRule .* - [L]
# send all /wiki traffic to php
RewriteCond %{REQUEST_URI} ^/wiki.*
RewriteRule .* - [L]
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
<Proxy balancer://mongrel_cluster>
BalancerMember http://127.0.0.1:6000
BalancerMember http://127.0.0.1:6001
</Proxy>

2) Add/Edit the .htaccess file in both the forum and the wiki directories. This tells any requests that were sent the way of either the forum or the wiki to halt the rewriting engine and just process the requests themselves.

RewriteEngine Off

Now, both http://example.com/forum and http://example.com/wiki render correctly.

As for getting the cookie to sync – please see my next post…

P.S. And now for all the phrases that I couldn’t find anywhere when I was looking for how to make all this magic happen – maybe someone else will find this stuff here: PHP within Rails, PHP inside Rails, PunBB in Rails app, Ruby on Rails, DokuWiki, PHP/Rails integration, PHP integration with Rails

Tags: - - - - - - -

View blog reactions

{ 2 } Trackbacks

  1. […] This Old Network Ideas on interconnections, identity, and information from all sides. « Running PHP within Rails […]

  2. […] close out a week of very geeky Rails and PHP […]