Skip to content

Instantly share code, notes, and snippets.

@macmladen
Last active September 25, 2015 18:22
Show Gist options
  • Save macmladen/98a47cbd9c8803225843 to your computer and use it in GitHub Desktop.
Save macmladen/98a47cbd9c8803225843 to your computer and use it in GitHub Desktop.
Installing Apache Solr search engine on server and integrating with Drupal

Apache Solr and Drupal integration

Solr compatibility chart

All versions download available at: https://archive.apache.org/dist/lucene/solr/

Java available at: http://java.com/en/download/index.jsp

Current build (26.09.2014)

Solr 4.10 - requires Java 7, compatible with Java 8

Java 7

Required from: Solr 4.8.0

All previous versions 1.x, 2.x, 3.x seem to work with Java 6 (JRE 1.6)

Java 6

Last: Solr 4.7.2

Mac OS X 10.6

Java 1.6.0_65

Mac OS X 10.7-10.10

If it is upgrade from prevoius OS X version, then you inherited Java 6 from latest update in 10.6.

Otherwise you have to install manually Java from Oracle, 6, 7 or 8. Choose the version according to your needs, but there is no reason not to go with latest one.

Drupal search_api_solr

  1. Check your Java Runtime Envoronment version with java -version
  2. Download appropriate solr version
  3. unpack and move somewhere to /opt or /usr (it may live with tomcat container and own user at home)
  4. once moved, go into $(SOLR_dir)/examples and execute cp -a solr mysolr or use some name on your own instead of mysolr
  5. copy schema files from module search_api_solr from appropriate version e.g solr-conf/4.x/* over the same named files in mysolr/collection1/conf/. Make sure they are placed over, check by comparing size and date.
  6. You need also connector library SolrPhpClient
  7. Now you need to configure search_api, add Solr server, add index and configure fields for index.

SolrPhpClient

This is very important link between any PHP and Solr. There is a change in Solr 4.x that throws error:

solrexception: unknown commit parameter 'waitflush'

get the latest version with

$ svn export http://solr-php-client.googlecode.com/svn/trunk/ SolrPhpClient

If you are looking for particular version of the library, add -r22 after export

Running solr

run as a terminal process

You may start solr each time you need it in terminal by issuing command that starts java with arguments pointing to your configuration (you may alias it or make shell command).

This way is useful if you need solr temporarily. It will run until stopped by CTRL + C or by closing terminal window.

shell alias, change directory paths to suit your setup

$ alias solr="cd /opt/solr-4.7.2/example/; java -server $JAVA_OPTS -Dsolr.solr.home=mysolr -jar start.jar

run solr as shell command

You should save as solr in path to your local commands, for example /usr/local/bin

Also, if you supply argument, you may start different solr profiles in example/ like $ solr other-solr

Still, it is active process that is terminated by CTRL + C or by closing terminal window.

#!/bin/sh
if [ -z "$1" ]; then
  PROFILE="mysolr"   # enabling some other profile as well, otherwise use default mysolr
else
  PROFILE=$1
fi
cd /opt/solr-4.7.2/example/ && java -server $JAVA_OPTS -Dsolr.solr.home=$PROFILE -jar start.jar

running in background or detached from terminal process

You can run the process in background leaving terminal for some other things. However, it will still be terminated by closing terminal window and will output solr response to the window.

You run process in background by appending ampersand at the end of command, $ solr & or direct $ java -jar start.jar &. This is not actually useful.

Better approach is to go detached. Start solr and detach process from terminal.

  1. Start the process by alias, command or direct, $ solr
  2. Pause the process with CTRL + Z or you may start it in background as $ solr &
  3. Start it running in background $ bg
  4. Release it from terminal process with $ disown That will release the last job from current terminal.
  5. Use $ disown -r to release all running background processes
  6. Use $ disown -a to release all background processes
  7. Use $ disown -h to keep background process in jobs list but also keep it on terminal process closing
  8. Use $ disown -h %1 same as previous but uses number to identify process as listed with jobs
  9. Use $ jobs to see all background running processes
  10. Use $ jobs -l to see job number and PID too

Briefly, just use this commands

$ solr mysolr &
$ disown

running another solr

Use different port like -Djetty.port=8980

java -server $JAVA_OPTS -Dsolr.solr.home=other-solr -Djetty.port=8980 -jar start.jar

init.d

This enables running, stopping and restarting as service and starting upon reboot

Copy this into file /etc/init.d/solr

#!/bin/sh

# Starts, stops, and restarts Apache Solr.
#
# chkconfig: 35 92 08
# description: Starts and stops Apache Solr

SOLR_DIR="/var/solr-4.10.3/example"
JAVA_OPTIONS="-Xmx1024m -DSTOP.PORT=8079 -DSTOP.KEY=mustard -Dsolr.solr.home=mysolr -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

Edit the line starting with SOLR_DIR to make sure it's pointing to the location where your solr installation lives.

Make the script executable:

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

Now install the script using chkconfig:

$ sudo chkconfig --add solr

This will add the script to runlevels 3 and 5, so solr will start once the system is up. Check to make sure that things are properly set up by typing

$ chkconfig --list

You should see solr among the various services. You should also be able to use the service command to stop and start solr (you actual system may call it services or some other — this starts daemons from /etc/init.d)

$ sudo service solr stop
Stopping Solr
$ sudo service solr start
Starting Solr
$ sudo service solr restart
Stopping Solr
Starting Solr

Firewall Exception

Solr shouldn't be accessible over internet but you may need that briefly in order to debug, check or setup so you need to open Firewall for your own IP but do not forget to close it afterwards! You may also use temporary allow access.

If you use iptables add a rule to allow access to Solr's admin section and query Solr data (replace 0.0.0.0 with the correct IP address):

$ iptables -A INPUT -s 0.0.0.0 -p tcp -m tcp --dport 8983 -j ACCEPT 
$ service iptables save

Or, if you want to allow port 8983 for all requests use:

$ iptables -A INPUT -p tcp -m tcp --dport 8983 -j ACCEPT 
$ service iptables save

Also, if you're using a MySQL database connection for data importer you'll want to open a firewall exception for the localhost MySQL port:

$ iptables -A INPUT -s 127.0.0.1 -p tcp -m tcp --dport 3306 -j ACCEPT 
$ service iptables save 
$ iptables -L 
... 
ACCEPT tcp -- localhost anywhere tcp dpt:mysql 
...

Running Solr Admin

Your Solr is accessible on port 8983 or othewise configured and on link /solr like:

http://mydomain.com:8983/solr

where you can see basic settings in dashboard and log status in logging which is crucial to hunting down any problems.

When you wish to see Solr index you select (default is) collection1 from Core selector (bellow Thread dump) where you may run default query that will give you rough picture what is inside Solr index(es).

References

Very useful overview full of information.

By Shay Anderson on October 2013

Last updated October 5, 2010. Created on August 10, 2009. Edited by rfay, phd_hiren_g, Francewhoa, jvandyk all @ drupal.org

frjo – 1 June, 2009 Update 2010-01-05

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment