How to install Apache Solr 4.6 with Apache Tomcat 7, for use with Drupal

Here's the 2014 version on how-to install Apache Solr 4.10, alongside Apache Tomcat 7, to help out with things like Drupal faceted search.

Ok... let's do this.

Install Java 1.7

yum install java-1.7.0-openjdk
# or
aptitude install java7-jdk



Done--that was quick.

Install Tomcat 7

Get the latest version of Tomcat here:


http://tomcat.apache.org/download-70.cgi

cd /usr/local/src
wget http://apache.claz.org/tomcat/tomcat-7/v7.0.61/bin/apache-tomcat-7.0.61.tar.gz

tar xvf apache-tomcat-7.0.61.tar.gz
mv apache-tomcat-7.0.61 /usr/local/tomcat

Add the user tomcat that Apache Tomcat will run under.

useradd -Mb /usr/local tomcat
chown -R tomcat:tomcat /usr/local/tomcat

Change the port commonly used port 8080 to something less conflicting like port 8983.

sed -i s/8080/8983/g /usr/local/tomcat/conf/server.xml

Test to see if user tomcat can start Apache Tomcat

sudo -u tomcat /usr/local/tomcat/bin/startup.sh

If all goes well, you shouln't see any errors.

Cool! we're done with Installing Tomcat 7.

Install Apache Solr

Get the latest version of Apache Solor here:

http://lucene.apache.org/solr/mirrors-solr-latest-redir.html

cd /usr/local/src
wget http://www.eng.lsu.edu/mirrors/apache/lucene/solr/4.10.2/solr-4.10.2.tgz
tar xvf solr-4.10.2.tgz

Install the the Apache Solr java libraries into Tomcat's library directory.

yes | cp -a solr-4.10.2/dist/solrj-lib/* /usr/local/tomcat/lib/

Install the Apache Solrl log4j.properties configuration file into Tomcat's conf directory

yes | cp -a solr-4.10.2/example/resources/log4j.properties /usr/local/tomcat/conf/

Install Apache Solr's webapp solr-*.war into Tomcat's webapps directory.

yes | cp -a solr-4.10.2/dist/solr-4.10.2.war /usr/local/tomcat/webapps/solr.war

Install the Apache Solr context file.

vi /usr/local/tomcat/conf/Catalina/localhost/solr.xml

Paste this into solr.xml:



  


Phew! we're done with installing Apache Solr

Setup Apache Solr for Drupal

Get the latest Drupal module Apache Solr Search here:

https://drupal.org/project/apachesolr

cd /usr/local/src
wget http://ftp.drupal.org/files/projects/apachesolr-7.x-1.7.tar.gz
tar xvf apachesolr-7.x-1.7.tar.gz

Setup the the Solr directory and configuration for tomcat and copy Drupal Apache Solr Search module contents to it.

mkdir -p /usr/local/tomcat/solr
yes | cp -r solr-4.10.2/example/solr/collection1/conf/ /usr/local/tomcat/solr/

rsync -av apachesolr/solr-conf/solr-4.x/ /usr/local/tomcat/solr/conf/

mkdir /usr/local/tomcat/solr/drupal 
yes | cp -r /usr/local/tomcat/solr/conf /usr/local/tomcat/solr/drupal/

Create the Apache Solr Drupal settings xml in tomcat/solr.

vi /usr/local/tomcat/solr/solr.xml

Paste the following in:



  
    
  

Alright! Let's shut tomcat down and assign the proper permissions and test this.

/usr/local/tomcat/bin/shutdown.sh	
chown -R tomcat:tomcat /usr/local/tomcat
sudo -u tomcat /usr/local/tomcat/bin/startup.sh

Let's make Apache Tomcat (along with Apache Solr) start automatically.

vi /etc/init.d/tomcat

Paste this in and save:


#!/bin/sh
#
# Startup script for Tomcat Servlet Engine
#
# chkconfig: 345 86 14
# description: Tomcat Servlet Engine
# processname: tomcat

### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short Description: Tomcat Servlet Engine
# Description: Tomcat Servlet Engine
### END INIT INFO

umask 002

NAME=tomcat

#### The following variables can be overwritten in /etc/default/$NAME
#### See /usr/local/tomcat/bin/catalina.sh for a complete list of available variables
#### and their definitions.

TOMCAT_USER=tomcat
CATALINA_HOME=/usr/local/$NAME
CATALINA_BASE=$CATALINA_HOME
CATALINA_OUT=$CATALINA_BASE/logs/catalina.out
#CATALINA_OPTS="-Dcom.sun.management.snmp.port=9983 -Dcom.sun.management.snmp.acl.file=$CATALINA_HOME/conf/snmp.acl -Dcom.sun.management.snmp.interface=0.0.0.0"
CATALINA_TMPDIR=$CATALINA_BASE/temp
JAVA_HOME=/usr
JRE_HOME=$JAVA_HOME
JAVA_OPTS="-Djava.awt.headless=true"
JAVA_ENDORSED_DIRS=$CATALINA_HOME/endorsed
CATALINA_PID=$CATALINA_HOME/bin/$NAME.pid
#LOGGING_CONFIG=
#LOGGING_MANAGER=

##### End of variables that can be overwritten in /etc/default/$NAME #####

# Overwrite settings from the default file
if [ -f "/etc/default/$NAME" ]; then
    . /etc/default/$NAME
fi

export CATALINA_HOME CATALINA_BASE CATALINA_OUT CATALINA_OPTS CATALINA_TMPDIR JAVA_HOME JRE_HOME JAVA_OPTS JAVA_ENDORSED_DIRS CATALINA_PID LOGGING_CONFIG LOGGING_MANAGER

RETVAL=0

start() {
    su -p $TOMCAT_USER -c "$CATALINA_HOME/bin/catalina.sh start"
    RETVAL=$?
}

stop() {
    su -p $TOMCAT_USER -c "$CATALINA_HOME/bin/catalina.sh stop 60 -force"
    RETVAL=$?
}

status() {
    if [ ! -z "$CATALINA_PID" ]; then
        if [ -f "$CATALINA_PID" ]; then
            echo "$NAME is running"
            RETVAL=0
            return
        fi
    fi
    echo "$NAME is not running"
    RETVAL=3
}

debug() {
    su -p $TOMCAT_USER -c "$CATALINA_HOME/bin/catalina.sh jpda start"
    RETVAL=$?
}

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

exit $RETVAL

Set the proper permission.

chmod +x /etc/init.d/tomcat

chkconfig --add tomcat
# or
update-rc.d tomcat defaults

We're DONE!

So now the Drupal's Apache Solr interface is now located here:

http://localhost:8983/solr/#/drupal

And the URL you're going to put in for Drupal's Apache Solr Search module is this:

http://localhost:8983/solr/drupal

After all this, you'll probably also want to install Drupal's Search API Solr search

https://drupal.org/project/search_api_solr

Thanks to Lullabot's "Installing Solr for use with Drupal" guide for providing the basis for this how-to.

Troubleshooting

If you get an error about not being able to load the config file /usr/local/tomcat/solr/drupal/solrconfig.xml

org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Could not load config file /usr/local/tomcat/solr/drupal/solrconfig.xml

We can use Search API Solr search module Solr configuration files instead of Drupal's Apache Solr Search module.

cd /usr/local/src	
wget http://ftp.drupal.org/files/projects/search_api_solr-7.x-1.x-dev.tar.gz
tar xvf search_api_solr-7.x-1.x-dev.tar.gz
rsync -av search_api_solr/solr-conf/4.x/ /usr/local/tomcat/solr/conf/
mkdir /usr/local/tomcat/solr/drupal
cp -r /usr/local/tomcat/solr/conf /usr/local/tomcat/solr/drupal/
/usr/local/tomcat/bin/shutdown.sh	 
chown -R tomcat:tomcat /usr/local/tomcat 
sudo -u tomcat /usr/local/tomcat/bin/startup.sh

And that should fix any Drupal Solr errors.