19th Street Design is now Greg Willis Industries!

Head on over to the new website for updated information and the new contact form. I'll keep this old website up until the summer of 2012. Make sure you update your links to the new site. Thanks!

Installing Apache Solr on Drupal 6

One of the sessions that most interested me at the Do It With Drupal thing in December 08 was Drupal Search Options led by Robert Douglas. He spoke mostly about Apache Solr and how it can be an alternative to Drupal's core search. In addition to being faster and more accurate than core search, it can perform a faceted search (like eBay, Amazon, or any number of retailer have).

A big caveat for Apach Solr in Drupal, however, is the Java requirement. Ya gotsta have Java on your server to this thing to work. In my old hosted server, I was SOL. But since I've moved to Slicehost, I can install pretty much whatever I want. So first-up on the weird-things-I-want-on-my-websites list was the Apache Solr search on my Drupal sites.

And yes I installed it and yes it works (try the search at greg-willis.com. But I didn't find any tip-to-tail installation information, so I thought I'd add my experience to help anyone looking to do what I did. The whole shebang is below the fold...

Most of the info at Drupal - Apache Solr Search Integration is solid and helpful. If I had to do it all again, I might look more into installing Tomcat.

1. Install Java 1.6
I have a 512 slice at Slicehost running Ubuntu Hardy. First thing I did was to install Java. I followed the instructions at jeff's blog - ubuntu slicehost sun java apt-get which, to abbreviate, was to install Java from a repository using sudo apt-get install sun-java6-jdk.

2. Get the PHP library
The code is found at http://code.google.com/p/solr-php-client/ and upload it to /sites/all/modules/apachesolr/SolrPhpClient.

3. Upload and Enable the ApacheSolr Module in Drupal
Not much to add, you know the module drill.

4. Install Apache Solr
I downloaded Apache Solr into a sub-directory of my home directory. You can find the latest here:http://people.apache.org/builds/lucene/solr/nightly/. I replaced the schema.xml and solrconfig.xml files in apache-solr-nightly/example/solr/conf with the ones in the ApacheSolr Drupal module (this is right from the perfectly fine directions in the module's README.txt). In the apache-solr-nightly/example directory I typed java -jar start.jar and a whole bunch of Java start-up information filled the terminal. It was running.

(The module's instructions then said to go to http://localhost:8983/solr/admin/ and test that the server was running. I didn't bother with this - mainly because I'm not running this on a local box, and with virtual hosts I didn't feel like figuring it out.)

5. Configure the Apache Solr Module
When you go to admin/settings/apachesolr, there's a message at the top which should display "Your site has contacted the Apache Solr server." Hallelujah! I left all the settings with the defaults. I enabled a taxonomy under the 'Enabled filters' tab. And under the 'Search index' tab I kept hitting cron.php until all the content was indexed.

6. Searching with Apache Solr
UPDATED: In the latest apache solr module under settings>advanced configuration, you can choose to enable Solr as the default search so you no longer have to replace the $search_box variable. You may ignore the rest of this step.

It wasn't clear to me from the documentation just how to get to the actual search box that uses Solr. You find it at /search/apachesolr_search. But I wanted my website search box, the one at the top of each page, to be the Solr search. There's probably a better way of doing this, but in my page.tpl.php I replaced this code:

<div id="search-box">
<?php print $search_box; ?>
</div>

with the following code:
<div id="search" class="container-inline">
<form class="search-form" id="search-form" method="post" accept-charset="UTF-8" action="/search/apachesolr_search">
<input type="text" class="form-text" value="" id="edit-keys-header" name="keys" maxlength="255"/>
<input id="edit-submit" class="form-submit" type="submit" value="Search" />
</form>
</div>

And that did the trick.


7. Keeping Apache Solr Running
I'd gotten it working, but what about when I reboot the server? Am I going to type in that java -jar start.jar after each reboot? Oh heck no. You have to start Apache Solr on boot. Now I'm not a Sys Admin by trade, so this was new to me. All the scripts to start up apps at boot live in /etc/init.d/. So what you need to do is place a start-up script there and then place a links to it in the directories that Ubuntu reads when starting up. So first, I need a script to start up Apache Solr. With a little digging, I found a script at rc98.net - Solr init script:

#!/bin/sh -e

# Starts, stops, and restarts solr

SOLR_DIR="path/to/apache-solr/example"
JAVA_OPTIONS="-Xmx1024m -DSTOP.PORT=8079 -DSTOP.KEY=stopkey -jar start.jar"
LOG_FILE="/var/log/solr.log"
JAVA="/usr/bin/java"

case $1 in
    start)
        echo "Starting Solr"
        cd $SOLR_DIR
        $JAVA $JAVA_OPTIONS 2> $LOG_FILE &
        ;;
    stop)
        echo "Stopping Solr"
        cd $SOLR_DIR
        $JAVA $JAVA_OPTIONS --stop
        ;;
    restart)
        $0 stop
        sleep 1
        $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}" >&2
        exit 1
        ;;
esac

Excellent. sudo nano /etc/init.d/solr and I dropped in the above script. Then I needed to link to this script from the start-up directories. I found an article on this at debuntu.org - How-To: Managing services with update-rc.d. sudo update-rc.d solr defaults and easy enough, it places links in the right places. I reboot, and my search keeps working. Lovely.

Now I'm a relative novice with the Sys Admin stuff, so if anything I wrote here causes serious issues, please let me know in the comments. Otherwise, I hope this helps.



UPDATE: 4 Nov 09 - Installing on CentOS at eApps
Most of the steps are the same, I'll just note the differences.

1. Install Java 1.6

Add the Java-SE-6 application through the eApps control panel.

7. Keeping Apache Solr Running

For starting applications at boot-up, CentOS has the chkconfig command instead of update-rc.d. Easy enough, but how do you get the solr application listed in chkconfig? Answer here: http://drupal.org/node/545368.

Tags: Drupal 6 | Search | Slicehost | Web Server

Comments

Apache Solr 6.x-1.2 Automatically Uses Default Search

In the latest apache solr module under settings>advanced configuration, you can choose to enable Solr as the default search so you no longer have to replace the $search_box variable.

Thanks

Updated the article. Thanks for the info!

Great rundown - thank you!!

You saved me a ton of time. One thing I figured out: you can display the basic keyword search field by using the module-provided function:

apachesolr_search_view($type = NULL)

Thanks

Thanks Greg. Your post was very handy to write a how-to handbook for absolute beginners at http://drupal.org/node/504558

Glad I could help! -Greg

Glad I could help! -Greg

Great tutorial. Once thing I

Great tutorial.

Once thing I noticed when going through it, after you create the /etc/init.d/solr file, you need to make it executable by doing:

sudo chmod a+rx /etc/init.d/solr

Other than that, works flawlessly. Thanks so much!

Steve

Thanks - I've updated the

Thanks - I've updated the tutorial to include this.

-Greg

Get PHP Library

Can you clear up a question I have concerning step 1. The installation instructions in the readme text instuct to get PHP library and can be found at http//code.etc.etc.etc

What is meant by this? Where is the file placed?

Or do you just place the library in the /modules/apachesolr directory and run the command svn checkout -r6 http// etc.etc.

I am not real clear on the step to take here.

Thanks,

Jerry

Oops, dropped that step on

Oops, dropped that step on the first take. It's now included. Thanks! -Greg

thanks

wish I had found this earlier... would have definitely saved me some time.. anyway using apache solr right now and a little disappointed that it doesn't work with drupal multisites.

Try with tomcat?

Handy tips, thanks. I recently setup apachesolr with Tomcat and has a few troubles before I got it working. I documented the setup on my site (http://jamonation.com/node/856) so if you want to have a go with tomcat or have any tips for things that I left out, let me know.