How to Install Apache Solr with Tomcat on Linux

by Ramesh Natarajan on April 12, 2013

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


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 6 comments… read them below or add one }

1 BajiBabu April 17, 2013 at 3:25 am

To,
Natarajan sir,
hi sir goodafternoon sir.
sir i have a doubt can you please clarify that one
i.e how to recover delete file/directory in linux .
please give the solution
Thanks&Regards
BajiBabu

2 Jeremy May 21, 2013 at 1:50 pm

You also need to add the SLF4J logging jars to /home/solrdev/apache-tomcat-7.0.39/lib. Otherwise you will see this error when you try to start the app: “Application at context path /solr could not be started”. You can download them here.

3 chud May 22, 2013 at 12:32 pm

Good instructions.
Don’t forget to open port 8080 in your firewall afterward.

4 MarwaGamal June 14, 2013 at 1:55 pm

thanx , it really helped me .

i’d ask for help cause i have done everything as mentioned above except that when i try to run my core from the web , tomcat reported 404 error as “The requested resource is not available.” how could i know what cause that error ??how could i fix it ?

thanx in advance

5 vardhan November 7, 2013 at 12:39 am

After installing the solr in tomcat by above procedure, I want to index the data in solr. can you provide tutorial for indexing as continuation of this tutorial?

6 tim December 3, 2013 at 12:52 pm

you also need to start the solr server by doing something like
$ cd /home/solrdev/solr-4.2.0/example
$ java -jar start.jar

Leave a Comment

Previous post:

Next post: