Configure Apache Solr 8.x for Drupal 10 on Ubuntu

Last modified
Thursday, May 30, 2024 - 14:55

Hi, on this post I'll go step by step into the process on how to configure Apache Solr for Drupal 10 so your sites can have a robust search engine based on indexes specially targeted for big sites that handle tons of data. Also, the Ubuntu version I'll be running is 22.04 since it's the release that has PHP 8 required by Drupal 10, you can see how to configure Drupal 10 under Ubuntu 22.04 on this link.

Apache Solr is a Java application that works as a search engine based on indexes, pieces of data that can by queried super fast providing search results in a very optimal, reliable and again fast way without overloading your Drupal database which will incur into slowing down your site response speed on any request. Great thing about Solr is that is fully compatible with Drupal through the Search API module and the Search API Solr module so you can be able to index your site data to Solr and query the same data using Views making this approach very strong and easy to use.

As one of the first steps, I always recommend to disable the core's Search module if your plan is to use the Search API either with Database indexation or Solr integration since these are way stronger compared to the base search functionality.

Ok, let's begin.

First we need to download and install the Search API and the Search API Solr modules, for this we will be using Composer. Go to the root folder of your project and run:

# Install Search API
$ composer require 'drupal/search_api:^1.34'

# Install Search API Solr
$ composer require 'drupal/search_api_solr:^4.3'

Once the packages are downloaded by Composer, let's enable them using drush again on the root folder of your project, run:

# Enable Search API and Search API Solr.
$ drush en search_api search_api_solr search_api_solr_admin

# Clear caches and run cron.
$ drush cr && drush cron

Alright, now we have enabled the required modules, we can proceed with the initial configuration.
Under Configuration, you'll find the Search API config link that will take you to the Search API config page /admin/config/search/search-api, here we will click on the Add New Server button

Add Server Index

 

This will redirect you to the New Server configuration page where we will be adding some information required to set up a Solr Instance or Server.

As for Server Name, give it a proper name as you wish just keep in mind we will be working with the machine name of it, in the case of this demo I'll be using the name Solr Server which its machine name will be solr_server.

Next, check the Solr radio button under Backend. This will automatically display the Solr specific options to configure the connection between Drupal and the Solr Server the we will be configuring after finishing this set up, its not really necessary to configure Solr first.

As the last step for this configuration you just have to fill in the following and leave the rest of configurations as defaults. Since we are going to by running Solr on the same server as Drupal the server will be your localhost. Set the following:

Under Configure Solr Backend -> Solr Connector Check the Standard Radio button.

Under Configure Solar Backend -> Configure Solr Standard Connector set the following info:

HTTP Protocol: HTTP (default)
Server Name: localhost (default)
Solr Port: 8983 (default)
Solr Path: / (default)
Solr core: my_solr_core_name (Required, set a proper name for your core, I'll be using as name drupal_test_core for this demo)

And that is all, we'll leave the rest of options as default. Save your changes. 

After the Options are saved you'll be redirected to the main Search API page with an error saying that the Server cannot be reached but don't worry, once Solr is installed on your system you'll only have to refresh the page and the connection will appear working as expected.

Ok, let's start with Solr. I'll be using the latest Solr 8.x provided on the Apache Solr page and the 8th version of it since it's the one Pantheon is currently working with so in terms of compatibility with the Pantheon Cloud platform ( I believe Acquia is using the same version as well ) I consider is the way to go, plus is fully supported by the Search API Solr module. By the time of this post I'll be using the latest 8.x version which is 8.11.2.

First we need to make sure we have openjdk, on your home folder, run:

# Make sure you are in your home folder:
$ cd ~/

# Check if openjdk packages are available, if not this will installed them
$ sudo apt-get install default-jdk -y

Download Apache Solr from the Apache Solr site archives repository, got to you home into a terminal window and run:

# Make sure you are in your home folder:
$ cd ~/

# Download Apache Solr
$ https://archive.apache.org/dist/lucene/solr/8.11.2/solr-8.11.2.tgz

Once the download is complete, extract the downloaded file using the following command:

# Extract Solr
$ tar -xvzf solr-8.11.2.tgz

Now that we have the files extracted, we need to run the installation script:

# Make sure you are in your home folder:
$ cd ~/

# Install Solr 
$ sudo ./solr-8.11.2/bin/install_solr_service.sh solr-8.11.2.tgz

Once installed, you'll see something similar to the next screenshot:

Solr Installation Complete

 

At this point, you should have Solr running on your system, you can now go to http://localhost:8983/ and see the Solr admin interface working:

 

Solr UI

 

So far so good, Ok now we will need to configure what is called a Collection for our Drupal site, run these steps each time you need to create an Index for different projects just make sure to change the Collection name per project.

# Create new generic Solr Collection
$ sudo su - solr -c "/opt/solr/bin/solr create -c drupal_test_core -n data_driven_schema_configs"

If all went well you should get something similar to the next screenshot:

Solr Collection created

Now that we have the generic Collection created, let's grab the specific Solr config files provided by Drupal using Drush. Follow the next steps:

# Move to the root folder of your project and run:
$ cd path_to_my_project_root/
$ drush solr-gsc solr_server config.zip 8.11.2

The command above will download a file called config.zip into the web/ folder of your project, we are going to move it to our Home folder and extract it there.

# Move config.zip to $HOME
$ mv /path/to/my_project/web/config.zip ~/

# Change directory to $HOME
$ cd ~/

# Create a container folder.
$ mkdir solr-conf/

# Extract files
$ unzip config.zip -d solr-conf/

Now the have the specific files for your project we need to clear the conf/ directory into our collection and move the files in there, run:

# Delete generic files from Collection
$ sudo su - solr -c "rm -rf /var/solr/data/my_collection_name/conf/*" (Replace my_collection_name with the name you created)

# Copy Drupal's specifics to the same directory above:
$ sudo cp  ~/solr-conf/* /var/solr/data/my_collection_name/conf

Make sure the new Collection has the proper permissions, run:

# Change/Update Permissions of the Collection
$ sudo chmod -R 0775 /var/solr/data/my_collection_name/
$ sudo chown -R solr:solr /var/solr/data/my_collection_name/

Finally, restart the Solr service:

# Restart Solr Service
$ sudo systemctl restart solr.service

And now you should see in your Solr Server UI on the left column the newly created Collection as shown in the next screenshot:

Solr Collection Running

 

NOTE: I'm using this approach since the once described on the Search API Solr documentation doesn't work for the Standard configuration of a new core, I'm always getting an Specified configuration directory error when running the command stated in the docs.

And that is all! Go back to Drupal to the Search API UI page and select the Server you previously created and you should see it connected without errors, something similar to the next screenshot:

 

Drupal Solr Server

From here, you just need to add a New Index to your Server where you can choose the different options of the type of content you desire.

PRO TIP: When working existing projects that are targeting specific Solr Servers you might run into issues when importing the db configurations, so if you have config overrides working per environment you should be Ok but if not what i always do is override my local configs into the settings.local.php file with the following:

/**
 * Apache Solr Specifics for local Environments.
 * Override Pantheon or others.
 * see @https://www.drupal.org/project/search_api_solr/issues/2796407
 *
 * Connector stays the same so don't get scared.
 * $config['search_api.server.SERVERNAME'] override MY_SERVER_NAME accordingly.
 * 'host' => '192.168.10.35' Solr server IP OR it can be localhost (127.0.0.1) depends on your local or env config.
 * the rest are connection parameters options to desired Solr server above.
*/

$config['search_api.server.My_SERVER_NAME'] = [
  'backend_config' => [
    'connector_config' => [
      'host' => 'localhost',
      'path' => '/solr',
      'core' => 'my_collection_name',
      'port' => '8983',
    ],
  ],
];

Perfect! now that you have Solr integrated with Drupal you can start creating Views to display search data indexed in your Solr Server. Hope you enjoy and please leave your comments bellow if you have any questions or suggestions.

 

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.