Configuring Apache Solr Multi-core With Drupal and Tomcat on Ubuntu 9.10

One of the things I've had to do recently is set up a development server that has multiple sites with Apache Solr so that each site has its own index in Solr.  After searching the internets for information on how to do this, I found two sources that contained part of the process, but not put together in a way that worked for me. 

    Nick Veenhof's blog post on setting up Solr on Ubuntu 9.10
    The "Apache Solr Multi-core Setup" handbook page on drupal.org (adapted from a post by Chris Rikli)

Nick's post got me through the Tomcat setup with Solr for one site, but his step for multi-site involved making copies of the solr.war file. I noticed Peter Wolanin's comment that it would be better to use the solr multi core feature to serve the 2 indexes at different paths, so I decided to try and implement it that way.  I found the handbook page, but it was not written for Tomcat on Ubuntu, so I had to combine them.  And then of course, I knew I had to blog about it.

All of these steps (except for a couple minor modifications) are copied from the two sources listed above.  I am doing this on Ubuntu 9.10, the same as Nick.

So here we go.  Please feel free to raise your hand if you have any questions.
 

Step 1. Installing Tomcat

sudo apt-get update<br />
        sudo apt-get upgrade<br />
        sudo apt-get install tomcat6 tomcat6-admin tomcat6-common tomcat6-user tomcat6-docs tomcat6-examples

This command downloads and install the following packages

tomcat6 : Servlet and JSP engine<br />
        tomcat6-admin : Admin web applications<br />
        tomcat6-common : Common files<br />
        tomcat6-user : Tools to create user instances<br />
        tomcat6-docs : Example web applications<br />
        tomcat6-examples : Example web applications

Start tomcat by typing

/etc/init.d/tomcat6 start

Step 2. Security

If you are using IP tables and installing Apache Solr on an external server, modify or add the following line to accept the port 8080:

-A INPUT -p tcp -m tcp --dport 8080 -j ACCEPT

After installation go to http://localhost:8080 or http://serverip:8080 in your browser. Now you should see the Tomcat welcome page.

PS: As noticed by Robert Douglass, be careful when opening ports in public servers. Apache Solr Does NOT provide security by default so anyone is able to post data towards your system. Best thing to do is to add another rule where you only accept 8080 connections from a certain IP address.

Step 3. Downloading Solr

Download apachesolr to your home directory for temporary reasons.  Get it from one of the mirrors at http://www.apache.org/dyn/closer.cgi/lucene/solr/.  To use the recommended mirror:

cd ~/<br />
        wget &lt;a href=&quot;http://www.motorlogy.com/apachemirror/lucene/solr/1.4.0/apache-solr-1.4.0.tgz<br />
        tar&quot; title=&quot;http://www.motorlogy.com/apachemirror/lucene/solr/1.4.0/apache-solr-1.4.0.tgz<br />
        tar&quot;&gt;http://www.motorlogy.com/apachemirror/lucene/solr/1.4.0/apache-solr-1.4....&lt;/a&gt; -zxvf apache-solr-1.4.0.tgz

 

Step 4. Linking Tomcat6 with Apache Solr application

This should give you an idea on where your distribution installed tomcat6.
Attention: If your path is different, do not forget to also adjust this in the next steps.  whereis tomcat6 should show you tomcat6:

/etc/tomcat6 /usr/share/tomcat6

Copy the solr.war file to the webapps directory:

sudo cp apache-solr-1.4.0/dist/apache-solr-1.4.0.war /usr/share/tomcat6/webapps/solr.war

Copy the example solr application to a new directory called solr. We will change this example solr application later on to be viable for Drupal 6:

sudo cp -R apache-solr-1.4.0/example/solr/ /usr/share/tomcat6/solr/

Create our config file

sudo nano /etc/tomcat6/Catalina/localhost/solr.xml

And fill it with the following configuration :

&lt;Context docBase=&quot;/usr/share/tomcat6/webapps/solr.war&quot; debug=&quot;0&quot; privileged=&quot;true&quot; allowLinking=&quot;true&quot; crossContext=&quot;true&quot;&gt;<br />
        &lt;Environment name=&quot;solr/home&quot; type=&quot;java.lang.String&quot; value=&quot;/usr/share/tomcat6/solr&quot; override=&quot;true&quot; /&gt;<br />
        &lt;/Context&gt;

Step 5. Managing Tomcat6 application

We want to be able to see how and/or if our Solr application is running, and we can do this by using the manager application. By default you don't have access to this application so we have to modify the permissions:

sudo nano /etc/tomcat6/tomcat-users.xml

And modify it so it more or less reflects the same information as shown here.

&lt;tomcat-users&gt;<br />
        &lt;role rolename=&quot;admin&quot;/&gt;<br />
        &lt;role rolename=&quot;manager&quot;/&gt;<br />
        &lt;user username=&quot;nick&quot; password=&quot;ateneatech&quot; roles=&quot;admin,manager&quot;/&gt;<br />
        &lt;/tomcat-users&gt;

Drop Tomcat security so Solr can access /usr/share/tomcat6/solr:

sudo nano /etc/default/tomcat6

And modify it so our security is disabled. Be careful if you are running on a server which you do not control 100%!

TOMCAT6_SECURITY=no

We'll restart Tomcat after we got Solr multi-core installed.
 

 

Step 6. Linking Drupal 6 with a running Apache Solr

 

I assume you have Drush installed, so we continue with downloading the apachesolr module. Execute this command in the designated website:

drush dl apachesolr

You will also need to get the Solr PHP Client library (available at http://code.google.com/p/solr-php-client/downloads/list) and unpack it so it is under the apachesolr module directory as SolrPhpCLient (i.e. sites/all/modules/apachesolr/SolrPHPClient)

Let's copy our schema files that will customize our Apache Solr instance so it fits the "Drupal" bill.

sudo cp ~/apachesolr/schema.xml /usr/share/tomcat6/solr/conf/schema.xml<br />
        sudo cp ~/apachesolr/solrconfig.xml /usr/share/tomcat6/solr/conf/solrconfig.xml

 

Step 7. Set up up the multi-core functionality

First, we copy the solr.xml that is configured for mutli-core to the /solr directory:

sudo cp ~/apache-solr-1.4.0/example/multicore/solr.xml /usr/share/tomcat6/solr/solr.xml

Now create a directory within the /usr/share/tomcat6/solr directory for each site you want to use Solr with.  For example, if I had two sites "site1.com" and "site2.com" and I wanted to use Solr with them both, I might create the following directories:

/usr/share/tomcat6/solr/site1<br />
        /usr/share/tomcat6/solr/site2

Copy the /usr/share/tomcat6/solr/conf directory into each directory you just created. When you're done, each site's directory should have a copy of /conf in them:

sudo cp /usr/share/tomcat6/solr/conf /usr/share/tomcat6/solr/site1/conf<br />
        sudo cp /usr/share/tomcat6/solr/conf /usr/share/tomcat6/solr/site2/conf

Note: DO NOT delete the /usr/share/tomcat6/solr/conf directory.  Even though there is a copy for each site, it is still needed by the Solr core.  For stock Solr, this directory can be deleted, but it is needed when running it in Drupal.

Now alter the solr.xml file you copied over to list both sites.  Your solr.xml should look like this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;<br />
        &lt;solr persistent=&quot;false&quot;&gt;<br />
        &lt;cores adminPath=&quot;/admin/cores&quot;&gt;<br />
        &nbsp; &lt;core name=&quot;site1&quot; instanceDir=&quot;site1&quot; /&gt;<br />
        &nbsp; &lt;core name=&quot;site2&quot; instanceDir=&quot;site2&quot; /&gt;<br />
        &lt;/cores&gt;<br />
        &lt;/solr&gt;

OK, let's restart our Tomcat service:

sudo /etc/init.d/tomcat6 restart

Surf to http://localhost:8080/manager/html and log in with your username and password from above to check if the Solr instance is started. If not, start and it and check wether or not you receive an error code.  If your application is started, surf to http://localhost:8080/solr/admin and you should see a nice screen!
 

Step 8. Configure the Drupal modules

Enable the "Apache Solr framework" and "Apache Solr search" modules. Also, enable the core Drupal Search module if you haven't already. Do this for both sites.

Navigate to the Solr Settings page for each site (admin/settings/apachesolr/settings):

- Solr host name: localhost<br />
        - Solr port: 8080<br />
        - Solr path for site1.com: /solr/site1<br />
        - Solr path for site2.com: /solr/site2

Under "Advanced Configuration", enabled the "Make Apache Solr Search the default" option if you want to replace the default search with Solr (and I assume you do).

Click "Save Configuration."  The first time around it'll probably tell you it can't reach the server, but if you refresh the page you'll be good to go.

Now, whenever you add a new site, you will just need to do the following steps to enable Solr search:

    Copy /usr/share/tomcat6/solr/conf to /usr/share/tomcat6/solr/mysite
    Add the site to /usr/share/tomcat6/solr/solr.xml (you will need to reload Solr to see the new core)
    Configure Solr settings in Drupal at admin/settings/apachesolr

And that's it.  Happy searching!

Share This