<?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; rubygems</title>
	<atom:link href="http://matthewcarriere.com/tag/rubygems/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>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>
