Apache Solr is an open source text search server. It is based on the Apache Lucene search libraries. Solr does full-text search, highlight the hits, near real-time indexing.
It has an extremely scalable search infrastructure that provides replication, load-balanced search query, and automatic failover. This can get the input data that needs to be indexed for searching from various sources including information from a database. You can use HTTP/XML, JASON APIs provided by Apache Solr and write application code in any programming language.
The Apache Solr search server is written in Java. So, it needs a servlet container in the backend to run. By default, when you install Apache Solr, it comes with Jetty as the servlet container that you can use to run some examples.

But in real life, when you install Apache Solr, you want to install with much more robust servlet container like Tomact. This article explains how to install Solr with Tomcat.

Create a Solr account

On your system, create a username for solr and install Solr under that account. In this example, I’ve created a username called solrdev, where I’ll install solr for my development purpose.

# adduser solrdev
# passwd solrdev

Download Solr

The current stable version of solr is 4.2. Download it from Apache Solr website. Or, use the following wget command to download it directly.

# su - solrdev
$ wget http://apache.petsads.us/lucene/solr/4.2.0/solr-4.2.0.tgz
$ tar xvfz solr-4.2.0.tgz
 

Download Tomcat

The current stable version of tomcat is 7.0.39. Download it from Apache Tomcat website. Or, use the following wget command to download it directly. We’ll be running both Tomcat and Solr under solrdev account that we just created.
wget http://mirror.symnds.com/software/Apache/tomcat/tomcat-7/v7.0.39/bin/apache-tomcat-7.0.39.tar.gz tar xvfz apache-tomcat-7.0.39.tar.gz

Create Solr Example Directory

Create the solr example directory, where we’ll host all the solr cores. This will also be used as SOLR_HOME.

mkdir -p /home/solrdev/solr/example/
cd /home/solrdev/solr-4.2.0/example
cp -r solr /home/solrdev/solr/example/

Setup Environment Variable

Setup the appropriate environment variables that are required by both Tomcat and Solr in your .bash_profile. Append the following lines to your .bash_profile.

vi ~/.bash_profile
export CATALINA_HOME=/home/solrdev/apache-tomcat-7.0.39
export PATH=~/bin:~/local/bin:$CATALINA_HOME/bin:$PATH
export CATALINA_OPTS="-server -Xmx2048m -Xms2048m"
export JAVA_OPTS="-Dsolr.data.dir=/home/solrdev/solr/example/solr"
export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=/home/solrdev/solr/example/solr"
export SOLR_HOME=/home/solrdev/solr/example/solr
Execute the .bash_profile.

$ cd
$ . ./.bash_profile

Copy Solr War file to Example Directory

Copy the solr war file from the distribution directory to the example SOLR_HOME directory that we created.

cd /home/solrdev/solr-4.2.0/dist
cp solr-4.2.0.war /home/solrdev/solr/example/solr/

Change the ports accordingly in server.xml

Verify the Tomcat ports. If this is the 1st tomcat instance on the server, you don’t need to modify these ports. If you are installing more than one tomcat on the same server, you may want to change these ports so that it doesn’t conflict.
As you see below, Tomcat is using 8005 for shutdown port, 8080 for HTTP, 8443 for HTTPS and 8009 for AJP.

$ cd /home/solrdev/apache-tomcat-7.0.39/conf
$ grep -i port server.xml
<Server port="8005" shutdown="SHUTDOWN">
    <Connector port="8080" protocol="HTTP/1.1"
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Start the Tomcat

Start the Apache Tomcat using the catalina.sh script as shown below.

$ cd /home/solrdev/apache-tomcat-7.0.39/bin

$ ./catalina.sh start
Using CATALINA_BASE:   /home/solrdev/apache-tomcat-7.0.39
Using CATALINA_HOME:   /home/solrdev/apache-tomcat-7.0.39
Using CATALINA_TMPDIR: /home/solrdev/apache-tomcat-7.0.39/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /home/solrdev/apache-tomcat-7.0.39/bin/bootstrap.jar:/home/solrdev/apache-tomcat-7.0.39/bin/tomcat-juli.jar
Note: If you don’t have java installed on your system, you’ll get the following error message. Make sure you have JAVA_HOME environment variable setup to point to the java on your system.

$ ./catalina.sh start
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
Verify that the Tomcat started without any issues by checking the log files.

$ cd /home/solrdev/apache-tomcat-7.0.39/logs

$ ls
manager.2013-01-24.log
host-manager.2013-01-24.log
localhost_access_log.2013-01-24.txt
localhost.2013-01-24.log
catalina.2013-01-24.log
catalina.out

$ tail catalina.out
INFO: Starting ProtocolHandler ["http-bio-8082"]
Jan 24, 2013 23:04:27 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8011"]
Jan 24, 2013 23:04:27 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 642 ms

Create solr.xml

Next, create a solr configuration file solr.xml as shown below.

$ cd /home/solrdev/apache-tomcat-7.0.39/conf/Catalina/localhost
$ vi solr.xml
<?xml version="1.0" encoding="utf-8"?>
<Context docBase="/home/solrdev/solr/example/solr/solr-4.2.0.war" debug="0" crossContext="true">
  <Environment name="solr/home" type="java.lang.String" value="/home/solrdev/solr/example/solr" override="true"/>
</Context>
 
You are pretty much done with the basic setup at this stage. After this, you can copy one of the solr example cores that comes with the Apache Solr to the /home/solrdev/solr/example/solr directory and add this to the /home/solrdev/solr/example/solr/solr.xml file. If you’ve developed your own core, copy those over to the /home/solrdev/solr/example/solr directory, and modify the /home/solrdev/solr/example/solr/solr.xml file with the core information.
For example, if your core name is “devproj”, you’ll add an entry similar to the following in the /home/solrdev/solr/example/solr/solr.xml file.

$ vi solr.xml
  <cores adminPath="/admin/cores">
        <core name="devproj"       instanceDir="devproj" />
  </cores>
To view your Solr cores, go to http://{your-ip-address}:8080/solr

 
أحدث أقدم