<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Say what???: Tag apache</title>
    <link>http://drotner.org/articles/tag/apache</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>by Kelly McCauley</description>
    <item>
      <title>Apache Setup for a Non-Root Rails App</title>
      <description>&lt;h2&gt;Introduction&lt;/h2&gt;


&lt;p&gt;Over the weekend, I wanted to put a small &lt;a href="http://rubyonrails.org/"&gt;Ruby on
Rails&lt;/a&gt; application that I had written for myself into
production.  This small article documents the setup and configuration that I
used to get my application running with &lt;a href="http://httpd.apache.org"&gt;Apache&lt;/a&gt; and a
&lt;a href="http://mongrel.rubyforge.org/"&gt;Mongrel&lt;/a&gt; cluster back end.  The article assumes
that the reader knows the basics of &lt;a href="http://httpd.apache.org/docs/2.2/"&gt;Apache
administration&lt;/a&gt;, &lt;a href="http://mongrel.rubyforge.org/docs/index.html"&gt;Mongrel
configuration&lt;/a&gt;, and &lt;a href="http://www.capify.org/"&gt;deployment of
Ruby on Rails applications&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are plenty of documentation references for installing a rails
application as the root web page.  The &lt;a href="http://www.pragmaticprogrammer.com/titles/rails/index.html"&gt;Agile Web Development with
Rails&lt;/a&gt; book, in the
&lt;em&gt;Setting Up a Deployment Enviroment&lt;/em&gt; section, gives a fairly detailed
description of the steps needed to deploy an application into production.  The
&lt;a href="http://mongrel.rubyforge.org/docs/apache.html"&gt;Mongrel: Apache&lt;/a&gt; documentation
is another excellent reference.  But I didn&amp;#8217;t want to install my rails
application at the root of my &lt;span class="caps"&gt;URL&lt;/span&gt;&amp;#8230;.&lt;/p&gt;

&lt;p&gt;My rails application is just a small &lt;span class="caps"&gt;URL&lt;/span&gt; manager called &lt;em&gt;Blink&lt;/em&gt; and I wanted
it to be served from the private section of my web pages:&lt;/p&gt;



&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;https://drotner.org:8443/blink/
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;I am using Apache as the front end to &lt;em&gt;Blink&lt;/em&gt; because I&amp;#8217;m using mod_svn to
host my &lt;a href="http://subversion.tigris.org/"&gt;Subversion&lt;/a&gt; repositories and didn&amp;#8217;t want
to have yet another front end web server to administer.  &lt;em&gt;Blink&lt;/em&gt; itself will be
running on a cluster of mongrel servers (initially, only one mongrel
server).&lt;/p&gt;

	&lt;h2&gt;Setup and Configuration&lt;/h2&gt;


&lt;p&gt;I first checked out a copy of &lt;em&gt;Blink&lt;/em&gt;&amp;#8217;s source code, configured &lt;em&gt;Blink&lt;/em&gt;, and
created the database.  The next step was to configure the mongrel cluster.  My
&lt;code&gt;mongre_cluster&lt;/code&gt; init.d script looks for server configurations in
&lt;code&gt;/etc/mongrel&lt;/code&gt;, so I created
&lt;code&gt;/etc/mongrel/blink.drotner.org.8443.mongrel_cluster.yml&lt;/code&gt;&lt;p&gt;



&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;--- &lt;tt&gt;
&lt;/tt&gt;prefix: /blink&lt;tt&gt;
&lt;/tt&gt;cwd: /var/www/ssl_drotner_org/apps/blink&lt;tt&gt;
&lt;/tt&gt;log_file: log/mongrel.log&lt;tt&gt;
&lt;/tt&gt;port: &amp;quot;8200&amp;quot;&lt;tt&gt;
&lt;/tt&gt;environment: production&lt;tt&gt;
&lt;/tt&gt;address: 127.0.0.1&lt;tt&gt;
&lt;/tt&gt;servers: 1&lt;tt&gt;
&lt;/tt&gt;user: mongrel&lt;tt&gt;
&lt;/tt&gt;group: mongrel&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;



&lt;p&gt;I set mongrel&amp;#8217;s &lt;code&gt;prefix&lt;/code&gt; option (line 2) since I want &lt;em&gt;Blink&lt;/em&gt; to run under
the &lt;code&gt;/blink/&lt;/code&gt; URL path.  I started up the mongrel cluster and then used &lt;code&gt;curl&lt;/code&gt;
to verify that &lt;em&gt;Blink&lt;/em&gt;&amp;#8217;s mongrel server was running.&lt;/p&gt;



&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;root@drotner:/etc/mongrel # curl http://127.0.0.1:8200/blink/
&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;You are being &amp;lt;a href=&amp;quot;http://127.0.0.1:8200/blink/authn_sessions/new&amp;quot;&amp;gt;redirected&amp;lt;/a&amp;gt;.&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;&lt;code&gt;curl&lt;/code&gt; outputted exactly what I was expecting.  The next step was
configuring Apache to proxy requests for &lt;code&gt;/blink/&lt;/code&gt; to the back end mongrel
cluster.&lt;/p&gt;



&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;11&lt;tt&gt;
&lt;/tt&gt;12&lt;tt&gt;
&lt;/tt&gt;13&lt;tt&gt;
&lt;/tt&gt;14&lt;tt&gt;
&lt;/tt&gt;15&lt;tt&gt;
&lt;/tt&gt;16&lt;tt&gt;
&lt;/tt&gt;17&lt;tt&gt;
&lt;/tt&gt;18&lt;tt&gt;
&lt;/tt&gt;19&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;20&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;21&lt;tt&gt;
&lt;/tt&gt;22&lt;tt&gt;
&lt;/tt&gt;23&lt;tt&gt;
&lt;/tt&gt;24&lt;tt&gt;
&lt;/tt&gt;25&lt;tt&gt;
&lt;/tt&gt;26&lt;tt&gt;
&lt;/tt&gt;27&lt;tt&gt;
&lt;/tt&gt;28&lt;tt&gt;
&lt;/tt&gt;29&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;30&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;31&lt;tt&gt;
&lt;/tt&gt;32&lt;tt&gt;
&lt;/tt&gt;33&lt;tt&gt;
&lt;/tt&gt;34&lt;tt&gt;
&lt;/tt&gt;35&lt;tt&gt;
&lt;/tt&gt;36&lt;tt&gt;
&lt;/tt&gt;37&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&amp;lt;VirtualHost 66.241.137.71:8443&amp;gt;&lt;tt&gt;
&lt;/tt&gt;  DocumentRoot &amp;quot;/var/www/ssl_drotner_org/htdocs-secure&amp;quot;&lt;tt&gt;
&lt;/tt&gt;  ServerName drotner.org:8443&lt;tt&gt;
&lt;/tt&gt;  ErrorLog /var/log/apache2/ssl_drotner_org_error_log&lt;tt&gt;
&lt;/tt&gt;  CustomLog /var/log/apache2/ssl_drotner_org_access_log combinedio&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  # ... SNIP ...&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  ProxyRequests Off&lt;tt&gt;
&lt;/tt&gt;  ProxyVia Full&lt;tt&gt;
&lt;/tt&gt;  RequestHeader set X_FORWARDED_PROTO 'https'&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  &amp;lt;Proxy balancer://mongrel_cluster&amp;gt;&lt;tt&gt;
&lt;/tt&gt;    BalancerMember http://127.0.0.1:8200&lt;tt&gt;
&lt;/tt&gt;    #BalancerMember http://127.0.0.1:8201&lt;tt&gt;
&lt;/tt&gt;  &amp;lt;/Proxy&amp;gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  &amp;lt;Proxy *&amp;gt;&lt;tt&gt;
&lt;/tt&gt;    Order Deny,Allow&lt;tt&gt;
&lt;/tt&gt;    Deny from all&lt;tt&gt;
&lt;/tt&gt;    Allow from all&lt;tt&gt;
&lt;/tt&gt;  &amp;lt;/Proxy&amp;gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  RewriteEngine On&lt;tt&gt;
&lt;/tt&gt;  # Check for  maintenance file and redirect all requests&lt;tt&gt;
&lt;/tt&gt;  RewriteCond  %{DOCUMENT_ROOT}/system/maintenance.html -f&lt;tt&gt;
&lt;/tt&gt;  RewriteCond  %{SCRIPT_FILENAME} !maintenance.html&lt;tt&gt;
&lt;/tt&gt;  RewriteRule  ^/blink/.*$ /system/maintenance.html [L]&lt;tt&gt;
&lt;/tt&gt;  # Rewrite /blink/ to check for static index page&lt;tt&gt;
&lt;/tt&gt;  # RewriteRule ^/blink/$ /index.html [QSA]&lt;tt&gt;
&lt;/tt&gt;  # Rewrite to check for Rails cached page&lt;tt&gt;
&lt;/tt&gt;  RewriteRule ^/blink/([^.]+)$ /blink/$1.html [QSA]&lt;tt&gt;
&lt;/tt&gt;  # Redirect all non-static requests to the mongrel cluster&lt;tt&gt;
&lt;/tt&gt;  RewriteCond %{DOCUMENT_ROOT}/blink/%{REQUEST_FILENAME} !-f&lt;tt&gt;
&lt;/tt&gt;  RewriteRule ^/blink/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&amp;lt;/VirtualHost&amp;gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;



&lt;p&gt;&lt;code&gt;ProxyRequests Off&lt;/code&gt; (line 9) is critical and keeps the Apache
server from being an open proxy server.&lt;/p&gt;

&lt;p&gt;Note that &lt;em&gt;Blink&lt;/em&gt; is being served over
&lt;a href="http://en.wikipedia.org/wiki/Transport_Layer_Security"&gt;&lt;span class="caps"&gt;TLS&lt;/span&gt;/SSL&lt;/a&gt;.
&lt;code&gt;RequestHeader set X_FORWARDED_PROTO 'https'&lt;/code&gt; (line 11), in a
nutshell, tells rails to write URLs with &lt;code&gt;https://&lt;/code&gt; for location redirects instead of the
default &lt;code&gt;http://&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Proxy balancer://mongrel_cluster&amp;gt;...&lt;/code&gt; (lines 13 &amp;#8211; 15) tells
Apache where to find &lt;em&gt;Blink&lt;/em&gt;&amp;#8217;s mongrel servers.  Each &lt;code&gt;BalanceMember&lt;/code&gt; must map
to a mongrel server running on the given port.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html"&gt;rewrite&lt;/a&gt; rules, lines 24 &amp;#8211; 35, do
the grunt work of deciding whether the requests go to the &lt;em&gt;Blink&lt;/em&gt; application
or elsewhere.&lt;/p&gt;

&lt;p&gt;Finally, I restarted my Apache server, and pointed my browser to
&lt;code&gt;https://drotner.org:8443/blink/&lt;/code&gt; and got the appropriate index page.  Now, back to what I enjoy doing &amp;#8230; programming.&lt;/p&gt;


</description>
      <pubDate>Mon, 17 Sep 2007 16:19:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4d15e06b-345b-4ff5-b224-48b07ef65ae6</guid>
      <author>Kelly McCauley</author>
      <link>http://drotner.org/articles/2007/09/17/apache-setup-for-a-non-root-rails-app</link>
      <category>SysAdmin</category>
      <category>rails</category>
      <category>apache</category>
      <category>mongrel</category>
    </item>
  </channel>
</rss>
