<?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>jonathanjulian.com &#187; mod_rails</title>
	<atom:link href="http://jonathanjulian.com/category/mod_rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathanjulian.com</link>
	<description>Ruby, Rails, JavaScript, software development</description>
	<lastBuildDate>Fri, 13 Jan 2012 21:03:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Generate mod_rails httpd.conf using cap</title>
		<link>http://jonathanjulian.com/2008/04/generate-mod_rails-httpd-conf-using-cap/</link>
		<comments>http://jonathanjulian.com/2008/04/generate-mod_rails-httpd-conf-using-cap/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 01:33:43 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Capistrano]]></category>
		<category><![CDATA[mod_rails]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://jonathanjulian.com/?p=6</guid>
		<description><![CDATA[With all the discussions around the new mod_rails (aka Passenger), I thought I&#8217;d post the Apache conf that I came up with. Here&#8217;s the strategy: generate httpd.conf using cap, upload it to the server, and graceful restart Apache. This way, you don&#8217;t have to get your hands dirty logging into the box to reconfigure anything. [...]]]></description>
			<content:encoded><![CDATA[<p>With all the discussions around the new <a href="http://modrails.com">mod_rails</a> (aka Passenger), I thought I&#8217;d post the Apache conf that I came up with. Here&#8217;s the strategy: generate httpd.conf using cap, upload it to the server, and graceful restart Apache. This way, you don&#8217;t have to get your hands dirty logging into the box to reconfigure anything.</p>

<p>Here&#8217;s the erb template for httpd.conf:</p>

<pre><code>#
# Apache httpd.conf
# generated by cap. run by &lt;%= user %&gt; at &lt;%= Time.now %&gt;
#

ServerRoot "&lt;%= current_release %&gt;"
PidFile "&lt;%= current_release %&gt;/tmp/pids/httpd.pid"
LockFile "&lt;%= current_release %&gt;/log/accept.lock"
ServerName localhost
ServerAdmin "me"
Listen &lt;%= web_port %&gt;

# prefork MPM
# StartServers: number of server processes to start
# MinSpareServers: minimum number of server processes which are kept spare
# MaxSpareServers: maximum number of server processes which are kept spare
# MaxClients: maximum number of server processes allowed at a time
# MaxRequestsPerChild: maximum number of requests a server process serves until death
StartServers          &lt;%= apache_processes %&gt;
MinSpareServers       &lt;%= apache_processes %&gt;
MaxSpareServers       &lt;%= apache_processes %&gt;
MaxClients            100
MaxRequestsPerChild   0

ErrorLog "&lt;%= current_release %&gt;/log/apache_error_log"
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg
LogLevel info
LogFormat "%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog "&lt;%= current_release %&gt;/log/apache_access_log" combined
#RewriteLog "&lt;%= current_release %&gt;/log/apache_rewrite_log"
# 0-9, 0 = off, 2 and 3 is good for debugging
#RewriteLogLevel 3

ServerTokens Prod
ServerSignature Off

DefaultType text/plain
TypesConfig /usr/local/apache2/conf/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz

&lt;% if behind_https %&gt;
# for envs that sit behind an https load balancer
RequestHeader set X-FORWARDED_PROTO https
&lt;% end %&gt;

# using Passenger (http://www.modrails.com/) to serve Rails
LoadModule passenger_module &lt;%= gem_home %&gt;/passenger-1.0.1/ext/apache2/mod_passenger.so
RailsSpawnServer            &lt;%= gem_home %&gt;/passenger-1.0.1/bin/passenger-spawn-server
RailsRuby             &lt;%= ruby_binary %&gt;

RailsUserSwitching    off
# max number of Rails processes to be spawned at a time
RailsMaxPoolSize      &lt;%= max_rails_processes %&gt;
# spawner processes will be killed if idle for 60 minutes
RailsPoolIdleTime     3600
RailsEnv              &lt;%= environment %&gt;

RailsAllowModRewrite  on
RewriteEngine On
# Check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ %{DOCUMENT_ROOT}/system/maintenance.html [L]

DocumentRoot "&lt;%= current_release %&gt;/public"
&lt;Directory &lt;%= current_release %&gt;/public&gt;
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all

    # Set the max size for file uploads to 10M
    LimitRequestBody 10485760
&lt;/Directory&gt;

&lt;Directory /&gt;
    Options FollowSymLinks
    AllowOverride None
    Order Deny,Allow
    Deny from all
&lt;/Directory&gt;
</code></pre>

<p>Here is the cap task to generate httpd.conf and upload it to the server:</p>

<pre><code>desc "Generate the apache httpd.conf file from the template"
task :generate_apache_httpd_conf_file, :roles =&gt; :app do
  require 'erb'
  # set up defaults for some vars to go into the template. others are required.
  apache_processes = fetch(:www_procs, 2)
  max_rails_processes = fetch(:max_rails_procs, 2)
  ruby_binary = fetch(:ruby_binary, "ruby")
  gem_home = fetch(:gem_home, '/usr/local/lib/ruby/gems/1.8/gems')
  behind_https = fetch(:fronted_by_https, false)
  template = ERB.new(File.read('config/templates/httpd.conf.erb'), nil, '&lt;&gt;')
  result = template.result(binding)
  put(result, "#{current_release}/httpd.conf")
end
</code></pre>

<p>It runs in an after_update_code task. This way, <strong>all server configs are under code control.</strong> This is huge! Each deployed environment&#8217;s settings are known and tracked &#8211; not just loosely keyed on each server. I treat Apache as part of our application &#8211; not a service that happens to be running that we deploy into. Using this technique, along with <a href="http://errtheblog.com/posts/50-vendor-everything">vendor_everything</a>, we&#8217;ve minimized our host dependencies.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanjulian.com/2008/04/generate-mod_rails-httpd-conf-using-cap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

