<?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>xambr::blog &#187; nginx</title>
	<atom:link href="http://blog.xambr.com/tag/nginx/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xambr.com</link>
	<description>crafting my thoughts</description>
	<lastBuildDate>Tue, 20 Jul 2010 18:57:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Running Ramaze with Unicorn, Nginx and God</title>
		<link>http://blog.xambr.com/2010/01/16/running-ramaze-with-unicorn-nginx-and-god/</link>
		<comments>http://blog.xambr.com/2010/01/16/running-ramaze-with-unicorn-nginx-and-god/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 01:19:15 +0000</pubDate>
		<dc:creator>max.rb</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[god]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[ramaze]]></category>
		<category><![CDATA[unicorn]]></category>

		<guid isPermaLink="false">http://blog.xambr.com/?p=205</guid>
		<description><![CDATA[I&#8217;ve been meaning to write a post about running a Ramaze website under a reasonable setup for a long time now so I&#8217;m happy to finally get to it. I&#8217;m going to focus my attention on the configuration of the setup vs the installation of the software - RACK, Ramaze, Unicorn and God are ruby gems and there are plenty of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been meaning to write a post about running a <a href="http://ramaze.net/" target="_blank">Ramaze</a> website under a reasonable setup for a long time now so I&#8217;m happy to finally get to it. I&#8217;m going to focus my attention on the configuration of the setup vs the installation of the software - <a href="http://rack.rubyforge.org/" target="_blank">RACK</a>, <a href="http://ramaze.net/" target="_blank">Ramaze</a>, <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> and <a href="http://god.rubyforge.org/" target="_blank">God</a> are ruby gems and there are plenty of good articles about installing <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> out there already.</p>
<h2>What You Need</h2>
<p>I&#8217;ve included the version number of the software I&#8217;ve tested this setup with but I suspect this configuration will work with newer versions as well (and probably older versions too).</p>
<ol>
<li><a href="http://rack.rubyforge.org/" target="_blank">RACK</a> (web server interface)
<ul>
<li>1.0.1</li>
</ul>
</li>
<li><a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> (http server)
<ul>
<li>0.95.2</li>
</ul>
</li>
<li><a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> (front end reverse proxy server)
<ul>
<li>0.6.36</li>
</ul>
</li>
<li><a href="http://god.rubyforge.org/" target="_blank">God</a> (process monitoring)
<ul>
<li>0.7.13</li>
</ul>
</li>
<li><a href="http://ramaze.net/" target="_blank">Ramaze</a> (web framework)
<ul>
<li>2009-05</li>
</ul>
</li>
</ol>
<h2>Some Assumptions</h2>
<p>While the config files below make the following assumptions you should be able to handle any discrepancies by altering the paths or changing the run user.</p>
<ol>
<li>Nginx is configured to read configuration files from its <code>conf/sites-enabled</code> directory
<ul>
<li>Add <code>include &lt;path to nginx install&gt;/conf/sites-enabled/*;</code> to the <code>http</code> section of your primary <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> config file to enable this</li>
</ul>
</li>
<li>Your website will run under the www-data user</li>
<li>The <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a>, <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> and <a href="http://god.rubyforge.org/" target="_blank">God</a> config files exist within the <code>config</code> directory of your <a href="http://ramaze.net/" target="_blank">Ramaze</a> website</li>
<li>The <a href="http://rack.rubyforge.org/" target="_blank">RACK</a> config file (config.ru) is in the root of your website</li>
<li>You&#8217;ve created the <code>log</code> and <code>tmp/pids</code> directories underneath the root of your website</li>
</ol>
<h2>Configuring the Pieces</h2>
<h3>1. <a href="http://rack.rubyforge.org/">RACK</a></h3>
<p>This config file is actually a very simple ruby file, its main purpose in this situation is to load your website&#8217;s <code>app.rb</code> via the require statement. This in turn loads the rest of your site.<br />
<script src="http://gist.github.com/278861.js?file=config.ru"></script></p>
<h3>3. <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a></h3>
<p>While I&#8217;ve commented the major parts of the configuration file inline, I highly recommend checking out the <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn website</a> and this <a href="http://unicorn.bogomips.org/examples/unicorn.conf.rb" target="_blank">sample configuration file</a> for more details/features.  <script src="http://gist.github.com/278861.js?file=unicorn.conf"></script></p>
<h3>2. <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a></h3>
<p>You can find more info about configuring Nginx <a href="http://wiki.nginx.org/NginxConfiguration">here</a>.  If assumptions #2 &amp; #4 are true then you&#8217;ll need to create a symlink in the <code>&lt;path-to-nginx-install&gt;/conf/sites-enabled/*</code> directory to your <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> config in the config directory under the root of your website. You can do this by executing</p>
<pre class="code">ln -s /var/www/apps/my.domain.com/config/nginx.conf &lt;path-to-nginx-install&gt;/conf/sites-enabled/my.domain.com</pre>
<p><script src="http://gist.github.com/278861.js?file=nginx.conf"></script></p>
<h3>4. God</h3>
<p>As with the RACK and Unicorn config files, this one is also written in Ruby. The God home page has a much more detailed breakdown of the config file and is definitely worth a read. <script src="http://gist.github.com/278861.js?file=god.conf"></script></p>
<h2>Flicking the Switch</h2>
<p>Once you have your configuration files in place and your website on your target server you&#8217;re going to need to turn it all on. The first step is to tell <a href="http://god.rubyforge.org/" target="_blank">God</a> to keep an eye on your <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> process and the second step is to cycle <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> forcing it to pickup your new website&#8217;s config file.</p>
<h3>Alerting <a href="http://god.rubyforge.org/" target="_blank">God</a></h3>
<p>The first thing to do is start <a href="http://god.rubyforge.org/" target="_blank">God</a> and have it monitor your master <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> process for your new website.  If <a href="http://god.rubyforge.org/" target="_blank">God</a> is not already running you can start it using:</p>
<pre class="code">god --log-level=debug</pre>
<p>To have it start monitoring your <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> process, execute the following:</p>
<pre class="code">sudo god load /var/www/apps/my.domain.com/config/god.conf</pre>
<h3>Cycling <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a></h3>
<p>Cycling <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> is something that cries out for a script so here is a simple one:<br />
<script src="http://gist.github.com/278905.js?file=cycle-nginx.sh"></script></p>
<h2>Dealing with Errors</h2>
<p>After flicking the switch for the first time under this setup I&#8217;ve run into a 502 bad gateway error multiple times. Each time it was because I had forgotten to create the required <code>tmp/pids</code> or <code>log</code> directories under the website root. If you do run into a 502 its probably because <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> had trouble starting up or <a href="http://wiki.nginx.org/Main" target="_blank">Nginx</a> is not pointing to the right spot.</p>
<h4>Debugging</h4>
<p>Check the <a href="http://god.rubyforge.org/" target="_blank">God</a> logs for your website by executing:</p>
<pre class="code">god --log-level debug log my.domain.com</pre>
<p>That will tell you if <a href="http://god.rubyforge.org/" target="_blank">God</a> has managed to start the <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> process successfully. If it has then checkout the <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> error logs in the log directory of your website. If it cannot start the process and doesn&#8217;t indicate why then try executing the <a href="http://unicorn.bogomips.org/" target="_blank">Unicorn</a> startup command in a shell directly (that should at least give you the error message Unicorn is generating).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.xambr.com/2010/01/16/running-ramaze-with-unicorn-nginx-and-god/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
