<?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>David Lowry &#187; railscasts</title>
	<atom:link href="http://www.davidlowry.co.uk/tag/railscasts/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davidlowry.co.uk</link>
	<description></description>
	<lastBuildDate>Sat, 05 Nov 2011 14:35:44 +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>Simple Thinking Sphinx on Dreamhost</title>
		<link>http://www.davidlowry.co.uk/279/simple-thinking-sphinx-on-dreamhost/</link>
		<comments>http://www.davidlowry.co.uk/279/simple-thinking-sphinx-on-dreamhost/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 13:10:16 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[learning]]></category>
		<category><![CDATA[meaningful labor]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[dreamhost]]></category>
		<category><![CDATA[railscasts]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[sphinx]]></category>
		<category><![CDATA[thinking sphinx]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://davidlowry.co.uk/?p=279</guid>
		<description><![CDATA[*** Please note &#8211; this will probably not work (at all) (for more than a day of light use) without Cron use. And isn&#8217;t at all authorised by Dreamhost!! *** For a recent client project I&#8217;ve used a Dreamhost unlimited account, which for value compared with the resources available and the fact that you don&#8217;t...<a href="http://www.davidlowry.co.uk/279/simple-thinking-sphinx-on-dreamhost/">&#187;</a>]]></description>
			<content:encoded><![CDATA[<p>*** Please note &#8211; this will probably not work (at all) (for more than a day of light use) without Cron use. And isn&#8217;t at all authorised by Dreamhost!! *** </p>
<p>For a recent client project I&#8217;ve used a Dreamhost unlimited account, which for value compared with the resources available and the fact that you don&#8217;t have to do any building or setting up of the server environment makes it an easy win for a site that&#8217;s not going to have a huge amount of traffic or a large amount of processing.</p>
<p>Post-launch I got to work putting together a basic search engine and here&#8217;s a quick run through of the steps it took to get a very simple Sphinx instance working on Dreamhost, and a few hurdles thrown in the way by various googled articles.</p>
<h3>Development Environment</h3>
<p>Using the <a href="http://freelancing-gods.com/posts/a_concise_guide_to_using_thinking_sphinx">guide from FG</a> install Sphinx locally:</p>
<pre><code>curl -O http://sphinxsearch.com/downloads/sphinx-0.9.8-rc2.tar.gz
tar zxvf sphinx-0.9.8-rc2.tar.gz
cd sphinx-0.9.8-rc2
./configure
make
sudo make install</code></pre>
<p>then install the TS plugin into your application</p>
<pre><code>script/plugin install git://github.com/freelancing-god/thinking-sphinx.git
</code></pre>
<p>Any problems with that, check out the <a href="http://freelancing-gods.com/posts/a_concise_guide_to_using_thinking_sphinx">FG page</a> linked.</p>
<h3>Getting a basic search going</h3>
<p>Following tutorials such as the <a href="http://railscasts.com/episodes/120-thinking-sphinx">Sphinx Railscast</a> will get you there pretty quick.</p>
<p>In your searchable model you need to define an index</p>
<pre><code>
class Page < ActiveRecord::Base
  define_index do
    indexes :title
    indexes :long
    indexes :short
  end ...</code></pre>
<p>Run the indexer and start the Sphinx instance:</p>
<pre><code>
rake thinking_sphinx:index
rake thinking_sphinx:start</code></pre>
<p>After this you'll be able to search on your object. So using script/console</p>
<pre><code>@searched_pages = Page.search("query")</code></pre>
<p>will return what you're looking for!</p>
<h3>Setting up Dreamhost</h3>
<p>First things first you need to install Sphinx in your local area, as posted <a href="http://hughevans.net/2009/03/10/thinking-sphinx-dreamhost">by Hugh Evans</a>:</p>
<pre><code>cd ~/
mkdir -p local
wget http://sphinxsearch.com/downloads/sphinx-0.9.8.1.tar.gz
tar -xzf sphinx-0.9.8.1.tar.gz
cd sphinx-0.9.8.1/
./configure --prefix=$HOME/local/ --exec-prefix=$HOME/local/
make
make install</code></pre>
<p>then set up the PATHs</p>
<pre><code>echo "export PATH="$PATH:~/local/bin"" >> ~/.bash_profile
source ~/.bash_profile</code></pre>
<p>You can choose to set up a CRON task at this point too, but I'm not going into that.</p>
<p>Also at this point in the there's talk of using Sphinx being anti TOS in DH's eyes... but we'll see does the process get killed or not!</p>
<h3>Configuring Sphinx for DH</h3>
<p>Create a file called sphinx.yml in the RAILS_ROOT/config/ folder.</p>
<p>Because Dreamhost uses an externally referenced MySQL server instead of localhost you need to set up the sql_* parameters:</p>
<pre><code>
  sql_host: "mysql.YOURDOMAIN"
  sql_port: 3306
  sql_user: "USER"
  sql_password: "PASSWORD"
  sql_database: "DATABASE"
</code></pre>
<p>And because you installed Sphinx in your local area:</p>
<pre><code>
  bin_path: '/home/YOURUSERNAME/local/bin'
</code></pre>
<p>Finally, after setting whatever memory/fine tuning settings you wish/require set up the locations for the Sphinx files:</p>
<pre><code>
  config_file: "/home/YOURUSERNAME/DOMAIN.co.uk/shared/production.sphinx.conf"
  searchd_log_file: "/home/YOURUSERNAME/DOMAIN.co.uk/shared/log/searchd.log"
  query_log_file: "/home/YOURUSERNAME/DOMAIN.co.uk/shared/log/searchd.query.log"
  pid_file: "/home/YOURUSERNAME/DOMAIN.co.uk/shared/log/searchd.production.pid"
  searchd_file_path: "/home/YOURUSERNAME/DOMAIN.co.uk/shared/db/sphinx"
</code></pre>
<p>That should be you ready to start deploying.</p>
<h3>Deploying</h3>
<p>Using Git + Capistrano for deployment (and Passenger for the http server) my deploy.rb's namespace area looks like this:</p>
<pre><code>
namespace :deploy do
  task :restart do
    after_symlink
    restart_sphinx
    run "touch #{deploy_to}/current/tmp/restart.txt"
  end

  task :start do
    # nothing  (this avoids the 'spin' script issue)
  end

  desc "Re-establish symlinks"
  task :after_symlink do
    run <<-CMD
      rm -fr #{release_path}/db/sphinx &#038;&#038;
      ln -nfs #{shared_path}/db/sphinx #{release_path}/db/sphinx
    CMD
  end

  desc "Stop the sphinx server"
  task :stop_sphinx , :roles => :app do
    run "cd #{current_path} &#038;&#038; rake thinking_sphinx:stop RAILS_ENV=production"
  end

  desc "Start the sphinx server"
  task :start_sphinx, :roles => :app do
    run "cd #{current_path} &#038;&#038; rake thinking_sphinx:configure RAILS_ENV=production &#038;&#038; rake thinking_sphinx:index RAILS_ENV=production &#038;&#038; rake thinking_sphinx:start RAILS_ENV=production"
  end

  desc "Restart the sphinx server"
  task :restart_sphinx, :roles => :app do
    stop_sphinx
    start_sphinx
  end

end
</code></pre>
<p>There's probably a neater way to do this, but basically this makes sure Sphinx's indexes and conf files live in the shared deployment folder.</p>
<p>I recommend you try all this in a staging area first, obviously... and you can use Dreamhost's control panel to set up a staging subdomain with a new database in whatever fashion you prefer.</p>
<p>Any problems with this script flag them up, please! This is as much for my future reference as you googlies out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidlowry.co.uk/279/simple-thinking-sphinx-on-dreamhost/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

