<?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>BuzaMoto &#187; Server</title>
	<atom:link href="http://blog.buzamoto.com/category/server/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.buzamoto.com</link>
	<description>Life as a BuzaMoto Employee</description>
	<lastBuildDate>Tue, 03 Aug 2010 20:54:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mongrel Cluster + Apache2 on Webfaction</title>
		<link>http://blog.buzamoto.com/2009/02/20/mongrel-cluster-apache2-on-webfaction/</link>
		<comments>http://blog.buzamoto.com/2009/02/20/mongrel-cluster-apache2-on-webfaction/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 10:03:25 +0000</pubDate>
		<dc:creator>mud</dc:creator>
				<category><![CDATA[Note]]></category>
		<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://blog.buzamoto.com/?p=78</guid>
		<description><![CDATA[One project I worked on recently required to set up a mongrel cluster on Webfaction. On a typical Rails setup, I have a mongrel cluster running under apache2. I thought it would be easy to set this up on Webfaction, but it wasn&#8217;t so straight forward, so I&#8217;m going to document it here.
First, the one-click [...]]]></description>
			<content:encoded><![CDATA[<p>One project I worked on recently required to set up a mongrel cluster on <a href="http://www.webfaction.com">Webfaction</a>. On a typical Rails setup, I have a mongrel cluster running under apache2. I thought it would be easy to set this up on Webfaction, but it wasn&#8217;t so straight forward, so I&#8217;m going to document it here.</p>
<p>First, the one-click rails app on webfaction assumes you will run one mongrel server per application. However, in order to handle multiple instances with load balancing, we will need to make some changes. The setup is not too complicated, but we need to run our own apache2. But before you install additional software, you should first make sure your app runs on the server with one instance of mongrel. Once you do that, you can follow the instructions below.</p>
<p><span id="more-78"></span></p>
<h2>Installing Stuff</h2>
<p>First, we need to install apache2. You can compile your own version by downloading the source, but webfaction makes it easy since they already have a one-click install for mod_python. If you install mod_python, it will install apache as a webapp under your webapp directory. That&#8217;s what I did.</p>
<p>Next, we&#8217;ll install the mongrel and mongrel_cluster gems. But if you haven&#8217;t install your own version of Ruby and RubyGems, you might want to follow <a href="https://help.webfaction.com/index.php?_m=knowledgebase&amp;_a=viewarticle&amp;kbarticleid=89">this knowledgebase article</a>. Run:</p>
<pre>gem install mongrel_cluster</pre>
<p>to install mongrel_cluster.</p>
<h2>Configuring Stuff</h2>
<h3>Control Panel</h3>
<p>In order to create new instances of mongrel, you need to go into the control panel to open up a new port. All you need to do is specify the app as a custom app. The custom app will not install anything, but it does create an empty directory in your webapps directory. With a custom app, webfaction just assigns a port number to you. There&#8217;s no need to run an app within a specific directory. So go ahead and create as many apps as you need instances, and note the port numbers.</p>
<h3>httpd.conf</h3>
<p>We now need to setup the httpd.conf by turning off the mod_python stuff, and turning on some extra modules. If you installed apache through the one-click install, your httpd.conf file lives here:</p>
<pre>~/webapps/apache/apache2/conf/httpd.conf</pre>
<p>Open that up in your favorite text editor and replace the contents with this:</p>
<pre>ServerRoot "/home/<strong>username</strong>/webapps/apache/apache2"

LoadModule authz_host_module modules/mod_authz_host.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule alias_module modules/mod_alias.so

DocumentRoot /home/<strong>username</strong>/webapps/apache/htdocs
KeepAlive Off
Listen <strong>7708</strong>
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog logs/access_log combined
ServerLimit 2

NameVirtualHost *:<strong>7708</strong>

&lt;VirtualHost *:<strong>7708</strong>&gt;
  ServerName <strong>example.com</strong>
  ServerAlias <strong>example</strong>.webfactional.com www 

  DocumentRoot /home/<strong>username</strong>/webapps/website_mongrel_source/public

  RewriteEngine On

  &lt;Proxy balancer://mongrel&gt;
    BalancerMember http://127.0.0.1:<strong>7777</strong>
    BalancerMember http://127.0.0.1:<strong>7779</strong>
  &lt;/Proxy&gt;

  &lt;Directory /home/<strong>username</strong>/webapps/<strong>railsapp</strong>/public&gt;
     Order allow,deny
     Allow from all
  &lt;/Directory&gt;

  # Let apache serve static files
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
  RewriteRule (.*) $1 [L]

  # Rewrite index to check for static
  RewriteRule ^/$ /index.html [QSA]

  # Rewrite to check for Rails cached page
  RewriteRule ^([^.]+)$ $1.html [QSA]

  # Redirect all non-static requests to cluster
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://mongrel%{REQUEST_URI} [P,QSA,L]

  ProxyPass / balancer://mongrel/
  ProxyPassReverse / balancer://mongrel/
  ProxyPreserveHost on

  # Custom log file locations
  ErrorLog  /home/<strong>username</strong>/webapps/<strong>railsapp</strong>/log/error.log
  CustomLog /home/<strong>username</strong>/webapps/<strong>railsapp</strong>/log/access.log combined
&lt;/VirtualHost&gt;</pre>
<p>All the things in bold should be replaced with information specific to your server. This is a typical apache configuration for a mongrel_cluster on apache2. You should configure it to suite your needs. Remember to restart apache2 after you modify httpd.conf for the changes to take place. I noticed that the httpd command was somehow still bound to the root apache2 server, so to restart the server, I had to run this:</p>
<pre>~/webapps/apache/apache2/bin/restart</pre>
<h3>mongrel_cluster</h3>
<p>One of the problems with webfaction is that we can&#8217;t specify specific port numbers that our applications run on. Port numbers are automatically assigned every time you create a new app under their control panel. Since mongrel_cluster uses sequential port numbers (ie. if you run 3 instances and the first port number is 3000, mongrel_cluster assumes the other two instances run on 3001 and 3002.) a simple modification to mongrel_cluster is required. Follow <a href="http://zownir.net/2008/03/15/mongrel_cluster-with-nonconsecutive-ports">this article</a> and you&#8217;re all set. Once you make the modification to mongrel_cluster, and create a modified mongrel_cluster config file, you can run mongrel_cluster with:</p>
<pre>mongrel_rails cluster::start</pre>
<p>as usual. Once you do that, you should be all set. You will now be serving up your rails app with mongrel_cluster under your own apache2 install.</p>
<h3>autostart.cgi</h3>
<p>You may want to create or modify your autostart.cgi file to make sure your app comes back up if your server died for some reason. My python skill was a bit rusty, but this did the trick:</p>
<pre>#!/bin/env python2.4
import os

# Test if the process is already running
running = False
ports = ['<strong>7777</strong>', '<strong>7779</strong>']
for port in ports:
  try:
      # Try to read pid file
      pid_file = '/home/<strong>username</strong>/webapps/<strong>railsapp</strong>/tmp/pids/mongrel.%(port)s.pid' % {'port': port}
      pid = open(pid_file).read()
      # Check if this process is up
      lines = os.popen('ps -p %s' % pid).readlines()
      if len(lines) &gt; 1:
          running = True
      else:
          # Delete pid file
          os.remove(pid_file)
  except IOError:
      pass

print "Content-type: text/html\r\n"
if running:
    print """&lt;html&gt;&lt;head&gt;&lt;META HTTP-EQUIV="Refresh" CONTENT="2; URL=."&gt;&lt;/head&gt;&lt;body&gt;
    Site is starting ...&lt;a href="."&gt;click here&lt;a&gt;&lt;/body&gt;&lt;/html&gt;"""
else:
    print """&lt;html&gt;&lt;head&gt;&lt;META HTTP-EQUIV="Refresh" CONTENT="2; URL=."&gt;&lt;/head&gt;&lt;body&gt;
    Restarting site ...&lt;a href="."&gt;click here&lt;a&gt;&lt;/body&gt;&lt;/html&gt;"""
    os.setpgid(os.getpid(), 0)
    os.system('/<strong>path_to</strong>/mongrel_rails cluster::start')</pre>
<p>and that should allow your rails app to restart properly. If you want to serve another rails app with mongrel_cluster, you just need to add a new virtual host to the httpd.conf file. Anyway, that&#8217;s all there is to getting mongrel_cluster running on a shared Webfaction account!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.buzamoto.com/2009/02/20/mongrel-cluster-apache2-on-webfaction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Crypted SMTP Auth with Postfix</title>
		<link>http://blog.buzamoto.com/2008/06/20/crypted-smtp-auth-with-postfix/</link>
		<comments>http://blog.buzamoto.com/2008/06/20/crypted-smtp-auth-with-postfix/#comments</comments>
		<pubDate>Sat, 21 Jun 2008 01:38:49 +0000</pubDate>
		<dc:creator>mud</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://blog.buzamoto.com/?p=8</guid>
		<description><![CDATA[
Today was a good day. Not only did I win Solitaire on my iPod on a train down to Providence, I fixed a mailserver issue that I&#8217;ve been struggling with for a few weeks.
I&#8217;m slowly switching over to Slicehost for all my web hosting requirements, and setting up the server (I&#8217;m running Ubuntu Hardy) so [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-9" title="sany0005" src="http://blog.buzamoto.com/wp-content/uploads/2008/06/sany0005.jpg" alt="" width="500" height="333" /></p>
<p>Today was a good day. Not only did I win Solitaire on my iPod on a train down to Providence, I fixed a mailserver issue that I&#8217;ve been struggling with for a few weeks.</p>
<p>I&#8217;m slowly switching over to <a href="http://slicehost.com">Slicehost</a> for all my web hosting requirements, and setting up the server (I&#8217;m running Ubuntu Hardy) so that I can get rid of all the other hosting I use (I already got rid of crappy Dreamhost). I&#8217;ve been using <a href="http://pair.com">Pair</a> for the last ten years and have been really happy with them, but I&#8217;m a server junkie. VPS is just too tasty. Anyway, most server stuff is pretty straight forward to set up. <a href="http://articles.slicehost.com/">Pickled Onion articles</a> and a bunch of <a href="http://www.howtoforge.com/">howtoforge</a> docs will get you started from a fresh disk image in no time. One thing that tripped me up was setting up the mail server. I wanted the standard PostfixAdmin controlled virtual user setup using MySQL to manage the users. Following <a href="https://help.ubuntu.com/community/PostfixCompleteVirtualMailSystemHowto">this</a> and <a href="http://www.howtoforge.com/virtual-users-domains-postfix-courier-mysql-squirrelmail-ubuntu8.04">this</a> got everything working, except SMTP would not authenticate properly when storing passwords encrypted in the database.</p>
<p><span id="more-8"></span></p>
<p>Everything worked, Courier IMAP and SMTP Auth using TLS&#8230;but only if the users passwords were stored cleartext in the database. Even if connecting securely, storing passwords cleartext is a bad idea. The problem comes from <a href="http://ubuntuforums.org/showthread.php?t=259862">Ubuntu SASL libraries not supporting password_format</a>. I didn&#8217;t want to run the patch, since&#8230;well, I don&#8217;t really know how patching while using aptitude works (you probably have to run it every time you update), and I didn&#8217;t want to start compiling some things from source, while mostly relying on a package manager. I&#8217;ve inherited servers where some things are compiled from source and other things are managed on multiple package managers, and it is a BAD IDEA.</p>
<p>So, the solution. I followed the Ubuntu article and set up my database much like their example, but instead of using auxprop for pwcheck_method, I use saslauthd with PAM.</p>
<p>First, make sure you install libpam-mysql:</p>
<p><code> sudo aptitude install libpam-mysql<br />
</code></p>
<p>Then, in /etc/postfix/sasl/smtpd.conf:</p>
<p><code> pwcheck_method: saslauthd<br />
mech_list: plain login<br />
</code></p>
<p>You might also want to add log_level: 7 while you are debugging. Next, create /etc/pam.d/smtp with the following:</p>
<p><code> auth required pam_mysql.so user=the_user passwd=the_password host=127.0.0.1 db=postfix table=mailbox usercolumn=username passwdcolumn=password crypt=1<br />
account sufficient pam_mysql.so user=the_user passwd=the_password host=127.0.0.1 db=postfix table=mailbox usercolumn=username passwdcolumn=password crypt=1<br />
</code></p>
<p>Then make sure you use $CONF['encrypt'] = &#8217;system&#8217; for PostfixAdmin. You should observe /var/log/mail.log and /var/log/auth.log for any errors. When I first tried out PAM, I didn&#8217;t realize that I had not installed libpam-mysql, so only today when I went back and set the log_level did I actually realize I needed to install it.</p>
<p>Now I feel a little better not having cleartext passwords around&#8230;</p>
<p>ps. I am really bad at Solitaire, so it was a good day.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.buzamoto.com/2008/06/20/crypted-smtp-auth-with-postfix/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
