Setting up Apache Solr 5 on Centos for use with Drupal

Setting up Apache Solr 5 on Centos for use with Drupal

As of this writing, the latest version of Solr is 5.3.0. I used this version but I assume that will work on any version 5.x and up. In this step by step guide we will install that version and integrate it with Drupal 7  using Search API module.

All we need to do is to configure the pre-setup Search API Solr server and index. The good thing about using Search API is that it is already integrated with Views module and we can do unlimited customization with our search results.

The Solr version 5.x is now a stand alone server (thank God), it already comes with Java web server. If we look at Solr version 4.x and below, they need to install Tomcat or Jetty in order to run as a server. Our installation in version 5.x is by far much easier.

 

Install Java

First things first. We need Java. Solr is a Java application and in order to run it we need to install Java in our system.

First we check out if Java is already installed in our machine by issuing the following command:
which java

If Java is not installed, check the available versions of Java and install it:

yum list available java*
yum install java-1.8.0-openjdk.x86_64

Then verify if it is already installed:

which java
java -version

 

You should now have Java 8 installed and see something like that:

[[email protected] ~]# java -version
openjdk version "1.8.0_51"
OpenJDK Runtime Environment (build 1.8.0_51-b16)
OpenJDK 64-Bit Server VM (build 25.51-b03, mixed mode)
[[email protected] ~]#

 

 

Seup Solr server

Solr require system user named “solr” and we will create one:

adduser solr

 

Download and setup Solr server:

cd /opt
curl -O http://www.trieuvan.com/apache/lucene/solr/5.3.0/solr-5.3.0.tgz
tar zxvf solr-5.3.0.tgz
cp /opt/solr-5.3.0/bin/install_solr_service.sh .
rm -rf solr-5.3.0
./install_solr_service.sh solr-5.3.0.tgz
chown -R solr:solr /opt/solr*
chown -R solr:solr /var/solr*

 

Make the Solr server auto-run when the machine reboot or shutdown:

chkconfig --add solr

chkconfig | grep solr

 

Create Solr core:

Solr core is a running Lucene index instance with the Solr configuration. It is ideal to create one for each Drupal site (if  you are running multi-site).

 

Create Solr core instance named “nixpal” (or whatever you like just remember the name):

cd /opt/solr/bin

./solr create -c nixpal

 

I assume you did that with root, so you need to change ownership again just in case.

chmod -R solr:solr /var/solr/

If you get this error:

“Failed to create core ‘nixpal’ due to: Error CREATEing SolrCore ‘nixpal’: Unable to create core [nixpal] Caused by: /var/solr/data/nixpal/data”

Issue this command again:

./solr create -c nixpal

And of course change ownership again.

chmod -R solr:solr /var/solr/

 

 

Installing schema file and solrconfig file from Drupal.

Download search_api_solr module, untar and copy the Drupal specific Solr 5.x core configurations to overwrite the existing configuration files in the created core instance:

I use version 1.9. Check for newer version of course if you can.

curl -O http://ftp.drupal.org/files/projects/search_api_solr-7.x-1.9.tar.gz
tar zxvf search_api_solr-7.x-1.9.tar.gz

cp search_api_solr/solr-conf/5.x/* /var/solr/data/nixpal/conf

sudo chown -R solr:solr /var/solr/data/nixpal
rm -rf search_api_solr-7.x-1.9.tar.gz search_api_solr

 

I’ve search hours for that. Anyway, in case you need ONLY the files (schema.xml / solrconfig.xml, extra settings, etc) you can find them also here ( http://cgit.drupalcode.org/search_api_solr/tree/solr-conf?id=7.x-1.7 ) for ALL Solr versions (3/4/5). Handy to have that link around.

 

Security

Add your server’s IP address to whitelist as well as your personal computer’s network IP address (in able to access the Solr admin web interface http://www.mywebsite.com:8983/solr).

Wrap the item ‘<New id=”Contexts” class=”org.eclipse.jetty.server.handler.ContextHandlerCollection”/>’ with an IPAccessHandler:

vi /opt/solr/server/etc/jetty.xml

search and replace the ‘<New id=”Contexts” class=”org.eclipse.jetty.server.handler.ContextHandlerCollection”/>’ with these lines:

<!-- IP Authentication -->

<New class="org.eclipse.jetty.server.handler.IPAccessHandler">

<Call name="addWhite">

<!-- list of args with ip-addresses -->

<Arg>127.0.0.1</Arg>

</Call>

<Call name="addWhite">

<!-- list of args with ip-addresses -->

<Arg>xxx.xxx.xxx.xxx</Arg>

</Call>

<Set name="handler">

<New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>

</Set>

</New>

<!-- END Of IP Authentication -->

 

 

Finally it should be something like this (parsing a few extra lines from top and bottom)

<!-- =========================================================== -->
<!-- Set handler Collection Structure -->
<!-- =========================================================== -->
<Set name="handler">
<New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<Ref id="RewriteHandler"/>
</Item>
<Item>

<!-- <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/> -->


<!-- ip auth -->
<New class="org.eclipse.jetty.server.handler.IPAccessHandler">

<Call name="addWhite">
<!-- list of args with ip-addresses -->
<Arg>127.0.0.1</Arg>
</Call>

<!-- Chris Dynamic IP  -->
<Call name="addWhite">
<!-- list of args with ip-addresses -->
<Arg>extra.ip.here.too</Arg>
</Call>

<Call name="addWhite">
<Arg>another.ip.here</Arg>
</Call>


<Set name="handler">
<New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
</Set>
</New>
<!-- end of ip auth -->


</Item>
<Item>
<New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>

 

Now you can start your server:

systemctl start solr.service 

 

or

service solr start

 

(because I hate the systemd way)

 

You got Apache SOLR installed and running with a core named nixpal.

So your URL should be something like:

http://www.nixpal.com:8983/solr/nixpal

Happy searching / indexing !

 

2 Replies to “Setting up Apache Solr 5 on Centos for use with Drupal”

Leave a Reply