Setting up Solr in a production environment

Yesterday I had the task of setting up Solr on a production Ubuntu 12.04 linux machine... it turned out to be a nightmare. After experimenting and benchmarking both (Thinking) Sphinx and (Sunspot) Solr for an upcoming Ruby on Rails app I have been writing, Solr seemed to be by far the best. The performance difference was fairly negligible and Solr was by far the most feature rich, also it worked nicely with Sqlite for development, whereas Sphinx required I switch everything to its own Postgres server in development.

I found a number of existing articles on this task, on other blogs around the internet, but none seemed to fulfil exactly how I have read is recommended, or best for a rails app.

  • Install Solr from scratch rather than through the distro's package manager
  • Be able to access Solr like any other daemon process (in turn I can write some capistrano tasks to ease management)

Installing Java

Since Solr runs on Java it must be installed, sadly Ubuntu doesn't package Sun's Java along with it (I heard for licensing reasons). I debated using OpenJDK but since this is production I would rather the system be as rock solid as possible and prevent any incompatibility issues.

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer

You should now have the official java version installed, which can be checked with java -version

Installing Tomcat

I decided to use the package manage for Tomcat, since verion 7 was included from Ubuntu 11.10, it is fairly up to date.

sudo apt-get install tomcat7

You can verify Tomcat is running by visiting port 8080 on your server.

Installing Solr

First I downloaded the latest Solr distribution into a temporary location, then moved it to /var/lib/solr (although this could be anywhere). After that its just some configuration… easy huh.

cd /tmp
wget http://mirror.ox.ac.uk/sites/rsync.apache.org/lucene/solr/3.6.0/apache-solr-3.6.0.tgz
tar -xf apache-solr-3.6.0.tgz
cd apache-solr-3.6.0
sudo mkdir /var/lib/solr
sudo cp -r ./* /var/lib/solr

Now we need to create a config file within Tomcat to point to the Solr binary and the solr folder in the rails app which sunspot created. This could be symlinked from the rails app to be super cool.

cd /var/lib/tomcat7/conf/Catalina/localhost/
sudo nano solr-appname.xml

solr-{appname}.xml should contain:


<?xml version="1.0" encoding="utf-8"?>
<Context docBase="/var/lib/solr/dist/apache-solr-3.6.0.war" debug="0" crossContext="true">
    <Environment name="solr/home" type="java.lang.String" value="/var/www/appname/current/solr/conf" override="true"/>
</Context>

If you restart Tomcat with sudo service tomcat7 restart and visit the /solr folder on port 8080 of your server, you should be able to view the Solr admin screen.

Turned out I had a number of issues:

Gotchas

  • Make sure that Tomcat can write to your solr configuration folder, it creates it's index there
  • The default configuration that Sunspot gave me included a value for dataDir which caused about 6 hours of head scratching.

I hope this has been useful. I was advised in my StackOverflow question that running Solr on Tomcat is better than trying to re-use the example app as some sites suggest.