PigRider Blogs

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

Install Solr 4.4 with Jetty in CentOS, and set up Solr server to work with Sunspot Gem.

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

Ruby on Rails Linux

The process to install Solr 4.4 with Jetty in CentOS, and set up Solr server to work with Sunspot Gem: (Please click here to see my latest blog for installing Solr 4.10, in which I developed an easy, straightforward, understandable, and painless method to let Solr work together with Sunspot gem.) 1. Install Java OpenJDK. Solr 4.4 requires Java 1.6 or greater.
yum -y install java-1.7.0-openjdk.x86_64
2. Download Solr 4.4 and drop it into our installation directory /opt/solr
curl -O http://www.us.apache.org/dist/lucene/solr/4.4.0/solr-4.4.0.tgz
tar xzf solr-4.4.0.tgz
mv solr-4.4.0/example /opt/solr
3. Enable Solr as a standard service
# Download the Jetty default startup script
curl -o /etc/init.d/solr http://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk/jetty-distribution/src/main/resources/bin/jetty.sh

# Change file to be executable
chmod +x /etc/init.d/solr

# Change references from Jetty configuration to Solr
perl -pi -e 's/\/default\/jetty/\/sysconfig\/solr/g' /etc/init.d/solr

# Enable the Solr service
chkconfig solr on
4. 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/
5. The startup script looks for environment variables in /etc/sysconfig/solr. Create this file and put below codes in:
JAVA_HOME=/usr/java/default
JAVA_OPTIONS="-Dsolr.solr.home=/opt/solr/solr $JAVA_OPTIONS"
JETTY_HOME=/opt/solr
JETTY_USER=solr
JETTY_LOGS=/opt/solr/logs
6. Start Solr as a normal service. Now, Solr 4.4 with Jetty is successfully installed.
service solr restart
7. Next, set up this Solr 4.4 server with Sunspot gem. In your Rails application Gemfile, add these two gems:
gem 'sunspot_rails'

group :development do
  gem 'sunspot_solr'
end
After running bundle install, run these two commands:
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. A Solr configuration file schema.yml, which is used to set connection between Sunspot and Solr, can be found in the directory /solr/conf/. 8. Copy this configuration file schema.yml from your Rails application to the home directory of the running Solr 4.4 instance. It will overrider the Solr example configuration file there, and it will set up Solr 4.4 server to work with Sunspot Gem.
cp /RailsApplicationPath/Solr/conf/schema.yml /opt/solr/solr/collection1/conf/.
The home directory of the running Solr 4.4 instance is /opt/solr/solr/collection1/. You can find this information from Solr admin page http://localhost:8983/solr/admin 9. Add _version_ field into the configuration file schema.yml to satisfy Solr 4.4 initialization requirement. Actually, two lines of code need to be added into the file. They are:
<field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/>

<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
The configuration file schema.yml eventually will look like:
<schema name="sunspot" version="1.0">
  <types>
    <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
    <!-- *** Other Sunspot fieldType Definitions *** -->
  </types>

  <fields>
    <field name="_version_" type="long" indexed="true" stored="true" multiValued="false"/>
    <!-- *** Other Sunspot field Definitions *** -->
  </fields>

  <!-- *** Other Sunspot Configurations *** -->
</schema>
10. Now, your Solr 4.4 server should work well with the Sunspot Gem in Rails production environment using the port 8983. However, you may wish to add more ports, like port 8982 for Rails development environment. This can be set in the Jetty configuration file /opt/solr/etc/jetty.xml You will see below codes in this Jetty configuration file. It is used to set the port 8983 for Rails production environment.
    <Call name="addConnector">
      <Arg>
          <New class="org.eclipse.jetty.server.bio.SocketConnector">
            <Call class="java.lang.System" name="setProperty"> <Arg>log4j.configuration</Arg> <Arg>etc/log4j.properties</Arg> </Call>
            <Set name="host"><SystemProperty name="jetty.host" /></Set>
            <Set name="port"><SystemProperty name="jetty.port" default="8983"/></Set>
            <Set name="maxIdleTime">50000</Set>
            <Set name="lowResourceMaxIdleTime">1500</Set>
            <Set name="statsOn">false</Set>
          </New>
      </Arg>
    </Call>
If you wish to add a new port for Solr 4.4 server to listen, you only need to modify port number in above codes and add them into the Jetty configuration file /opt/solr/etc/jetty.xml For example, if a new port 8982 for Rails development environment need to be added, the configuration in /opt/solr/etc/jetty.xml will look like:
    <!-- Original Port 8983 for Rails Production Environment -->
    <Call name="addConnector">
      <Arg>
          <New class="org.eclipse.jetty.server.bio.SocketConnector">
            <Call class="java.lang.System" name="setProperty"> <Arg>log4j.configuration</Arg> <Arg>etc/log4j.properties</Arg> </Call>
            <Set name="host"><SystemProperty name="jetty.host" /></Set>
            <Set name="port"><SystemProperty name="jetty.port" default="8983"/></Set>
            <Set name="maxIdleTime">50000</Set>
            <Set name="lowResourceMaxIdleTime">1500</Set>
            <Set name="statsOn">false</Set>
          </New>
      </Arg>
    </Call>

    <!-- New Added Port 8982 for Rails Development Environment -->
    <Call name="addConnector">
      <Arg>
          <New class="org.eclipse.jetty.server.bio.SocketConnector">
            <Call class="java.lang.System" name="setProperty"> <Arg>log4j.configuration</Arg> <Arg>etc/log4j.properties</Arg> </Call>
            <Set name="host"><SystemProperty name="jetty.host" /></Set>
            <Set name="port"><SystemProperty name="jetty.port" default="8982"/></Set>
            <Set name="maxIdleTime">50000</Set>
            <Set name="lowResourceMaxIdleTime">1500</Set>
            <Set name="statsOn">false</Set>
          </New>
      </Arg>
    </Call>
Last, run the Linux command service solr restart to let Solr server pick up new configurations. 11. 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. Please modify it according to your case.
curl http://localhost:8983/solr/replication?command=details
Some contents in this blog is not written by me originally. I just copied and recorded them from other websites.