Tuesday, August 21, 2012

Installing Solr on Ubuntu

If you want to install Solr on your web server you will need to install some sort of Java application server (JBoss, Jetty, Tomcat ...) to contain the app, that is Solr.

Good place to start is of course the official documentation at Apache and I used this guide to help me get started.

I personally choose Tomcat as my container, because of previous experience.

The exact procedure is:
# install tomcat 
apt-get install tomcat6

# get Solr and extract it
cd /tmp
wget http://archive.apache.org/dist/lucene/solr/3.6.1/apache-solr-3.6.1.tgz
tar xzf apache-solr-3.6.1.tgz

#create folder for Solr and move installation to it 
mkdir -p /var/solr
cp apache-solr-3.6.1/dist/apache-solr-3.6.1.war /var/solr/solr.war
cp -R apache-solr-3.6.1/example/multicore/* /var/solr/
chown -R tomcat6 /var/solr/

#disable tomcat security
echo 'TOMCAT6_SECURITY=no' | sudo tee -a /etc/default/tomcat6

We have now installed Tomcat and Solr but Tomcat is not aware of Solr. We need to define the app context for Tomcat.

In Catalina/localhost define solr.xml (or use any name you like)

vim /etc/tomcat6/Catalina/localhost/solr.xml 
And put following data to it:
<Context path="/solr" docBase="/var/solr/solr.war"
   debug="0" privileged="true" allowLinking="true" crossContext="true">
  <!-- make symlinks work in Tomcat -->
  <Resources className="org.apache.naming.resources.FileDirContext" allowLinking="true" />

  <Environment name="solr/home" type="java.lang.String" value="/var/solr" override="true" />
</Context>
in this file we define that on url /solr (Context path) resides application on location /var/solr/solr.war (docBase). We also define environment variable for java runtime to use.

If you want to define port go to /etc/tomcat6/server.xml and look for Connector port

<Connector port="8983" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               URIEncoding="UTF-8"
               redirectPort="8443" />
Set it to value of your desire.

To make thing simple I compiled a simple shell script to do all of the above:

#!/bin/bash

echo "1. Installing tomcat6"
sudo apt-get install tomcat6

echo "2. change ownership of files"
sudo chown -R tomcat6 /var/solr/

echo "3.tomcat6 configuratin"
echo 'TOMCAT6_SECURITY=no' | sudo tee -a /etc/default/tomcat6
sudo sed -i 's/Connector port="8080"/Connector port="8983"/g' /etc/tomcat6/server.xml

sudo cat > /tmp/solr.xml  << NIZ
<Context path="/solr" docBase="/var/solr/solr.war"
   debug="0" privileged="true" allowLinking="true" crossContext="true">
  <!-- make symlinks work in Tomcat -->
  <Resources className="org.apache.naming.resources.FileDirContext" allowLinking="true" />

  <Environment name="solr/home" type="java.lang.String" value="/var/solr" override="true" />
</Context>
NIZ

sudo mv /tmp/solr.xml /etc/tomcat6/Catalina/localhost/solr.xml

echo "4. Restarting tomcat6"
sudo service tomcat6 restart

Post a Comment