Last updated March 20, 2010. Created on February 18, 2010.
Edited by mtwesley, larsdesigns, pkiraly. Log in to edit this page.

Running Solr in special way

If you have the great Apache Solr Search module installed, and you would like to use our XC Drupal Toolkit module, you should set up a second Solr instance. This is not a hard task, but we should learn the meaning and usage of three necessary parameters. In this section we are using the Jetty servlet container, which comes with the Solr package. These parameters are relevant in other cases, but the way you should setup could be different.

To run Solr on different port than the standard one (which is 8983) use the jetty.port parameter, for example:

-Djetty.port=8984

If you want Solr to use a different index, than the standard port (which is solr/data) use the solr.data.dir parameter, for example:

-Dsolr.data.dir=./solr/data4xc_records

If you want Solr to use different configurations, than the standard one (which take place in solr/conf directory) use the solr.solr.home parameter, but make sure that you have the configuration directory inside the new Solr home directory. For example:

-Dsolr.solr.home=/users/xc_user/solr

With this parameters you can run multiple Solr instances in parallel. Example:

java start.jar
java -Dsolr.data.dir=./solr/data4xc_records -Djetty.port=8984 -Dsolr.solr.home=/users/xc_user/solr start.jar

With this way you can use the command line to run Solr instances, but there are other more robust ways to accomplish this by modifying Jetty's configuration files. For more information about the topic, please consult the Solr wiki: http://wiki.apache.org/solr/SolrJetty, or in the case you use a different servlet container: http://wiki.apache.org/solr/SolrInstall.

To help ourselves in starting/stopping Solr, we will use scripts to handle this.

solr.sh:

#!/bin/sh
#
# chkconfig: - 80 45
# description: Starts and stops Solr

start() {
        echo -n "Starting Solr... "
        nohup /usr/local/apache-solr-1.4.0/example/solr.start
        echo "OK"
        return 0
}

stop() {
        echo -n "Stopping Solr... "
        /usr/local/apache-solr-1.4.0/example/solr.stop
        echo "OK"
        return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?

solr.start:

#!/bin/sh
java -DSTOP.PORT=8079 -DSTOP.KEY=ftasolrstop -Xms512M -Xmx1024M -jar start.jar &

solr.stop:

#!/bin/sh
java -DSTOP.PORT=8079 -DSTOP.KEY=ftasolrstop -jar start.jar --stop

The source of the scripts is Peter Manis' blog.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

xatoo_’s picture

I find it usefull to generate a .pid file so that my monitoring service can check wether solr is running. Besides I don't like the idea of opening a port just to be able to shut down the service. Therefore I've created a different init file that uses start-stop-daemon. This one should work on debian/ubuntu, I don't know about other distros.

#! /bin/sh

### BEGIN INIT INFO
# Provides:          solr
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts Apache solr search
# Description:       Solr search platform
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SOLR_DIR="/opt/apache-solr/drupal_multisite" # Change this to your needs
DESC="Apache solr search"
NAME=solr
PID_FILE=/var/run/$NAME.pid
LOG_FILE=/var/log/$NAME.log
DAEMON=`which java`
DAEMON_ARGS="-Xmx1024m -jar start.jar"
SCRIPTNAME=/etc/init.d/$NAME
SOLR_USER=root
SOLR_GROUP=root

# Include status check functions
. /lib/lsb/init-functions

# Exit if the package is not installed
if [ ! -x "$DAEMON" ]; then
{
        echo "Couldn't find $DAEMON"
        exit 99
}
fi

#
# Function that starts the daemon/service
#
do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started
        start-stop-daemon --start --quiet --chuid $SOLR_USER:$SOLR_GROUP --exec $DAEMON --test > /dev/null \
                || return 1
        start-stop-daemon --start --chuid $SOLR_USER:$SOLR_GROUP --background --pidfile $PID_FILE --make-pidfile \
                -d $SOLR_DIR --startas $DAEMON -- $DAEMON_ARGS >> $LOG_FILE \
                || return 2

}

#
# Function that stops the daemon/service
#
do_stop()
{
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred
        start-stop-daemon --stop --signal 2 --retry 5 --pidfile $PID_FILE
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2
        return "$RETVAL"
}

case "$1" in
  start)
        log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) log_end_msg 0 ;;
                2) log_end_msg 1 ;;
        esac
        ;;
  stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) log_end_msg 0 ;;
                2) log_end_msg 1 ;;
        esac
        ;;
restart|force-reload)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  status)
        status_of_proc -p $PID_FILE $DAEMON $NAME && exit 0 || exit $?
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&2
        exit 3
        ;;
esac

exit 0