How to install Apache Solr 3.x for Drupal 7 or Drupal 6

First we'll need to install Java 1.6 JDK, and be on PHP 5.2 or higher (You can check my PHP installation tutorial here. Also I'm on CentOS.

Install Java 1.6 JDK


yum install java-1.6*

Install Apache Solr

We're going to install Apache Solr into /usr/local/share; however you can install it somewhere else if you'd like (e.g. install it in the root dir / )


cd /usr/local/share

Download Apache Solr. We're going to use the latest version at the time of this writing: Apache Solr 3.5. Look for the latest version here: http://www.apache.org/dyn/closer.cgi/lucene/solr/


wget http://mirrors.sonic.net/apache/lucene/solr/3.6.1/apache-solr-3.6.1.tgz
mv apache-solr-3.6.1 apache-solr

Next, download the latest version of the Apache Solr Drupal module Apache Solr Search Integration

Drupal 7

wget http://ftp.drupal.org/files/projects/apachesolr-7.x-3.x-dev.tar.gz
tar xvfz apachesolr-7.x-3.x-dev.tar.gz

Drupal 6

wget http://ftp.drupal.org/files/projects/apachesolr-6.x-3.x-dev.tar.gz
tar xvfz apachesolr-6.x-3.x-dev.tar.gz

Now we are going to rename all the default Apache Solr config files


cd apache-solr/example/solr/conf/
mv schema.xml schema-01.xml
mv solrconfig.xml solrconfig-01.xml
mv protwords.txt protwords-01.txt

Next we'll copy (and rename) all the Apache Solr Search Integration Drupal module config files into our Apache Solr installation at /usr/local/share/apache-solr/example/solr/conf/.


cp /usr/local/share/apachesolr/solr-conf/schema-solr3x.xml ./schema.xml
cp /usr/local/share/apachesolr/solr-conf/solrconfig-solr3x.xml ./solrconfig.xml
cp /usr/local/share/apachesolr/solr-conf/protwords.txt ./

Now we can test if the installation works. Optional; best to skip to next step.

NOTE: You probably should skip this step, and wait till we're done with the automatic startup (below)... as you won't be able to cleanly shut-down Apache solr. You'll get a port in use error message after trying to run it again, since it doesn't shut down properly when you stop the process via CTRL-x. A quick fix was to reboot the server.


cd /usr/local/share/apache-solr/example/
java -jar start.jar

You should now see a bunch of "Info: ... " text. If so, congratulations, your Apache Solr installation was successful

You should be able to visit http://YOURDOMAIN.COM:8983/solr/admin/

If you stop the process via CTRL-X, you will probably have to reboot the server as that's not a clean stop--where your 8083 port will remain in use if you try starting Solr again.

Anyhow... moving on

Start Apache Solr automatically

Now that we got our installation, we want our shit to start automatically (on each reboot)


vi /etc/init.d/solr

Paste this code (hit i key, and then paste via CTRL-SHIFT-V, or CTRL-V, or CMD-V -- it all depends on your SSH client):


#!/bin/sh

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

SOLR_DIR="/usr/local/share/apache-solr/example"
JAVA_OPTIONS="-Xmx2048m -DSTOP.PORT=8079 -DSTOP.KEY=mustard -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

Now hit ESC, and type :x

NOTE: you can change -Xmx2048m to whatever works best for your server. e.g. You can try -Xmx1024m instead; which equals 1GB of memory.

Or you can download this script directly from here (solr.txt; rename to 'solr')--here is help post.

Now that's done, we set the permissions and make our Apache Solr start automatically and shut down properly on reboot.


chmod 755 /etc/init.d/solr
chkconfig --add solr

And here are the available commands:


service solr start
service solr stop
service solr restart

Let's start it


service solr start

You should be able to visit http://YOURDOMAIN.COM:8983/solr/admin/

If you can't reach that, then port 8983 may be blocked by your firewall--which is a good thing. You can still test the connection by installing the Drupal module (next step)

Install Drupal Solr module

Download and install Apache Solr Search Integration module as usual. It should install successfully. You can go and mess around with the settings and indexing or do that shit later after we're fully done with the install...

You can double check here: /admin/config/search/apachesolr/settings/solr/edit or for drupal 6 /admin/settings/apachesolr to see if drupal is able to contact the Apache Solr server.

Next...

Secure Apache Solr

If your 8983 port is blocked, then you're all set. If not, then the best way is to block that port with your firewall.

If you're using ConfigServer Security & Firewall (CSF), then in your /etc/csf/csf.conf, under "TCP_IN" make sure 8983 is NOT present. It's also a good idea to whitelist your IP address if you actually do need to access: http://YOURDOMAIN.COM:8983/solr/admin/

SHAZAM! We're done...

Troubleshooting

Issue: Under admin/settings/apachesolr 0 items show. This was the case for Drupal 6 installations.

Fix: Click "Delete the Search & Solr index" button.

Tags: Server Drupal linux server apache solr solr