Contact Krimson

Call us: +32 (0) 3 298 69 98

Email us: info@krimson.be

Setup Drupal 6.x with Apache Solr on Tomcat6 and Ubuntu

Wow, I spent alot of time to make this guide as simple as possible. This is my second try in making a simple Apache Solr Setup Guide.

One good advice, never use the tomcat or the solr packages from ubuntu or any others...! REALLY!!! It will make a little mess in your filesystem and you can't get control anymore of the paths that are in use.
So if you have a clean server try this to make Apache Solr working
Note : Java is required (preferably the java 6 from Sun)

Step 1 : Downloading and installing tomcat and solr

Get the latest tomcat release and put it in your home directory (http://tomcat.apache.org/download-60.cgi)
Grab the latest Apache Solr and put it in your home directory as well (http://apache.belnet.be/lucene/solr/ or the nightly build. In this example I used the nightly build)

cd ~/
tar zxvf apache-tomcat-6*
tar zxvf apache-solr-*

Move tomcat to any place you like, I preferred to install it in /usr/local/tomcat6 (if you change it, make sure you change all other paths in this guide as well)
sudo mv apache-tomcat-6.0.18/ /usr/local/tomcat6/

Next, copy the Solr .war file from apache-solr-nightly/dist/ to the webapps/ directory of tomcat6/ and the example webapp from apache-solr-nightly/example/solr/ to the tomcat6/ root directory.
sudo cp apache-solr-nightly/dist/apache-solr-nightly.war /usr/local/tomcat6/webapps/solr.war
sudo cp -r apache-solr-nightly/example/solr/ /usr/local/tomcat6/solr/

Now that all the files are in place, we need to create a config file to run Solr.
sudo mkdir /usr/local/tomcat6/conf/Catalina/
sudo mkdir /usr/local/tomcat6/conf/Catalina/localhost/
sudo nano /usr/local/tomcat6/conf/Catalina/localhost/solr.xml

In this file, insert the following code:

<Context docBase="/usr/local/tomcat6/webapps/solr.war" debug="0" crossContext="true" >
   <Environment name="solr/home" type="java.lang.String" value="/usr/local/tomcat6/solr" override="true" />
</Context>

Edit your .bashrc file in your home directory and add the following to it:
Make sure the JAVA_HOME path is correct. Look for a java-6-sun* folder

export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.10
export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=/usr/local/tomcat6/solr"
export CATALINA_BASE=/usr/local/tomcat6
export CATALINA_HOME=/usr/local/tomcat6

Let's apply this exports and let's start our apache solr configuration
/usr/local/tomcat6/bin/startup.sh

If all went well, you should be able to start Tomcat and browse to http://localhost:8080/solr/admin and see the Solr admin page.

Step 2 : Making tomcat6 autostart and easy manual start

If this worked, great, but now it's time to get Tomcat to autostart itself when your server reboots. First, open a new script in your /etc/init.d/ directory.

sudo nano /etc/init.d/tomcat6

Insert the following code but be carefull, adjust the java path to your likings!
# Tomcat auto-start
#
# description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pid

export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.10
export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=/usr/local/tomcat6/solr"

case $1 in
start)
   sh /usr/local/tomcat6/bin/startup.sh
   ;;
stop)
   sh /usr/local/tomcat6/bin/shutdown.sh
   ;;
restart)
   sh /usr/local/tomcat6/bin/shutdown.sh
   sh /usr/local/tomcat6/bin/startup.sh
   ;;
esac
exit 0

Finally, to make sure it autostarts, run this command:
sudo chmod +x /etc/init.d/tomcat6
sudo update-rc.d tomcat6 start 91 2 3 4 5 . stop 20 0 1 6 .

Now when you want to shutdown or start manually this command works great
/etc/init.d/tomcat6 stop
/etc/init.d/tomcat6 start
/etc/init.d/tomcat6 restart


Step 3 : Connecting Drupal

robertDouglass wrote and maintains the excellent Apache Solr module. The module is integrated in the Acquia distributiun, or can be downloaded separately from Drupal.org.

We are going to copy our solrconfigs and schema from the drupal module to our directory
If you have an acquia distribution :

sudo cp /var/www/YOURWEBSITE/modules/acquia/apachesolr/solrconfig.xml /usr/local/tomcat6/solr/conf/solrconfig.xml

sudo cp /var/www/YOURWEBSITE/modules/acquia/apachesolr/schema.xml /usr/local/tomcat6/solr/conf/schema.xml

If you downloaded the module, install it and don't forget to checkout the important SolrPhpClient library (see the apachesolr README.txt)

cd /var/www/YOURWEBSITE/sites/all/modules/contrib/apachesolr/
sudo cp /var/www/YOURWEBSITE/sites/all/modules/contrib/apachesolr/solrconfig.xml /usr/local/tomcat6/solr/conf/solrconfig.xml
sudo cp /var/www/YOURWEBSITE/sites/all/modules/contrib/apachesolr/schema.xml /usr/local/tomcat6/solr/conf/schema.xml

Tip: it might be a good idea to use symlinks so we can easily update our modules and update our schemes if they change ...you never know with opensource ;-)

Step 4 : Configuring Drupal

Enable the module in the modules list and go to the config screen
fill in the next parameters :
Solr host name:
Host name of your Solr server, e.g. localhost or example.com (check the beginning, mostly localhost)
localhost

Solr port:
Port on which the Solr server listens. Tomcat is 8080 by default.
8080

Solr path:
Path that identifies the Solr request handler to be used.
solr

Enable spellchecker is a nice feature :-)

And click save configuration.

It's possible you'll have to click save twice for all the notices to disappear. If you do see them, check if all your variables are correct or refresh.

Click on the search index tab in our drupal site and just for caution, click on delete index (don't forget the checkbox) and click on re-index all content and run cron.

Step 5 : Make a second SOLR site

We are going to create a second configuration file and another apache solr directory for our site-specific files and insert the following configuration

sudo nano /usr/local/tomcat6/conf/Catalina/localhost/site2.xml

<Context docBase="/usr/local/tomcat6/webapps/solr.war" debug="0" crossContext="true" >
   <Environment name="solr/home" type="java.lang.String" value="/usr/local/tomcat6/site2" override="true" />
</Context>

And also make our site-specific directories and our admin interface

cd /usr/local/tomcat6
cp -r ~/apache-solr-nightly/example/solr/ /usr/local/tomcat6/site2
cd /usr/local/tomcat6/webapps
cp -r solr site2

And restart our tomcat service
/etc/init.d/tomcat6 restart

Happy solr'ing!