Install Solr 6 for Drupal 8 on Ubuntu 16.04

Submitted by christophe on Wed, 26/10/2016 - 21:55
Solr logo



Here is an update for Solr 7, Search API 2.x

This setup is intended for production use on a dedicated server (VPS, ...).
For development purpose it should be preferable to use Drupal VM, it comes with a default Solr configuration that can be enabled via the config.yml file. If you are using Drupal VM and want to configure Search API for Solr, you can jump to point 4.

1. Install Search API and Search API Solr modules

Make sure that you download the modules via Composer and not Drush, otherwise you will end up with missing dependencies (like Solarium).

# cd in your Drupal docroot first
composer config repositories.drupal composer https://packagist.drupal-composer.org/8
composer require drupal/search_api_solr

2. Install Solr

Update your system

sudo apt update && sudo apt upgrade -y

Install Java 8

sudo apt install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt update
sudo apt install oracle-java8-installer

# check if Java is installed properly, should return java version "1.8.x_yy"
java -version

Install Solr

cd ~
# get the latest 6.x release via https://lucene.apache.org/solr/mirrors-solr-latest-redir.html
wget http://archive.apache.org/dist/lucene/solr/6.6.2/solr-6.6.2.tgz
# extract the service installation file
tar xzf solr-6.6.2.tgz solr-6.6.2/bin/install_solr_service.sh --strip-components=2
# install Solr as a service
sudo ./install_solr_service.sh solr-6.6.2.tgz

# check the service status
service solr status

Create a collection (also known as "core")

sudo su - solr -c "/opt/solr/bin/solr create -c my_collection -n data_driven_schema_configs"

The Solr interface that allows to browse collections, should be accessible via the :8983 port (e.g. http://xxx.xxx.xxx.xxx:8983/solr/), make sure that the access is restricted (e.g. by a firewall for your IP only or a basic authentication mechanism).

3. Restrict access to the Apache Solr admin interface

Various methods can be used to restrict access, the most generic one is to use iptables, it will limit access to the port to specifig IP's (e.g. if not using a VPN), including localhost.

sudo iptables -A INPUT -p tcp -s localhost --dport 8983 -j ACCEPT
sudo iptables -A INPUT -p tcp -s YOUR_STATIC_IP --dport 8983 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 8983 -j DROP

This will define IP rules until the next reboot.
To make them peristent, use the generic iptables-save or netfilter-persistent (on Ubuntu 16.04)

sudo netfilter-persistent save
sudo netfilter-persistent reload

4. Configure Search API

Set the valid schema for the collection

# if not done yet (if you skipped the first sections), create your collection
sudo su - solr -c "/opt/solr/bin/solr create -c my_collection -n data_driven_schema_configs"
# replace my_collection by your own collection name
cd /var/solr/data/my_collection
# backup the configuration directory
mv conf conf.orig
# copy the Search API configuration
cp -R /path/to/contrib_modules/search_api_solr/solr-conf/6.x conf
# change owner and group
chown -R solr:solr conf
# change permissions
chmod -R 775 conf
# restart Solr service
service solr restart

Enable the Search API Solr module + the Solr Search Defaults module, that comes with an example default content index configuration.

# cd in your docroot then
drush en search_api_solr search_api_solr_defaults
# or better, if using Composer template for Drupal, having Drush that is not installed globally
# ../vendor/bin/drush en search_api_solr search_api_solr_defaults

Go to the Search API configuration (/admin/config/search/search-api), add your Solr server, setting the name of your collection under the Solr core field. Other values can be left as is in this basic configuration.
You can then continue by setting up the default index (under /admin/config/search/search-api/index/default_index/edit), activate the index then index your content.

Resources

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.