Mikkel Høgh

Coding the web since 1999

23 Jul 2011

How to install multicore Apache Solr on FreeBSD with Jetty

If you use Apache Solr with your Drupal site, you have probably come across the need to have more than one Solr instance. You may have multiple sites, or just multiple copies of the same site, production and staging perhaps?

There are two widely published ways to accomplish that. One is to set up completely separate Solr instances with whatever Java-server you’re using. That is somewhat inefficient, and I was unable to get such a setup working properly anyways. So here’s the alternative, “multi core”.

The benefit of using multi core is that you avoid most of the configuration overhead associated with figuring out how to get multiple WebAppContainerDeploymentContextFactoryGeneratorWidgetClass instances (or whatever they’re named in your brand of Java-server) to coexist by using the same WAR-file but not the same configuration. I spent a lot of hours trying to accomplish just that.

Instead, all the interesting stuff happens in the Solr configuration, which is a lot less confusing for a Java novice like me.

Instructions

  1. Install a Java JRE.

    You may want to see my explanation on how to do this.

  2. Install Jetty and Solr.

    If you use portmaster, this can be as simple as running portmaster www/jetty textproc/apache-solr

  3. Create a folder for your Solr multi core instance’s configuration and data files. This could be anywhere, but in this example, I’m going to use /srv/solr.

  4. Create a /srv/solr/solr.xml file for the configuration, setting up the different cores into folders.

    Mine looks like this:

     <?xml version="1.0" encoding="UTF-8" ?>
     <!--
     All (relative) paths are relative to the installation path
    
       persistent: Save changes made via the API to this file
       sharedLib: path to a lib directory that will be shared across all cores
     -->
     <solr persistent="false">
    
       <!--
       adminPath: RequestHandler path to manage cores.
         If 'null' (or absent), cores will not be manageable via request handler
       -->
       <cores adminPath="/admin/cores" sharedLib="lib">
         <core name="dev" instanceDir="dev" />
         <core name="prod" instanceDir="prod" />
         <core name="stg" instanceDir="stg" />
       </cores>
     </solr>
    
  1. Create all the folders referenced in the config file.

    We specified four folders, so lets create them via a simple mkdir dev lib prod stg (while standing in the /srv/solr folder).

  2. Make these folders owned by the user that will run our Solr instances. In this example, I’ll use the www account, but it would be more secure to set up a separate account for running Solr if you have other web servers running on the same machine.

    chown www:www dev lib prod stg

  3. For each core you want to set up, copy or symlink the schema and other configuration files you need into the conf folder in each core folder. In this example, I’m copying the example configuration from /usr/local/share/examples/apache-solr/solr/conf/. If you’re working with Drupal, be sure to copy the solr configuration it ships with into each core.

    mkdir prod/conf
    cd prod/conf
    cp -r /usr/local/share/examples/apache-solr/solr/conf/* ./
    cd ../.. cp -r prod/conf dev/ cp -r prod/conf stg/

  4. Enable Jetty

    In this example, we’re going to use Jetty to run the Solr service. I am not well versed in the Java lingo for this, but Jetty is a servlet container, so I guess that means Solr is being run as a servlet. I also tried this with Tomcat, but that was a lot harder to configure properly.

    Add jetty_enable="YES" on a new line in /etc/rc.conf.

  5. Copy /usr/local/jetty/etc/jetty.xml to /usr/local/etc.

  6. Symlink solr.war into /usr/local/jetty/webapps

cd /usr/local/jetty/webapps
ln -s /usr/local/share/java/classes/apache-solr-3.2.0.war solr.war

  1. Symlink /srv/solr into /usr/local/jetty

cd /usr/local/jetty ln -s /srv/solr

  1. Start Jetty by running service jetty start

Hopefully, after all this work, Solr should be ready once its done booting up.

You can check that it’s working by running curl -iL localhost:8080/solr/prod/admin/. This should output the HTML for the admin interface.

If you have problems, try running tail -f /usr/local/jetty/jetty.log in one terminal, and then service jetty restart in another, and look what goes on as Jetty restart (there’ll be a lot of messages flying by, but the error should be in there somewhere).