<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>matthewcarriere.com &#187; rails</title>
	<atom:link href="http://matthewcarriere.com/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewcarriere.com</link>
	<description>a blog about ruby on rails and the mac.</description>
	<lastBuildDate>Mon, 05 Jul 2010 21:29:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>using text_field_with_auto_complete and form_authenticity_token</title>
		<link>http://matthewcarriere.com/2008/08/13/using-text_field_with_auto_complete-and-form_authenticity_token/</link>
		<comments>http://matthewcarriere.com/2008/08/13/using-text_field_with_auto_complete-and-form_authenticity_token/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 06:14:39 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=31</guid>
		<description><![CDATA[Rails 2.0 introduced protection for Cross-site request forgery (CSRF). This is turned on by default in your Rails applications. However, many developers are turning off this valuable protection rather than making the changes necessary to use it. This seems the same to me as leaving your door unlocked while your friend visits because cutting keys [...]]]></description>
			<content:encoded><![CDATA[<p>Rails 2.0 introduced protection for <a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">Cross-site request forgery (CSRF)</a>. This is turned on by default in your Rails applications. However, many developers are turning off this valuable protection rather than making the changes necessary to use it. This seems the same to me as leaving your door unlocked while your friend visits because cutting keys is hard. Case in point: text_field_with_auto_complete</p>
<p>You know you have run into the new CSRF protection when you see this error in your Rails application:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActionController::InvalidAuthenticityToken</span></pre></div></div>

<p>This is the point where you Google the error and receive one of the following pieces of advice:</p>
<h4>1. Turn off RequestForgeryProtection all together</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># add the following line to your application.rb or one of your</span>
<span style="color:#008000; font-style:italic;"># environment.rb files. why not production? (sarcasm)</span>
config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#9900CC;">allow_forgery_protection</span> = <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

<h4>2. Turn off RequestForgeryProtection for just one controller</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">skip_before_filter <span style="color:#ff3333; font-weight:bold;">:verify_authenticity_token</span></pre></div></div>

<h4>3. Turn off RequestForgeryProtection for just one action.</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">protect_from_forgery <span style="color:#ff3333; font-weight:bold;">:except</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:attack_me</span></pre></div></div>

<p>So let&#8217;s see how easy it is to utilize this new feature!</p>
<p>I was working with the text_field_with_auto_complete in a pretty basic form:</p>
<p>For those of you who aren&#8217;t familiar with this there is a nice post on the <a href="http://wiki.rubyonrails.com/rails/pages/How+to+use+text_field_with_auto_complete">Rails Wiki</a></p>
<p>When I tried to perform my auto complete I received the <em>ActionController::InvalidAuthenticityToken</em> error.</p>
<p>Why? Because basically my Ajax request did not include a security token that RequestForgeryProtection uses to validate my request is coming from the right place. All you need to do is add this token to the parameters sent with your request. Rails makes this easy with the helper: form_authenticity_token. The token name RequestForgeryProtection will be looking for is: authenticity_token.</p>
<p>So let&#8217;s add this to our text_field_with_auto_complete:</p>
<p>Now when I performed my auto complete it worked flawlessly! Now to refactor that string&#8230; any suggestions?</p>
<h4>Update August 25, 2008</h4>
<p>Learned two things working with this&#8230;</p>
<p>1. It&#8217;s automatically doing a POST, so by changing the method to GET I can avoid having to deal with the authenticity_token.</p>
<p>2. And with that out of the way I don&#8217;t need to specify the parameters since by default it will send back the value of the text field.</p>
<p>That&#8217;s a lot cleaner!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/08/13/using-text_field_with_auto_complete-and-form_authenticity_token/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning Ruby on Rails with Heroku Episode 1</title>
		<link>http://matthewcarriere.com/2008/06/30/learning-ruby-on-rails-with-heroku-episode-1/</link>
		<comments>http://matthewcarriere.com/2008/06/30/learning-ruby-on-rails-with-heroku-episode-1/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 06:10:32 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[heroku]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[screencast]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=28</guid>
		<description><![CDATA[Ever since I saw Heroku I wanted to use it for a screencast on learning Ruby on Rails. This weekend I finished the very first episode! This episode is fairly light on Ruby on Rails content because I was so focused on screencasting and learning all about video and audio production for the web. So [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I saw <a href="http://heroku.com">Heroku</a> I wanted to use it for a screencast on learning Ruby on Rails. This weekend I finished the very first episode! This episode is fairly light on Ruby on Rails content because I was so focused on screencasting and learning all about video and audio production for the web.</p>
<p>So take a look and tell me what you think. I used <a href="http://vimeo.com">Vimeo</a> for hosting the video. I also produced the video in HD&#8230; however I didn&#8217;t realize that Vimeo only streams the HD content on their site, not the embedded video. So to view the best possible video you should view it on Vimeo. <a href="http://www.vimeo.com/1256684">here is the link</a>. You can make it full screen and enjoy the HD in all its glory.</p>
<p>Please feel free to leave some <strong>constructive</strong> criticism regarding the screencast. This way I can make some changes before I start pushing this out more regularly.</p>
<p>In this first episode of Learning Ruby on Rails with Heroku:</p>
<ol>
<li>Introduction</li>
<li>Goal of this screencast series</li>
<li>What is Heroku?</li>
<li>Getting an account</li>
<li>Creating your first Ruby on Rails application</li>
<li>Basic navigation of Heroku</li>
</ol>
<p><object width="640" height="360"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=1256684&amp;server=www.vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://www.vimeo.com/moogaloop.swf?clip_id=1256684&amp;server=www.vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="640" height="360"></embed></object><br /><a href="http://www.vimeo.com/1256684?pg=embed&#038;sec=1256684">Learning Ruby on Rails with Heroku Episode 1</a> from <a href="http://www.vimeo.com/carriere?pg=embed&#038;sec=1256684">Matthew Carriere</a> on <a href="http://vimeo.com?pg=embed&#038;sec=1256684">Vimeo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/06/30/learning-ruby-on-rails-with-heroku-episode-1/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Take it to the next level with the Ruby on Rails console</title>
		<link>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/</link>
		<comments>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/#comments</comments>
		<pubDate>Mon, 28 Jan 2008 05:53:19 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=14</guid>
		<description><![CDATA[If you are not using the Rails console while your developing then now is the time to start! I learn best by example, so that&#8217;s how I am going to structure this. Here&#8217;s the scenario: You are writing code against a class and need to determine which attributes you have available to you. In Rails, [...]]]></description>
			<content:encoded><![CDATA[<p>If you are not using the Rails console while your developing then now is the time to start! I learn best by example, so that&#8217;s how I am going to structure this.</p>
<p>Here&#8217;s the scenario:</p>
<p>You are writing code against a class and need to determine which attributes you have available to you. In Rails, your attributes are largely driven by the database schema, so you will not see them in the class definition. At this point most developers will take a look at the table in the database and find the column they need, then determine the type of data stored there. There is a better way&#8230;</p>
<p>Let&#8217;s use the command line.</p>
<p>We start by firing up our Rails application in console mode:<br />
<em>You must be in your Rails application directory to issue this command.</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">script<span style="color: #66cc66;">/</span>console</pre></td></tr></table></div>

<p>So now what? Well now you have command line access to your entire Rails application! For example, let&#8217;s say you have a class named Person (which would map to a people table in the database). You want a new instance of this class? Then issue the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">new</span></pre></td></tr></table></div>

<p>Perhaps you want to view a Person from the database:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">p</span> = Person.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>By default the results are displayed below each command and they are not very friendly, so let&#8217;s output them in YAML as it will be much more readable:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span></pre></td></tr></table></div>

<p>&#8216;y&#8217; is like calling &#8216;puts p.to_yaml&#8217; on the resulting value that follows it. There is also a &#8216;p&#8217; shortcut that is the equivalent of issuing &#8216;puts&#8217; on the value.</p>
<p>So how does this help with determining attributes? At first glance you may think you have to look at the list of values returned in your YAML command and find what your looking for&#8230; no&#8230; it gets better. Remember that all Ruby objects inherit certain methods, watch this:</p>
<p>You want to get the methods associated with your object:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">methods</span></pre></td></tr></table></div>

<p>You can also get the private, public, and protected methods using the following commands:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">public_methods</span>
y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">private_methods</span>
y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">protected_methods</span></pre></td></tr></table></div>

<p>So this class could return a huge YAML result which may not seem as efficient as scanning the schema in the database, and you&#8217;re right. So let&#8217;s get to what we are really looking for using &#8216;grep&#8217;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">methods</span>.<span style="color:#9900CC;">grep</span> <span style="color:#006600; font-weight:bold;">/</span>method_name<span style="color:#006600; font-weight:bold;">/</span></pre></td></tr></table></div>

<p>This will return any method which contains <em>method_name</em>. You can write pass any regular expression you like into this command and really drill into your class methods, and for help putting those regular expressions together I really like using <a href="http://www.rubular.com/">Rubular</a> a web-based Ruby regular expression editor.</p>
<p>And what about the type of an object?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">y <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9966CC; font-weight:bold;">class</span></pre></td></tr></table></div>

<p>This is just a taste of what you can do with the console&#8230; I use the console everyday as I develop. I will post some more console tips in future posts!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/01/27/take-it-to-the-next-level-with-the-ruby-on-rails-console/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running Ruby on Rails with Mongrel Clustering and Apache Proxy</title>
		<link>http://matthewcarriere.com/2008/01/18/running-ruby-on-rails-with-mongrel-clustering-and-apache-proxy/</link>
		<comments>http://matthewcarriere.com/2008/01/18/running-ruby-on-rails-with-mongrel-clustering-and-apache-proxy/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 05:42:52 +0000</pubDate>
		<dc:creator>matthew</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mongrel]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubygems]]></category>

		<guid isPermaLink="false">http://matthewcarriere.com/?p=6</guid>
		<description><![CDATA[Being a Rails coder is easy, deploying a Rails application can be hard. Through a great deal of effort, we&#8217;ll call it blood, sweat and tears, I have put together the following guidance on how to get your Ruby on Rails application up and running behind a Mongrel cluster using Apache Proxy. Here&#8217;s the deal, [...]]]></description>
			<content:encoded><![CDATA[<p>Being a Rails coder is easy, deploying a Rails application can be hard. Through a great deal of effort, we&#8217;ll call it blood, sweat and tears, I have put together the following guidance on how to get your Ruby on Rails application up and running behind a Mongrel cluster using Apache Proxy.</p>
<p>Here&#8217;s the deal, I am going to install all the necessary applications to get this up and running, so if you have some of these installed on your server already you may just skip ahead, however, if you have any issues with this you may want to read over those sections you skipped and understand why I installed a specific version or what pitfalls I encountered that may be affecting your setup. Also, I am running Ubuntu, so if you are running a different distribution, then the paths to specific configuration files on your system may be different. Let&#8217;s get started!</p>
<h4>Installing MySQL</h4>
<p>Install MySQL using the package manager in Ubuntu:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo apt-get install mysql-<span style="color: #000066;">server</span></pre></td></tr></table></div>

<p>After the installation you should be able to run the command <em>mysql</em> or <em>mysqladmin</em>. If you cannot run these then you may have to create symbolic links to these commands or add them to your path.</p>
<p>By default, MySQL only listens on localhost, in order to change that you need to comment out the line which reads: bind-address = 127.0.0.1</p>
<p>Change this by editing the following file on your system:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo vi <span style="color: #66cc66;">/</span>etc<span style="color: #66cc66;">/</span>mysql<span style="color: #66cc66;">/</span>my.cnf</pre></td></tr></table></div>

<p>You can search for text in <em>vi</em> using a forward slash.</p>
<p>You should restart MySQL after making a change to the config file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;"><span style="color: #66cc66;">/</span>etc<span style="color: #66cc66;">/</span>init.d<span style="color: #66cc66;">/</span>mysql restart</pre></td></tr></table></div>

<p>Now verify that MySQL is listening to the network. The output of the following command should show an entry for MySQL:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">netstat -tap</pre></td></tr></table></div>

<p>You need to set a password for the root account of MySQL, otherwise anyone can access it!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">mysqladmin -u root password your_password
mysqladmin -h server_name -u root password your_password</pre></td></tr></table></div>

<p>In case it is not clear, <em>your_password</em> should be replaced with a strong password of your choosing.</p>
<h4>Installing Ruby</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">apt-get install ruby ri irb</pre></td></tr></table></div>

<h4>Install Ruby Gems</h4>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp
wget http:<span style="color: #808080; font-style: italic;">//rubyforge.org/frs/download.php/17190/rubygems-0.9.2.tgz</span>
tar -zxvf rubygems-0.9.2.tgz
cd <span style="color: #66cc66;">/</span>rubygems-0.9.2.tgz
ruby setup.rb</pre></td></tr></table></div>

<p>Update rubygems</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">gem update --<span style="color: #000000; font-weight: bold;">system</span>
gem update
gem cleanup</pre></td></tr></table></div>

<p>Why the two calls to gem update? &#8211;system will update the RubyGems itself and without that parameter you will only be updating the gems.</p>
<h4>Install Rails</h4>
<p>Install Rails using Rubygems:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">gem install rails</pre></td></tr></table></div>

<p>Now let&#8217;s test the rails installation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">rails <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
.<span style="color: #66cc66;">/</span>script<span style="color: #66cc66;">/</span><span style="color: #000066;">server</span></pre></td></tr></table></div>

<p>This will start the default WeBrick web server, so open a web browser to http://localhost:3000 If all is well you will see the Ruby on Rails welcome screen. Once you are finished staring at your success, press CTRL + d to kill the web server.</p>
<h4>Install Mongrel</h4>
<p>Install the build tools:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">apt-get install build-essential ruby1.8-dev</pre></td></tr></table></div>

<p>Install Mongrel:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">gem install mongrel mongrel_cluster --include-dependencies</pre></td></tr></table></div>

<p>Now let&#8217;s test the Mongrel installation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
mongrel_rails start</pre></td></tr></table></div>

<p>This will start the Mongrel web server (now default), so open a web browser to http://localhost:3000 If all is well you will see the Ruby on Rails welcome screen&#8230; again. Now kill the web server.</p>
<h4>Configure Mongrel Rails cluster</h4>
<p>Add a user for our mongrel cluster:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">useradd mongrel</pre></td></tr></table></div>

<p>Add the group for our mongrel user:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">groupadd mongrel</pre></td></tr></table></div>

<p>Configure the directory security for your application:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">chown -R mongrel:mongrel <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest</pre></td></tr></table></div>

<p>Now we need to configure the Rails cluster for the test application. This command will write a YAML file to the config directory in your Rails application so you will only have to enter all these parameters once. After that, simply firing the command <em>mongrel_rails cluster::start</em> will use this configuration:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
mongrel_rails cluster::configure -e production \
-p <span style="color: #cc66cc;">8000</span> -N <span style="color: #cc66cc;">8</span> --<span style="color: #000066;">user</span> mongrel --group mongrel</pre></td></tr></table></div>

<p>You can reconfigure by running this command and using different parameters, you can also safely delete the YAML file from your config directory to blow these settings away.</p>
<p>Time to start the cluster:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">cd <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest
mongrel_rails cluster::start</pre></td></tr></table></div>

<p>You should be able to test your application by going to http://localhost:8000. You could also hit any of the 8 ports you have configured.</p>
<h4>Install Apache</h4>
<p>We have come to the last part of this lengthy installation. Apache. Now, it must be understood that you MUST install Apache 2.2 or higher. The reason for this is the Apache Proxy Balance Module. This module is required to glue all this together and is ONLY available in Apache 2.2 or higher. I am going to build from source on this one because the current version available to me in the apt-get library is not the sufficient version required.</p>
<p>Download Apache, unpack it, and then change to the directory which contains the unpacked files.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">wget http:<span style="color: #808080; font-style: italic;">//archive.apache.org/dist/httpd/httpd-2.2.3.tar.gz</span>
tar xvfz httpd-2.2.3.tar.gz
cd httpd-2.2.3</pre></td></tr></table></div>

<p>Configure the source tree for compiling Apache on your system:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">.<span style="color: #66cc66;">/</span>configure --prefix=<span style="color: #66cc66;">/</span>usr<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>apache2 \
--enable-mods-shared=all \
--disable-deflate \
--enable-proxy \
--enable-proxy-balancer \
--enable-proxy-http</pre></td></tr></table></div>

<p>Build and install using the following commands:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">make
sudo make install</pre></td></tr></table></div>

<p>Start Apache and navigate to your server through a browser. You should see the default Apache homepage.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="io" style="font-family:monospace;">sudo <span style="color: #66cc66;">/</span>usr<span style="color: #66cc66;">/</span>local<span style="color: #66cc66;">/</span>apache2<span style="color: #66cc66;">/</span>bin<span style="color: #66cc66;">/</span>apachectl start</pre></td></tr></table></div>

<p>There are several options for configuring and securing Apache that should be considered before taking this setup into production.</p>
<p>In order to configure Apache to serve your Mongrel cluster you will need to add the following to the Apache conf file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="io" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>Proxy balancer:<span style="color: #808080; font-style: italic;">//mongrel_cluster&gt;</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8000</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8001</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8002</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8003</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8004</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8005</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8006</span>
BalancerMember http:<span style="color: #808080; font-style: italic;">//127.0.0.1:8007</span>
<span style="color: #66cc66;">&lt;/</span>Proxy<span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>VirtualHost yourdomain.com:<span style="color: #cc66cc;">80</span><span style="color: #66cc66;">&gt;</span>
ServerAdmin yourname<span style="color: #66cc66;">@</span>yourdomain.com
ServerName  www.yourdomain.com
ServerAlias yourdomain.com
&nbsp;
<span style="color: #808080; font-style: italic;"># Indexes + Directory Root.</span>
DirectoryIndex test.html
DocumentRoot <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest<span style="color: #66cc66;">/</span>public<span style="color: #66cc66;">/</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>Directory <span style="color: #ff0000;">&quot;/tmp/railstest/public&quot;</span><span style="color: #66cc66;">&gt;</span>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
<span style="color: #66cc66;">&lt;/</span>Directory<span style="color: #66cc66;">&gt;</span>
&nbsp;
RewriteEngine On
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>HTTP_HOST<span style="color: #66cc66;">&#125;</span> ^yourdomain\.com$ <span style="color: #66cc66;">&#91;</span>NC<span style="color: #66cc66;">&#93;</span>
RewriteRule ^<span style="color: #66cc66;">&#40;</span>.<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span>$ http:<span style="color: #808080; font-style: italic;">//www. yourdomain.com$1 [R=301,L]</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Check for maintenance file and redirect all requests</span>
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>DOCUMENT_ROOT<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">system</span><span style="color: #66cc66;">/</span>maintenance.html -f
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>SCRIPT_FILENAME<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">!</span>maintenance.html
RewriteRule ^.<span style="color: #66cc66;">*</span>$ <span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">system</span><span style="color: #66cc66;">/</span>maintenance.html <span style="color: #66cc66;">&#91;</span>L<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Rewrite index to check for static</span>
RewriteRule ^<span style="color: #66cc66;">/</span>$ <span style="color: #66cc66;">/</span>index.html <span style="color: #66cc66;">&#91;</span>QSA<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Rewrite to check for Rails cached page</span>
RewriteRule ^<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>^.<span style="color: #66cc66;">&#93;</span>+<span style="color: #66cc66;">&#41;</span>$ $1.html <span style="color: #66cc66;">&#91;</span>QSA<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Redirect all non-static requests to cluster</span>
RewriteCond <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#123;</span>DOCUMENT_ROOT<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">/%</span><span style="color: #66cc66;">&#123;</span>REQUEST_FILENAME<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">!</span>-f
RewriteRule ^<span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#40;</span>.<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span>$ balancer:<span style="color: #808080; font-style: italic;">//mongrel_cluster%{REQUEST_URI} [P,QSA,L]</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Logfiles</span>
ErrorLog  <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest<span style="color: #66cc66;">/</span>log<span style="color: #66cc66;">/</span><span style="color: #000066;">error</span>.log
CustomLog <span style="color: #66cc66;">/</span>tmp<span style="color: #66cc66;">/</span>railstest<span style="color: #66cc66;">/</span>access.log combined
<span style="color: #66cc66;">&lt;/</span>VirtualHost<span style="color: #66cc66;">&gt;</span>
&nbsp;
NameVirtualHost yourdomain.com:<span style="color: #cc66cc;">80</span></pre></td></tr></table></div>

<p>Now, there are 3 pieces to take note of here. First, you need to create a Proxy group for the mongrel cluster we created. Since we created 8 nodes, there are 8 BalancerMembers. Second, you will need a VirtualHost entry for your Rails site (in this case the railstest site we created earlier). And Finally, you have to add the NameVirtualHost entry for your VirtualHost (I can&#8217;t tell you how many times I forget that).</p>
<p>That should get you going with a Mongrel Cluster behind Apache Proxy. If you want to setup more than one site, then just create 3 more entries in your Apache conf file. You may want to think about separating the declarations from the main httpd.conf file, but for this example I just wanted to get you going. Have Fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewcarriere.com/2008/01/18/running-ruby-on-rails-with-mongrel-clustering-and-apache-proxy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
