Using PHP inside Rails structure on Apache
I do alot of development in Rails recently, and it all goes fine and nice, and Rails is fast and easy to use, but sometimes I'm better off using already existing PHP scripts. Just place them into Rails project, and they just work (think of PHP-based web forms etc).
How do you setup your Apache & Mongrel cluster based Rails project to support execution of PHP scripts inside the Rails project structure? Very easy, actually.
If you use setup like mine, you have something like the following in the Apache httpd.conf file:
##########################################################################
## myserver.com
##########################################################################
<VirtualHost *>
ServerName myserver.com
ServerAlias www.myserver.comDocumentRoot /var/www/myserver
<Proxy balancer://mongrel_cluster>
BalancerMember http://127.0.0.1:8200
BalancerMember http://127.0.0.1:8201
</Proxy><Directory "/var/www/myserver">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>RewriteEngine On
RewriteRule ^/$ /index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
</VirtualHost>
This is just a basic Apache-based Mongrel cluster driven setup.
Now, say, you decide to put a PHP-based mailform inside your Rails project, accessible by myserver.com/mailform/mailform.php path.
Here's what you need to do. All modifications are performed in httpd.conf file (or a different place in case you don't have virtual server configuration defined not in default place).
First, make sure you change the document root to the full path to your Rails project public directory:
DocumentRoot /var/www/myserver/current/public
Second, add the following right after RewriteEngine on directive :
RewriteCond %{REQUEST_URI} ^/mailform.*$
RewriteRule ^(.*)$ $1 [L]
By doing this, you basically instruct Apache to not pass requests to urls starting with /mailform , to Rails. So requests will be processed by default server rules, not by Rails.
Of course you need to have PHP support available to Apache for PHP scripts to work :) And just in case php was compiled in after rails stuff was setup, you might need to add the following to the bottom of httpd.conf:
AddType application/x-httpd-php .php
That's it :)
August 16th, 2007 at 2:13 am
Hi,
I'm wondering if it is possible to do post-erb php processing. What I mean by that is that after Apache gets a page from Mongrel, that has been processed by rails, Apache sends it through PHP processing.
For instance, if a view in rails had:
<code>
</code>
then Mongrel would return:
hi
And then Apache would just handle the returned page as a normal PHP file.
The reason that I ask is because we have a PHP-based site that we're trying to convert into rails, and many scripts rely on PHP includes that want to push off for the future.
August 16th, 2007 at 2:53 pm
Hi David,
looks like wordpress have chopped some of your comment's tags..
You mean the possibility to forward Rails-processed stuff further to PHP processor? Since the processing goes like: Apache -> Mongrel -> RoR (Ruby), you'll actually have to call the PHP processor not from Apache, but rather from your Rails code (that is, if I understand how stuff works, right)
I guess it is possible with some effort put into it (you'll have to pass all the params and there might possible be problems with multipart forms or stuff like that).. but I think making modifications to the normal Rails processing flow just to be able to process PHP scripts is a little overkill. I'm not sure I would wanted to install such a plugin, even if it existed. Putting PHP-only stuff into separate directory is the cleanest solution in my opinion. Rails and PHP don't really mix very good :)
January 15th, 2008 at 10:28 am
A two piece solution is what I found…
1) in apache.conf
# send all /forum traffic to php
RewriteCond %{REQUEST_URI} ^/forum.*
RewriteRule .* - [L]
# send all /wiki traffic to php
RewriteCond %{REQUEST_URI} ^/wiki.*
RewriteRule .* - [L]
2) in your PHP app - edit .htaccess
RewriteEngine Off
posted more fully…
http://weblog.terrellrussell.com/2008/01/running-php-within-rails/
October 13th, 2010 at 2:56 pm
There has got to be a way to use a two layer approach, having the rails output .php and then apache pushing the php.