Deploying Thinking Sphinx
This is how I like to deploy Thinking Sphinx. In summary:
- Install Sphinx on the server.
- Decide where you want Sphinx’s PID file and indexes in production.
- Ignore Sphinx’s configuration and indexes in development.
- Configure Capistrano to work with Thinking Sphinx.
- Set up cron on the server to re-index your data regularly.
Instructions
1. Install Sphinx on the server.
On server:
$ curl -O http://www.sphinxsearch.com/downloads/sphinx-0.9.8.1.tar.gz
$ gzip -d sphinx-0.9.8.1.tar.gz
$ tar xvf sphinx-0.9.8.1.tar
$ cd sphinx-0.9.8.1
$ ./configure
$ make
$ sudo make install
And to make sure it installed correctly:
$ search
Sphinx 0.9.8.1-release (r1533)
Copyright (c) 2001-2008, Andrew Aksyonoff
Usage: search [OPTIONS]
...
2. Decide where you want Sphinx’s PID file and indexes in production.
I like all my PID files in /var/run/<process>. We also want to preserve Sphinx’s indexes across deployments.
In your app create config/sphinx.yml:
production:
pid_file: /var/run/sphinx/searchd.pid
searchd_files: /path/to/your/app/shared/db/sphinx
On server:
$ sudo mkdir /var/run/sphinx
$ sudo chown deploy:deploy /var/run/sphinx
$ mkdir -p /path/to/your/app/shared/db/sphinx
Adjust the ownership to suit your needs.
3. Ignore Sphinx’s configuration and indexes in development.
This isn’t really a deployment step but it needs to be done.
Add to .gitignore:
config/development/sphinx.conf
db/sphinx/*
4. Configure Capistrano to work with Thinking Sphinx.
Add this to your config/deploy.rb:
# Thinking Sphinx
namespace :thinking_sphinx do
task :configure, :roles => [:app] do
run "cd #{release_path}; rake thinking_sphinx:configure RAILS_ENV=#{rails_env}"
end
task :index, :roles => [:app] do
run "cd #{release_path}; rake thinking_sphinx:index RAILS_ENV=#{rails_env}"
end
task :start, :roles => [:app] do
run "cd #{release_path}; rake thinking_sphinx:start RAILS_ENV=#{rails_env}"
end
task :stop, :roles => [:app] do
run "cd #{current_path}; rake thinking_sphinx:stop RAILS_ENV=#{rails_env}"
end
task :restart, :roles => [:app] do
run "cd #{release_path}; rake thinking_sphinx:restart RAILS_ENV=#{rails_env}"
end
task :rebuild, :roles => [:app] do
run "cd #{release_path}; rake thinking_sphinx:rebuild RAILS_ENV=#{rails_env}"
end
end
# Thinking Sphinx typing shortcuts
namespace :ts do
task :conf, :roles => [:app] do
run "cd #{release_path}; rake thinking_sphinx:configure RAILS_ENV=#{rails_env}"
end
task :in, :roles => [:app] do
run "cd #{release_path}; rake thinking_sphinx:index RAILS_ENV=#{rails_env}"
end
task :start, :roles => [:app] do
run "cd #{release_path}; rake thinking_sphinx:start RAILS_ENV=#{rails_env}"
end
task :stop, :roles => [:app] do
run "cd #{current_path}; rake thinking_sphinx:stop RAILS_ENV=#{rails_env}"
end
task :restart, :roles => [:app] do
run "cd #{release_path}; rake thinking_sphinx:restart RAILS_ENV=#{rails_env}"
end
task :rebuild, :roles => [:app] do
run "cd #{release_path}; rake thinking_sphinx:rebuild RAILS_ENV=#{rails_env}"
end
end
# http://github.com/jamis/capistrano/blob/master/lib/capistrano/recipes/deploy.rb
# :default -> update, restart
# :update -> update_code, symlink
namespace :deploy do
task :before_update_code do
# Stop Thinking Sphinx before the update so it finds its configuration file.
thinking_sphinx.stop
end
task :after_update_code do
symlink_sphinx_indexes
thinking_sphinx.configure
thinking_sphinx.start
end
desc "Link up Sphinx's indexes."
task :symlink_sphinx_indexes, :roles => [:app] do
run "ln -nfs #{shared_path}/db/sphinx #{current_path}/db/sphinx"
end
end
5. Set up cron on the server to re-index your data regularly.
Edit your cron table (crontab -e) and add something along the lines of:
0 * * * * cd /path/to/your/app/current && /usr/local/bin/rake RAILS_ENV=production thinking_sphinx:index >> /path/to/your/app/current/log/cron.log 2>&1
Posted in Databases, Deployment, Rails

6 Comments
Jump to comment form