Skip to main content

Install Solr 5.x for Search Api Solr using Ansible

Drupal ver.: 
7.x
Category: 
DevOps
Tutorial
Untitled Document.md

As stated at official site, Solr is:

the popular, blazing-fast, open source enterprise search platform built on Apache Lucene™.

In order to use its features on Drupal site, we have to install Search Api Solr module and get Solr up & ready.
At the moment of writing this article, latest stable version, supportable by Search Api Solr is 5.4.1. Which is we going to install.
What makes this task much easier is Ansible and its roles, contributed by other developers.

If you don’t have Ansible installed, check out official docs.

Current instance, where we’ll be installing Solr is Ubuntu 16.04.

1. Download Geerlingguy’s Ansible roles solr & java.

  • In order to do this, we have to create folder, which will contain our ansible playbook. Let’s name it solr-ansible. Then make requirements.yml file with next contents:

      ---
      - src: geerlingguy.java    
      - src: geerlingguy.solr
  • Run next command:
    ansible-galaxy install --roles-path . -r requirements.yml
    Which, basically, tells ansible to download latest versions of listed above roles into the current directory.

2. Make playbook.

  • Create file, named main.yml which should contain main instructions for ansible.
    Contents of the file:

      ---
      - name: Install solr
        hosts: localhost
        become: yes
        become_method: sudo
     
        vars:
          solr_version: 5.4.1
          solr_cores:
            - main_core
            - second_core
     
        roles:
          - geerlingguy.java
          - geerlingguy.solr

name is the name for the playbook.
hosts tells Ansible on which machines this playbook should be ran.

vars contains variables, that will be used in roles.
solr_cores - here you can enlist cores, that you want to install.

3. Run playbook.

  • Run next command:

      ansible-install main.yml -vvv

Solr will be installed in /opt/solr folder.
Cores main_core and second_core can be found at /var/solr/data.

4. Apply configs.

  • Locate folder which contains Search Api Solr module. There’s subfolder solr-conf, that contains configs for different Sol versions. Because we’re using 5.4.1 version, we have to copy contents of 5.x subfolder of solr-conf to /var/solr/data/main_core/conf with overwriting contents. We’re going to configure only main_core core.

  • Contents of solrcore.properties should be placed within core.properties file, which can be found at /var/solr/data/main_core folder. Make sure that next variables have mentioned values:

      solr.contrib.dir=/opt/solr/contrib
      solr.luceneMatchVersion=5.4.1
  • If you want to use file extractors (extract text from pdf files, e.g.), make sure to replace following line:

      <!--<lib dir="../../dist/" regex="apache-solr-cell-\d.*\.jar" />-->

    with this one:

      <lib dir="${solr.contrib.dir:../../../contrib}/../dist/" regex="solr-cell-\d.*\.jar" />

inside solrconfig.xml file.

  • Restart Solr:

      sudo service solr restart

To make sure cores are running, we can go to: Solr dashboard

5. Enable Basic http authentication.

This is optional step, because secure connection could be established via SSL. Or by any other means.

  • Navigate to /opt/solr/server/etc.

  • Put next code at the end of jetty.xml file before </Configure> tag:

    <!-- Configure Basic http authentication -->
    <Call name="addBean">
      <Arg>
        <New class="org.eclipse.jetty.security.HashLoginService">
          <Set name="name">Solr Realm</Set>
          <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
          <Set name="refreshInterval">0</Set>
        </New>
      </Arg>
    </Call>
  • Put next code at the end of webdefault.xml before </web-app>:

      <security-constraint>
        <web-resource-collection>
          <web-resource-name>Solr authenticated application</web-resource-name>
          <url-pattern>/</url-pattern>
        </web-resource-collection>
        <auth-constraint>
          <role-name>admin-role</role-name>
        </auth-constraint>
      </security-constraint>
     
      <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>Solr Realm</realm-name>
      </login-config>
  • Create file realm.properties in the current directory with next contents:

      admin: admin, admin-role

    User name: admin
    User password: admin
    Role: admin-role. Note: should be the same as the value within <role-name> tag from snippet above.

  • Restart Solr:

      sudo service solr restart

Right now in order to go to Solr dashboard, we have to enter user name & user password, matching ones in realm.properties file.

Leave a Comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.