<?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>Alfa Jango Blog &#187; mod_rails</title>
	<atom:link href="http://www.alfajango.com/blog/tag/mod_rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alfajango.com/blog</link>
	<description>Engineering, Software, and Entrepreneurship</description>
	<lastBuildDate>Thu, 09 Sep 2010 17:52:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Make Sure Your Rails Application is Actually Caching (and not just pretending)</title>
		<link>http://www.alfajango.com/blog/make-sure-your-rails-application-is-actually-caching-and-not-just-pretending/</link>
		<comments>http://www.alfajango.com/blog/make-sure-your-rails-application-is-actually-caching-and-not-just-pretending/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 17:58:00 +0000</pubDate>
		<dc:creator>steve</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Phusion Passenger]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[mod_rails]]></category>
		<category><![CDATA[passenger]]></category>

		<guid isPermaLink="false">http://www.alfajango.com/blog/?p=177</guid>
		<description><![CDATA[We recently worked on a Rails application that had page and action caching set up, only to find that it was not actually working. It occurred to me that many Rails/Passenger/Apache applications may have caching set up in a way that it appears to be caching, when it is not actually caching.]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.alfajango.com/blog/wp-content/uploads/2010/03/rails-cache-broken.jpg"><img class="alignright size-full wp-image-181" title="rails-cache-broken" src="http://www.alfajango.com/blog/wp-content/uploads/2010/03/rails-cache-broken.jpg" alt="" width="300" height="312" /></a>We recently worked on a Rails application that had page and action caching set up, only to find that it was not actually working. It occurred to me that many Rails/Passenger/Apache applications may have caching set up in a way that it appears to be caching, when it is not actually caching. Searching through the interwebs for various Passenger/Apache configurations, such as <a href="http://wiki.github.com/browsermedia/browsercms/page-caching-with-phusion-passenger">this snippet on Github</a> or <a href="http://groups.google.com/group/phusion-passenger/browse_thread/thread/dde48ec055f6519f">this discussion on Google Groups</a>, I found that many did not work with the most recent version of <a href="http://modrails.com/">Phusion Passenger</a>. What&#8217;s more, these configurations give the appearance that they <em>are</em> working.
</p><p>
For an introduction to caching with Rails, check out this post: <a href="http://guides.rubyonrails.org/caching_with_rails.html">Caching With Rails: An Overview</a>
</p>
<h2>The Appearance That Caching Works</h2>
<p>
Here is what I mean by giving the appearance that caching is working:
</p><p>
<ol>
	<li>you add <code class="codecolorer rails default"><span class="rails">caches_page <span style="color:#ff3333; font-weight:bold;">:index</span></span></code> to your controller, for example</li>
	<li>configure and restart your server</li>
	<li>load the index page</li>
	<li>check your rails cache directory on your server, and see the index.html</li>
	<li>it works! (hint: this step is wrong, you&#8217;re not quite there yet)</li>
</ol>
</p><p>
Just because Rails is generating your cached page does not mean that your server is subsequently serving the cached <code class="codecolorer text default"><span class="text">.html</span></code> file instead of sending the request to Rails again.
</p>
<h2>Ensure It&#8217;s Serving the Cached Page</h2>
<p>
To make sure it&#8217;s subsequently serving the cached page, the easiest method is to look at the created date on the cached file on your server, for instance:
</p>
<span id="more-177"></span>
<p>

<div class="codecolorer-container bash twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> myapp<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>cache</div></div>

</p><p>
You will get an output similar to this:
</p><p>

<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-rw-r--r-- 1 deploy deploy 10930 2010-03-09 17:49 index.html</div></div>

</p><p>
Now wait a minute, and then reload the page in your browser. Once again type <code class="codecolorer bash default"><span class="bash"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> myapp<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>cache</span></code>, and make sure that the file still shows the same created date/time. If it instead shows this:
</p><p>

<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-rw-r--r-- 1 deploy deploy 10930 2010-03-09 17:50 index.html</div></div>

</p><p>
&#8230;then you have a problem. Your server is sending every request back to Rails, where Rails is then re-generating the cached page.
</p><p>
<em><strong>Alternative 1</strong>: You can also use Apache&#8217;s <code class="codecolorer apache default"><span class="apache"><span style="color: #00007f;">RewriteLog</span></span></code> to watch what is being served. Just add this to your site&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file and then <code class="codecolorer bash default"><span class="bash"><span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-f</span></span></code> the rewrite_log:</em>
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:<span style="color: #ff0000;">80</span>&gt;<br />
&nbsp; ...<br />
&nbsp; <span style="color: #adadad; font-style: italic;"># Comment out to disable rewrite debugging</span><br />
&nbsp; <span style="color: #00007f;">RewriteLog</span> /path/to/myapp/current/log/rewrite_log<br />
&nbsp; <span style="color: #00007f;">RewriteLogLevel</span> 9<br />
&nbsp; ...<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p><p>
<em><strong>Alternative 2:</strong> You can also use the <code class="codecolorer bash default"><span class="bash">top</span></code> command to watch your running processes and make sure the rails process doesn&#8217;t rise to the top while reloading your page. This is obviously not very scientific, though, and won&#8217;t work if you have any external traffic on the site.</em>
<h2>The Solution: Proper Server Configuration</h2>
The problem here is that your server is not properly configured to intercept calls to cached pages. This could take a bit of Googling and debugging, but the important thing here is that we&#8217;ve identified the problem and how to tell when it&#8217;s fixed. For completion&#8217;s sake, I&#8217;ll go ahead and show the proper configuration we&#8217;ve determined for Passenger 2.2.8:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:80&gt;<br />
&nbsp; ...<br />
&nbsp; RailsAllowModRewrite <span style="color: #0000ff;">on</span><br />
&nbsp; <span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span><br />
&nbsp; <span style="color: #adadad; font-style: italic;">#apache should serve cached pages</span><br />
&nbsp; <span style="color: #00007f;">RewriteRule</span> ^/$ /cache/index.html [QSA]<br />
&nbsp; <span style="color: #00007f;">RewriteRule</span> ^([^.]+)$ /cache/$1.html [QSA]<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p>
<div class="in-depth">
UPDATE: Christoffer Sawicki has <a href="http://termos.vemod.net/page-caching-with-rails-and-passenger">a set of rewrite rules</a> that I like much better than what I originally posted here. These rules explicitly check to make sure the cached file exists and that the current request is a GET request (instead of a POST, PUT, or DELETE), without relying on Rails magic. In short, use these instead:
<p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:80&gt;<br />
&nbsp; ...<br />
&nbsp; RailsAllowModRewrite <span style="color: #0000ff;">On</span> &nbsp;<br />
&nbsp; <span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span><br />
<br />
&nbsp; <span style="color: #00007f;">RewriteCond</span> %{THE_REQUEST} ^(GET|HEAD)<br />
&nbsp; <span style="color: #00007f;">RewriteCond</span> %{REQUEST_URI} ^/([^.]+)$<br />
&nbsp; <span style="color: #00007f;">RewriteCond</span> %{DOCUMENT_ROOT}/cache/%1.html -f<br />
&nbsp; <span style="color: #00007f;">RewriteRule</span> ^/[^.]+$ /cache/%1.html [QSA,L]<br />
<br />
&nbsp; <span style="color: #00007f;">RewriteCond</span> %{THE_REQUEST} ^(GET|HEAD)<br />
&nbsp; <span style="color: #00007f;">RewriteCond</span> %{DOCUMENT_ROOT}/cache/index.html -f<br />
&nbsp; <span style="color: #00007f;">RewriteRule</span> ^/$ /cache/index.html [QSA,L]<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p>
</div>
<p>
Happy caching! For more tips for getting the best performance out of your server, check out <a href="http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/">Performance Tuning for Phusion Passenger (An Introduction)</a>, and <a href="http://www.alfajango.com/blog/how-to-monitor-your-railspassenger-app-with-munin/">How to Monitor Your Rails/Passenger App with Munin</a>.
</p>
<h2><em>Update: Proper Nginx Configuration</em></h2>
<p>
For extra completeness, thank you to <em>atambo</em> on <a href="http://news.ycombinator.com/item?id=1185473">Hacker News</a> for pointing me to <a href="http://brainspl.at/nginx.conf.txt">this proper configuration for Rails caching on Nginx</a>. You can read the comments for an explanation.
</p>]]></content:encoded>
			<wfw:commentRss>http://www.alfajango.com/blog/make-sure-your-rails-application-is-actually-caching-and-not-just-pretending/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performance Tuning for Phusion Passenger (an Introduction)</title>
		<link>http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/</link>
		<comments>http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 13:54:07 +0000</pubDate>
		<dc:creator>steve</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[mod_rails]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[performance tuning]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[swap]]></category>

		<guid isPermaLink="false">http://www.alfajango.com/blog/?p=142</guid>
		<description><![CDATA[Once you've launched your application and people start actually using it, you may find server experiencing excessive swapping once the traffic begins to pick up. Before getting too deep into scaling, caching, upgrading your server, etc., there are a few Passenger settings you can tune to wring out the best performance your existing stack. I'll address these settings with a few rules of thumb we've gathered from the Passenger documentation and simple trial-and-error.]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://www.alfajango.com/blog/wp-content/uploads/2010/03/Picture-10.png"><img class="size-medium wp-image-151 alignright" title="Picture 10" src="http://www.alfajango.com/blog/wp-content/uploads/2010/03/Picture-10-237x300.png" alt="" width="237" height="300" /></a><a href="http://www.modrails.com/">Phusion Passenger</a> (aka mod_rails) allows for easy and scalable deployment of Ruby on Rails applications on Apache or Nginx servers. Part of what makes it so easy is that it comes with suitable default settings right out of the box, so that you don&#8217;t need to concern yourself with any of the details when deploying your application to production.
</p><p>
However, once you&#8217;ve launched your application and people start actually using it, you may find server experiencing excessive swapping once the traffic begins to pick up. Before getting too deep into scaling, caching, upgrading your server, etc., there are a few Passenger settings you can tune to wring out the best performance your existing stack. I&#8217;ll address these settings with a few rules of thumb we&#8217;ve gathered from the <a href="http://www.modrails.com/documentation.html">Passenger documentation</a> and simple trial-and-error.
</p><p>
Before getting started, we highly recommend employing Munin (<a href="http://www.alfajango.com/blog/how-to-monitor-your-railspassenger-app-with-munin/">instructions for Munin with Passenger here</a>) and NewRelic Performance Monitoring for your Passenger/Rails application, so that you can fine-tune your settings for the best performance.
</p><p>
<em>Note: the following settings are specific for Apache, you will need to modify the syntax/file-names for Nginx.</em>
</p>
<h2>Per-server Settings</h2>
<p>
<span style="font-weight: normal;">These settings are set per-server, meaning if you have multiple applications running on the server, these should be set only once throughout all of your <code class="codecolorer text default"><span class="text">.conf</span></code> files. If you have only one Rails/Passenger app on the server, then you may set these in that application&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file. On Ubuntu, this may be found at <code class="codecolorer text default"><span class="text">/etc/apache2/sites-available/yourapp.conf</span></code>. </span>
</p>
<span id="more-142"></span>
<p>
A more appropriate place for these however would be in Apache&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file.
</p>
<h3>PassengerMaxPoolSize and PassengerPoolIdleTime</h3>
<p>
<code class="codecolorer text default"><span class="text">/etc/apache2/apache2.conf</span></code>:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># PassengerMaxPoolSize</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 6</span><br />
<span style="color: #adadad; font-style: italic;"># For 2gb RAM: 30</span><br />
<span style="color: #adadad; font-style: italic;"># For 256 slice with MySQL running: 2</span><br />
PassengerMaxPoolSize <span style="color: #ff0000;">2</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># PassengerPoolIdleTime</span><br />
<span style="color: #adadad; font-style: italic;"># Recommended to be avg time per page * 2</span><br />
<span style="color: #adadad; font-style: italic;"># In Google Analytics... (Avg time on site / Avg page views) * 2</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 300</span><br />
PassengerPoolIdleTime <span style="color: #ff0000;">150</span></div></div>

</p><p>
You can essentially copy and paste this into your apache conf file. I&#8217;ve added some notes in there to help you remember how they should be tuned.</p>
<p>
<a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_passengermaxpoolsize_lt_integer_gt"><code class="codecolorer apache default"><span class="apache">PassengerMaxPoolSize</span></code></a>: This is the number of Passenger instances that can be spawned at once for your site. This basically relates directly to the amount of RAM that is available to your application. However, it is not a linear relationship. It&#8217;s more of an exponential relationship. Numbers that we and others have found to work well are:</p>
<table>
<tbody>
<tr>
<th>RAM (MB)</th>
<th>PassengerMaxPoolSize</th>
</tr>
<tr>
<td>256</td>
<td>2</td>
</tr>
<tr>
<td>512</td>
<td>6 (default)</td>
</tr>
<tr>
<td>1024 (1GB)</td>
<td>15</td>
</tr>
<tr>
<td>2048 (2GB)</td>
<td>30</td>
</tr>
</tbody>
</table>
<p>
Since this depends on available RAM, this will vary depending on what other processes are running on your server, such as MySQL, etc. If you have Munin installed with the Passenger plugins, you can monitor the Passenger Memory Stats and Passenger Status graphs to tweak this as necessary.
</p><p>
<a href="http://modrails.com/documentation/Users%20guide%20Apache.html#PassengerPoolIdleTime"><code class="codecolorer apache default"><span class="apache">PassengerPoolIdleTime</span></code></a>: This is essentially the amount of time (in seconds) that each Passenger instance continues to run after it becomes idle (i.e. someone stops clicking around the site). According to the documentation, this should be set to the average time a visitor views a page multiplied by 2. This of course prevents Passenger instances from shutting down (and thus having to re-spawn) while someone is viewing a page.
</p><p>
If your application has been in production long enough to have a few hundred viewers and you have Google Analytics installed, an easy way to get the ideal setting for your application is to take the Average Time on Site, divide by the Average Page Views per Visit, and multiply by 2.
</p><p>

<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">( Average Time on Site / Average Page Views per Visit ) x 2</div></div>

</p>
<h3>PassengerMaxInstancesPerApp (optional)</h3>
<p>
If you do have more than one Rails/Passenger application on the server, you may optionally set <code class="codecolorer text default"><span class="text">PassengerMaxInstancesPerApp</span></code>.
</p><p>
<a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_passengermaxinstancesperapp_lt_integer_gt"><code class="codecolorer apache default"><span class="apache">PassengerMaxInstancesPerApp</span></code></a>: This is the maximum number of instances each application may use of the total <code class="codecolorer text default"><span class="text">PassengerMaxPoolSize</span></code>. Therefore, <code class="codecolorer text default"><span class="text">PassengerMaxInstancesPerApp</span></code> must be smaller than <code class="codecolorer text default"><span class="text">PassengerMaxPoolSize</span></code>.
</p><p>
For example, if you have 2 applications running on one 256MB server, you may want to set <code class="codecolorer text default"><span class="text">PassengerMaxInstancesPerApp</span></code> to 1, so that one app may never hog all of the rails instances at the expense of the other app. If you have 2 applications running on a 512MB server (<code class="codecolorer text default"><span class="text">PassengerMaxPoolSize</span></code> 6), then you may want to set cci]PassengerMaxInstancesPerApp[/cci] to 4 or 5 to allow each app to better handle higher load (hopefully while the other app is under less load).
</p>
<h3>RailsAutoDetect</h3>
<p>
To get a little more performance out of your server, you can also turn the <a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_railsautodetect_lt_on_off_gt"><code class="codecolorer apache default"><span class="apache">RailsAutoDetect</span></code></a> setting of Passenger off. This will make it so that each request through Apache does not check to see if it is a Rails request that requires Passenger. However, this will now require that you explicitly tell Apache which requests are Rails and do require Passenger, using <a href="http://modrails.com/documentation/Users%20guide%20Apache.html#RailsBaseURI"><code class="codecolorer apache default"><span class="apache">RailsBaseURI</span></code></a>. This is easy though.
</p><p>
Again, in your <code class="codecolorer text default"><span class="text">apache2.conf</span></code> file:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># Must be used with RailsBaseURI in Vhost</span><br />
RailsAutoDetect <span style="color: #0000ff;">off</span></div></div>

</p><p>
Now, within your the Vhost block in your site&#8217;s <code class="codecolorer text default"><span class="text">.conf</span></code> file, you will need to set the <code class="codecolorer text default"><span class="text">RailsBaseURI</span></code>:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:80&gt;<br />
&nbsp; ...<br />
&nbsp; RailsBaseURI /<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p><p>
Here is a complete example of the Passenger block in your <code class="codecolorer text default"><span class="text">apache2.conf</span></code> file:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># PassengerMaxPoolSize</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 6</span><br />
<span style="color: #adadad; font-style: italic;"># For 2gb RAM: 30</span><br />
<span style="color: #adadad; font-style: italic;"># For 256 slice with MySQL running: 2</span><br />
PassengerMaxPoolSize <span style="color: #ff0000;">2</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># PassengerPoolIdleTime</span><br />
<span style="color: #adadad; font-style: italic;"># Recommended to be avg time per page * 2</span><br />
<span style="color: #adadad; font-style: italic;"># In Google Analytics... (Avg time on site / Avg page views) * 2</span><br />
<span style="color: #adadad; font-style: italic;"># Default: 300</span><br />
PassengerPoolIdleTime <span style="color: #ff0000;">144</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># PassengerMaxInstancesPerApp &lt; PassengerMaxPoolSize </span><br />
PassengerMaxInstancesPerApp <span style="color: #ff0000;">1</span><br />
<br />
<span style="color: #adadad; font-style: italic;"># Must be used with RailsBaseURI in Vhost</span><br />
RailsAutoDetect <span style="color: #0000ff;">off</span></div></div>

</p></p><p>
And a simple <code class="codecolorer text default"><span class="text">sites-available/yourapp.conf</span></code> may look like this:
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:80&gt;<br />
&nbsp; <span style="color: #00007f;">ServerName</span> www.yourapp.com<br />
&nbsp; <span style="color: #00007f;">DocumentRoot</span> /home/deploy/yourapp/current/public<br />
&nbsp; &lt;<span style="color: #000000; font-weight:bold;">Directory</span> <span style="color: #7f007f;">&quot;/home/deploy/yourapp/current/public&quot;</span>&gt;<br />
&nbsp; &nbsp; <span style="color: #00007f;">Options</span> <span style="color: #0000ff;">FollowSymLinks</span><br />
&nbsp; &nbsp; <span style="color: #00007f;">AllowOverride</span> <span style="color: #0000ff;">None</span><br />
&nbsp; &nbsp; <span style="color: #00007f;">Order</span> <span style="color: #00007f;">allow</span>,<span style="color: #00007f;">deny</span><br />
&nbsp; &nbsp; <span style="color: #00007f;">Allow</span> from <span style="color: #0000ff;">all</span><br />
&nbsp; &lt;/<span style="color: #000000; font-weight:bold;">Directory</span>&gt;<br />
&nbsp; RailsBaseURI /<br />
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</div></div>

</p>
<h2>More Passenger Configuration Settings</h2>
<p>
These settings so far only scratch the surface of Passenger configuration. Give the <a href="http://modrails.com/documentation/Users%20guide%20Apache.html#_configuring_phusion_passenger">Passenger documentation</a> a good read-through for more information on configuring Passenger.
</p>
<h2>Further Reading: Apache Settings</h2>
<p>
At this point, if you still need to squeeze more performance out of your server (and you will eventually), you&#8217;ll want to configure Apache to utilize its resources most efficiently with Passenger. I recommend reading through <a href="http://forum.slicehost.com/comments.php?DiscussionID=1776">this thread on the Slicehost forums</a> to get a good idea just how much trial-and-error this will take. <strong>Keep a close eye on your server when you tweak these settings, you may end up making it slower several times before finding that sweet spot.</strong>
</p><p>
You may read the <a href="http://httpd.apache.org/docs/1.3/mod/core.html">documentation for each setting in the Apache docs</a> (note that these are the Apache 1.3 docs, but they still explain purpose of each setting well). For more information using these settings to reduce the RAM consumed by Apache, see <a href="http://forum.slicehost.com/comments.php?DiscussionID=3313">this other Slicehost forum</a> and <a href="http://programming-gone-awry.blogspot.com/2009/06/how-to-save-much-ram-when-running-rails.html">this blog post on saving RAM with Rails and Passenger</a>.
</p><p>
If you&#8217;re impatient and just want to get something better than default, here are some numbers that worked well for us on a recent application running on a 256MB VPS on Slicehost. These settings can be found in your <code class="codecolorer text default"><span class="text">apache2.conf</span></code> file.
</p><p>

<div class="codecolorer-container apache twitlight" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:300px;"><div class="apache codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #adadad; font-style: italic;"># prefork MPM</span><br />
<span style="color: #adadad; font-style: italic;"># StartServers: number of server processes to start</span><br />
<span style="color: #adadad; font-style: italic;"># MinSpareServers: minimum number of server processes which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># MaxSpareServers: maximum number of server processes which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># MaxClients: maximum number of server processes allowed to start</span><br />
<span style="color: #adadad; font-style: italic;"># MaxRequestsPerChild: maximum number of requests a server process serves</span><br />
&lt;<span style="color: #000000; font-weight:bold;">IfModule</span> mpm_prefork_module&gt;<br />
&nbsp; <span style="color: #00007f;">StartServers</span>          2<br />
&nbsp; <span style="color: #00007f;">MinSpareServers</span>       1<br />
&nbsp; <span style="color: #00007f;">MaxSpareServers</span>       5<br />
&nbsp; <span style="color: #00007f;">MaxClients</span>           10<br />
&nbsp; <span style="color: #00007f;">MaxRequestsPerChild</span>   100<br />
&lt;/<span style="color: #000000; font-weight:bold;">IfModule</span>&gt;<br />
<span style="color: #adadad; font-style: italic;"># worker MPM</span><br />
<span style="color: #adadad; font-style: italic;"># StartServers: initial number of server processes to start</span><br />
<span style="color: #adadad; font-style: italic;"># MaxClients: maximum number of simultaneous client connections</span><br />
<span style="color: #adadad; font-style: italic;"># MinSpareThreads: minimum number of worker threads which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># MaxSpareThreads: maximum number of worker threads which are kept spare</span><br />
<span style="color: #adadad; font-style: italic;"># ThreadsPerChild: constant number of worker threads in each server process</span><br />
<span style="color: #adadad; font-style: italic;"># MaxRequestsPerChild: maximum number of requests a server process serves</span><br />
&lt;<span style="color: #000000; font-weight:bold;">IfModule</span> mpm_worker_module&gt;<br />
&nbsp; <span style="color: #00007f;">StartServers</span>          2<br />
&nbsp; <span style="color: #00007f;">MaxClients</span>           10<br />
&nbsp; <span style="color: #00007f;">MinSpareThreads</span>      25<br />
&nbsp; <span style="color: #00007f;">MaxSpareThreads</span>      75<br />
&nbsp; <span style="color: #00007f;">ThreadsPerChild</span>      25<br />
&nbsp; <span style="color: #00007f;">MaxRequestsPerChild</span>   100<br />
&lt;/<span style="color: #000000; font-weight:bold;">IfModule</span>&gt;</div></div>

</p>]]></content:encoded>
			<wfw:commentRss>http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
