by Ben Chavet

Installing Solr for use with Drupal

Overview

Solr is a powerful and feature-rich search platform released by Apache. Integrating it with Drupal allows for faster and more advanced search options. However, it also means that a Solr instance needs to be installed and running somewhere, similar to how a database like MySQL is required.

Solr is a Java application, and can be run independent from any server technology. However, for a production environment, it is typically best to run it in a J2EE server environment such as Tomcat, Glassfish, JBoss, etc. This article describes how to install Solr 4.3.0 for use by Drupal running under Tomcat 7 on a Linux server.

Java Installation

Tomcat and Solr are both Java applications, so the only real prerequisite is to install Java. The method of installation will vary by Linux distribution and the desired flavor of Java. Redhat, CentOS, Debian, and Ubuntu all provide OpenJDK implementations of Java 7 in their software repositories, which is used here. Any Java implementation should work here, though. So feel free to skip this step if if a different flavor is desired, or if Java is already installed on the server. Please note that a full Java Development Kit (JDK) must be installed. A Java Runtime Environment (JRE) installation is not sufficient.

Redhat/CentOS:


yum install java-1.7.0-openjdk
  

Debian/Ubuntu:


aptitude install java7-jdk
  

Tomcat Installation

Some Linux distributions provide a Tomcat package in their software repositories. However, installing the latest version from the Apache Software Foundation ensures that all of the latest security and bug fixes are present. It also keeps all of the configuration and data files consolidated into one location, and works the same regardless of Linux distribution.

Step 1: Create a low-privilege user, which will be used to run the Tomcat service.


useradd -Mb /usr/local tomcat
  

Step 2: Download the latest tar.gz binary of Tomcat 7 from http://tomcat.apache.org/download-70.cgi to /usr/local/src/ on the server.

Step 3: Unpack the Tomcat tar.gz file to /usr/local/tomcat


tar -C /usr/local -zxf /usr/local/src/apache-tomcat-7.*.tar.gz
mv /usr/local/apache-tomcat-7.* /usr/local/tomcat
  

Step 4: By default Tomcat listens on port 8080. However, that is a commonly used port for other services as well. To avoid conflict, change Tomcat to use port 8983 instead with this search/replace comand.


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

Step 5: Finally, change the ownership of the Tomcat directory, and start it to verify that it is working


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

It is important to note that there are some security implications of running Tomcat that need to be considered. Tomcat security is outside of the scope of this article, but there are a number of good resources available that describe how to harden a Tomcat installation, including one provided directly by the Tomcat project at http://tomcat.apache.org/tomcat-7.0-doc/security-howto.html. Generally speaking, though, if the only use for this Tomcat instance is to serve internal Solr requests, blocking outside access with the use of a firewall is usually sufficient.

Solr Installation

Some Linux distributions also provide a Solr package in its repository, but it is typically an old version. Just like with Tomcat, installing the latest package from the upstream project is the method used here.

Step 1: Download Solr-4.3.0 from http://lucene.apache.org/solr/ to the server and unpack the downloaded file.


tar -zxf solr-4.3.0.tgz
  

Step 2: Copy the java libraries provided by Solr to the Tomcat library directory


cp solr-4.3.0/dist/solrj-lib/* /usr/local/tomcat/lib/
  

Step 3: Copy the log4j configuration file provided by Solr to the Tomcat configuration directory


cp solr-4.3.0/example/resources/log4j.properties /usr/local/tomcat/conf/
  

Step 4: Copy the Solr webapp file to the Tomcat webbapp directory


cp solr-4.3.0/dist/solr-4.3.0.war /usr/local/tomcat/webapps/solr.war
  

Step 5: Create the Solr context file at /usr/local/tomcat/conf/Catalina/localhost/solr.xml with the following contents.


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

Solr Indexes

Solr is capable of providing multiple search indexes, or cores, using just one instance of the Solr application. Each core is independently configured, and there is a single configuration file to define each of the cores. The steps below show how to create a core named drupal. These steps can be used to create as many cores as required, each with a unique name.

Step 1: Create the base Solr directory, and create a copy of the example configuration


mkdir -p /usr/local/tomcat/solr
cp -r solr-4.3.0/example/solr/collection1/conf /usr/local/tomcat/solr/
  

Step 2: Download the latest version of the apachesolr Drupal module from https://drupal.org/project/apachesolr to the server and unpack the downloaded file.


tar -zxf apachesolr-*.tar.gz
  

Step 3: Copy the Solr configuration files from the Drupal module to the example Solr configuration directory from above.


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

Step 4: Create the Solr core definition file at /usr/local/tomcat/solr/solr.xml with the following contents to define the drupal core.


<?xml version="1.0" encoding="UTF-8" ?>
<solr persistent="false">
  <cores adminPath="/admin/cores">
    <core name="drupal" instanceDir="drupal" />
  </cores>
</solr>
  

Step 5: Create the drupal Solr core directory as defined above, and copy the example Solr configuration files to that location. These configuration files can be further modified as needed for any specific requirements for this core.


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

Step 6: Stop Tomcat, make sure the permissions are correct, and start Tomcat back up


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

The new Solr core admin interface is now available at http://localhost:8983/solr/#/drupal. The URL to use in Drupal's Apache Solr configuration is http://localhost:8983/solr/drupal.

Wrapping Up

The last order of business is to provide a method for which Solr can be started automatically when the server reboots. Attached to this article is an init script which will provide exactly that, and will work with both Redhat based and Debian based Linux distributions. Create the init file at /etc/init.d/tomcat. Then, make sure it is executable, and configure it to start on reboot with the following commands.


chmod +x /etc/init.d/tomcat
  

Redhat/CentOS:


chkconfig --add tomcat
  

Debian/Ubuntu:


update-rc.d tomcat defaults
  

If everything goes well, you'll have a working Solr search server. For more information about integrating it with Drupal, you can visit the Apache Solr Integration project on Drupal.org.

newsletter-bot