Pages

Thursday 6 December 2012

Setting up Sunspot-Solr in Production with jdk And Apache Tomcat

As you all know Apache Solr is the popular, blazing fast open source enterprise search platform from the Apache Lucene project. Its major features include powerful full-text search, hit highlighting, faceted search, dynamic clustering, database integration, rich document (e.g., Word, PDF) handling, and geospatial search. Solr is highly scalable, providing distributed search and index replication, and it powers the search and navigation features of many of the world's largest internet sites.

Sunspot is a ruby library for the expressive, powerful interaction with the solr search engine. Sunspot is built on the top of RSolr library. Which provides a low level interface for solr interaction, Sunspot provides a simple, intuitive, expressive DSL backed by powerful features for indexing objects and searching for them.

When we are working with RoR we can install sunspot by adding two gems to the Gemfile and do bundle install.

gem 'sunspot_rails'

gem 'sunspot_solr'

Then do bundle insatall.

bundle install

Then generate a default configaration file.

rails generate sunspot_rails:install

Now lets look how we can setting up solar on the system. For installing Solr we need Java in our system. So first we can set up  and configure Java. Install latest jdk to your system. You can either go to sun website and download the package and install it or use apt-get. For the later first run

apt-get update

It will update our source and find the latest packages. Then run
apt-get install openjdk-version-jdk

After completion of installation you should set the path and the java_home to .bashrc file for both root and local.

1. /home/user/.bashrc
2. /root/.bashrc
Then add
1. export PATH=~/java_installation_path(eg:/usr/lib/jvm/java-version)/bin:$PATH
2. export JAVA_HOME=~/java_installation_path(eg:/usr/lib/jvm/java-version)

After adding the above please do "source .bashrc" to affect changes.

If the above mentioned didn't went well please go through the below link. It is a lengthy process bu the job will get done definitely.

http://www.wikihow.com/Install-Oracle-Java-on-Ubuntu-Linux

Next step is installing latest tomcat to your system.

apt-get install tomcat7 tomcat7-admin tomcat7-common tomcat7-user tomcat7-docs

apt-get install libmysql-java(optional)

To test tomcat run
/etc/init.d/tomcat7 start

/etc/init.d/tomcat7 status

Now we need to give permission to user for accessing this.
nano /etc/tomcat7/tomcat-users.xml


It will open the above file and add the following lines to the file.
role rolename="manager"
role rolename="admin"
user username="admin" password="SPECIAL" roles="manager,admin"
Note: Each line should add as xml, means there should be starting and ending tags.

Then add Data Directory and give permission.
mkdir /var/lib/tomcat7/solr/data
chown -R tomcat7:tomcat7 /var/lib/tomcat6/solr/data/

There are some more permissions we have to give.
chown -R tomcat7:tomcat7 /var/lib/tomcat7/
chmod 775 /var/lib/tomcat7/conf/tomcat-users.xml
chmod 775 /var/lib/tomcat7/conf/Catalina/localhost/solr.xml

Wow we almost done. It is the time to test. Re-start Tomcat!!!!
/etc/init.d/tomcat7 restart
To test:
curl http://127.0.0.1:8080/solr/admin/

One last thing!!!!!!!! If you are using Rails for your site please do the following step. Copying the rails config file:
cp RAILS_ROOT/solr/conf/schema.xml /var/lib/tomcat7/solr/conf/schema.xml
/etc/init.d/tomcat7 restart

6 comments:

  1. I have followed the above steps to setup sunspot/solr in the production environment for the rails application.

    while reindexing it showed

    Solr Response: Bad Request


    can you help to solve this issue...

    ReplyDelete
  2. did you configure the sunspot.yml

    ReplyDelete
    Replies
    1. production:
      solr:
      hostname: localhost
      port: 8080 #tomcat defaults to port 8080
      path: '/solr/'
      auto_commit_after_request: false

      Changed like this

      Delete
    2. Is it working now. We are using an exclusive server for solr. The sample configuration
      production:
      solr:
      hostname: ---------- Here -----------
      port: 8080
      path: '/solr'
      log_level: WARNING
      pid_dir: '/home/ubuntu/sunspot_pid_store'
      auto_commit_after_request: false
      Still it is not working please check the log for solr.

      Delete
  3. In your server for Production environment where solr data path location refers to

    Inside the application or in some location ?

    I mean in the solrconfig.xml configuration file

    ${solr.data.dir:./solr/data}

    Thank you for the reply..

    ReplyDelete
    Replies
    1. Same only,Both application and system. Sorry I can't find out. If you fixed please let me know. I can also update my knowledge.

      Delete