Installing Apache Solr on CentOS

Installing Apache Solr on CentOS
Apache’s Solr search platform is a common solution for more flexible and better performing searches on sites that have outgrown their database’s built-in search facilities. Here at Nexcess, the most common use is by clients with Magento Enterprise Edition (the only version with built-in support for Solr). In this post I will walk through setting up Solr for general use, with some comments on setting it up for use the Magento EE.

Solr runs as a Java servlet, so the first thing needed is a way to run Java servlets. We’ll be using the OpenJDK and Tomcat from the JPackage repo, so we need to install the repo with:

wget http://jpackage.org/jpackage50.repo -O /etc/yum.repos.d/jpackage50.repo

Then install tomcat and java:

yum install -y java-1.6.0-openjdk tomcat6


NOTE You may get an error about unmet dependencies (probably only on CentOS 5). If so, you’ll want to install a compatibility RPM like so:

rpm -Uvh 'http://plone.lucidsolutions.co.nz/linux/centos/images/jpackage-utils-compat-el5-0.0.1-1.noarch.rpm'

Then run the yum command again. Next you’ll want to go to the Apache Solr mirrors page and select a mirror, then download the latest version of solr and extract it (3.5.0 is the latest at time of writing). Pay close attention to the file name, you don’t want the source (src) package:

wget -qO- 'http://apache.osuosl.org//lucene/solr/3.5.0/apache-solr-3.5.0.tgz' | \
tar xzf -

Now that we have the solr package, we need to get it into the right place (replace ${VERSION} with the version of solr you’re installing):

mv apache-solr-${VERSION}/example/solr /opt && \
mv apache-solr-${VERSION}/dist/apache-solr-${VERSION}.war /opt/solr/solr.war && \
chown tomcat:tomcat -R /opt/solr

Then Tomcat needs to be told about solr and started:

cat > /usr/share/tomcat6/conf/Catalina/localhost/solr.xml <<EOF
<?xml version="1.0" encoding="utf-8"?>
<Context docBase="/opt/solr/solr.war" debug="0" crossContext="true">
  <Environment name="solr/home" type="java.lang.String" value="/opt/solr/" override="true"/>
</Context>
EOF
chkconfig tomcat6 on && service tomcat6 restart

Solr should now be setup and working in a generic way! You can check by running this command and getting a 200 OK response (note that the trailing slash is important):

curl -sI http://localhost:8080/solr/ | head -2
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1

To use it with Magento requires changing the config that gets used. This can be done after moving solr to the /opt/solr directory:

mv /opt/solr/conf{,.orig} && \
ln -s /path/to/magento/lib/Apache/Solr/conf && \
cp /opt/solr/conf.orig/mapping-* /opt/solr/conf/ && \
wget -O /opt/solr/conf/elevate.xml 'http://magento-solr.googlecode.com/svn/trunk/solr/solr-home/conf/elevate.xml'

You can then continue on with the directions as usual.

This entry was posted in Apache. Bookmark the permalink.