Skip to main content

Setting up Solr and Drupal on Ubuntu 12.x

Back-end Development
Drupal

I've set up a new server with Solr and a drupal site a time or two, and now that I find myself doing it again, I figured I would keep a record and share for those a little unsure of where to begin.  So enough talk, here is what your searching for...

sudo apt-get update // make sure the index is updated
sudo apt-get install tomcat 6

Bam, your done!!!

Just Kidding...

It's time to get Solr.  I am going to grab the latest version of 3.x. in my case version 3.6.2.  Version 4.2 available, but because I'm not seeing much discussion regarding the usage of this with Drupal I'm going to assume it's not been well tested.  Also, apt has a solr-tomcat package.  I looked into this and it would appear this installs version 1.4 of solr.  Since I would prefer to have a newer version (though not the newest). I will need to downlaod it manually.  You probably don't want to just use the URL I have below, rather goto the apache solr website and follow the download links to get the appropriate tgz file.

At this point I'll stop using sudo on every line and just switch into the sudo shell

sudo -s -H

You don't have to do this... but if you do not just make sure to prefix all these commands with sudo.

cd /tmp
wget http://www.trieuvan.com/apache/lucene/solr/3.6.2/apache-solr-3.6.2.tgz
tar xvzf apache-solr-3.6.2.tgz
mkdir /var/solr 

Now we throw the war archive into this directory, an example to use for reference (I'm using the multicore one) and reference it from Tomcat (Catalina).

cp apache-solr-3.6.2/dist/apache-solr-3.6.2.war /var/solr/solr.war
cp -R apache-solr-3.6.2/example/multicore/* /var/solr/

Check to see if you have the tomcat6 user... you should if you installed with apt-get.

id tomcat6
chown -R tomcat6 /var/solr 

Now create an XML file at /etc/tomcat6/Catalina/localhost/solr.xml and include the following XML. This xml file (on tomcats auto discovery path) will let Tomcat know that the application solr can be found in /var/solr using the /var/solr/solr.war archive file.

<Context docBase="/var/solr/solr.war" debug="0" privileged="true" allowLinking="true" crossContext="true">
  <Environment name="solr/home" type="java.lang.String" value="/var/solr" override="true" />
</Context>

Next edit /etc/default/tomcat6 and uncomment the line

TOMCAT6_SECURITY=no

Now the port... it defaults to port 8080. Which is typically okay if your simply running apache on port 80.  But in my situation I am running apache port (and varnish on 80).  I change this port by editing the server.xml file in /etc/tomcat6.

<Connector port="8081" protocol="HTTP/1.1"
  connectionTimeout="20000"
  URIEncoding="UTF-8"
  redirectPort="8443" />

You should not be able to restart tomcat and see things working appropriately on by visiting the URL of your server with the appropriate port.  You can restart things by executing

/etc/init.d/tomcat6 restart

You should be able to visit http://youraddress/solr. and see the welcome page.  If so, contratualtions you should be all set up... now on to Drupal.

Drupal Setup 

Using drush as we normally do we'll grab the core apachesolr module / library for our installation.

drush dl apachesolr
drush en apachesolr 
drush en apachesolr_search

Note that enabling the search module is required for solr implementation.

Next we need to install the solr PHP library on our site.  Download the latest version from https://code.google.com/p/solr-php-client/downloads/list. And place it in your sites/all/libraries directory.

Next visit the apachesolr settings page and change the port to what you've set tomcat to run on. Settings can be found at Home » Administration » Configuration » Search and metadata » Apache Solr search

Now we need to configure solr to work with our Drupal site. Luckily the apachesolr module comes bundled with a lot of the work done for us already.  In your solr configuration directory, move the solrconfig.xml and schema.xml file out of the way and replace them with the files found in the apachesolr module under the solr-conf directory.  

Now restart tomcat

/etc/init.d/tomcat6 restart

You should now test that everything is still kosher by visiting http://youraddress/solr/admin. You should see a reference to drupal in the page header.  Like Solr Admin (drupal-3.0-0-solr3). You should also notice this same description within the solr settings admin page in your Drupal installation.

This completes the base install of solr on your site.  More configuration will likely be needed to tweek things how you want them to be.  There are also many, many other modules and add-ons to really make your search powerful.