PigRider Blogs

Sign In
HTML CSS Javascript Ruby on Rails C++ Java Python SQL Git Linux Others All

Install or upgrade to Solr 4.10 with Jetty in CentOS 6.6, and set up Solr server to work with Sunspot Gem 2.1.1

[Author: Dingyu]   [Tue, 2017-03-14, 16:14]   [5393 views]

Ruby on Rails Linux

An easy, straightforward, understandable, and painless process to install or upgrade to Solr 4.10 with Jetty in CentOS 6.6, and set up Solr server to work with Sunspot Gem 2.1.1 1. Install Java OpenJDK. Solr 4.10 requires Java 1.7 or greater. I installed Java 1.8 for myself.
yum list java*

# (optional: remove your old Java version
#   for example:

    yum remove java-1.7.0-openjdk

#   you may need to "yum remove" more related java softwares 
#   then run "java -verison" command, you should see "java: command not found"
# )

yum install java-1.8.0-openjdk

# then run "java -version" command, you should see something like "openjdk version 1.8.0_25"
2. Download Solr 4.10 and drop it into the installation directory /opt/solr
curl -O https://archive.apache.org/dist/lucene/solr/4.10.2/solr-4.10.2.tgz

tar zxvf solr-4.10.2.tgz

# (optional: backup

    mv /opt/solr /SomeBackupDirectory/solr_old

# )

mv solr-4.10.2 /opt/solr
3. Creating a dedicated Solr user allows permissions to be explicitly defined and reduce confusion caused by shared user accounts.
useradd -r -d /opt/solr -M -c "Apache Solr" solr

# Change permissions of Solr installation to this user
chown -R solr:solr /opt/solr/
4. Enable Solr as a standard service
# (optional: backup

    mv /etc/init.d/solr /SomeBackupDirectory/solr_old

# )

vi /etc/init.d/solr
Then, copy the below bash script into the file, and save it.
#!/bin/bash
#
# solr         APACHE SOLR 4.10
#
# chkconfig:   2345 85 15
# description: Startup Script for APACHE SOLR 4.10
#
# processname: solr


SOLR_BIN="/opt/solr/bin/solr"
SOLR_PORT="8983"


start() {
  $SOLR_BIN start -p $SOLR_PORT
  RETVAL=$?

  return $RETVAL
}


stop() {
  $SOLR_BIN stop -p $SOLR_PORT
  RETVAL=$?

  return $RETVAL
}


restart() {
  $SOLR_BIN restart -p $SOLR_PORT
  RETVAL=$?

  return $RETVAL
}


status() {
  $SOLR_BIN -i
  RETVAL=$?

  return $RETVAL
}


case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: service solr {start|stop|restart|status}"
esac


exit $RETVAL
Then, make the script be able to auto start when the system boots.
# Change file to be executable
chmod +x /etc/init.d/solr

# Enable the Solr service
chkconfig solr on

chkconfig --list solr

service solr restart   # this command should work now

# also be able to use below three commands:
# service solr start
# service solr stop
# service solr status
5. Next, set up the Solr 4.10 server with Sunspot gem. In your Rails application Gemfile, add these two gems:
gem 'sunspot_rails', '2.1.1'

group :development do
  gem 'sunspot_solr', '2.1.1'
end
After run bundle install and set SQL database credentials in /YourRailsApplicationPath/config/database.yml, then:
# (optional: backup
    mv /YourRailsApplicationPath/solr /SomeBackupDirectory/solr_old
    mv /YourRailsApplicationPath/config/sunspot.yml /SomeBackupDirectory/sunspot.yml_old
# )

rails generate sunspot_rails:install

bundle exec rake sunspot:solr:start   # or sunspot:solr:run to start in foreground
A directory solr will be generated in your Rails application. A Solr configuration file schema.yml, which is used to set connection between Sunspot and Solr, and can be found in the directory /YourRailsApplicationPath/solr/conf/ * Please make sure that you use the version 2.1.1 for Sunspot here. It is important because the schema.yml file of later versions might be not compatible with Solr 4.10 6. Copy this configuration file schema.yml from your Rails application to the configuration directory of the running Solr 4.10 instance. It will overrider the Solr example configuration file there, and it will set up Solr 4.10 server to work with Sunspot Gem.
cp /YourRailsApplicationPath/Solr/conf/schema.yml /opt/solr/example/solr/collection1/conf/.
chown solr:solr /opt/solr/example/solr/collection1/conf/schema.xml

service solr restart
The path of the running Solr 4.10 instance is /opt/solr/example/solr/collection1. You can find this information from Solr admin page http://localhost:8983/solr/admin 7. Modify the production setting in the Sunspot gem configuration file /YourRailsApplicationPath/config/sunspot.yml to:
production:
  solr:
    hostname: localhost
    port: 8983
    log_level: WARNING
    path: /solr/collection1   # important!
    # read_timeout: 2
    # open_timeout: 0.5
Then, you may be able to run bundle exec rake sunspot:solr:reindex in the production environment. 8. To check Solr server index size or other status, run below line in Linux. Sunspot uses port 8983 for production environment, so the below example uses port 8983. You may modify it according to your case.
curl http://localhost:8983/solr/replication?command=details