Most of high traffic or complex Drupal sites use Apache Solr as the search engine. It is much faster and more scaleable than Drupal's search module.
In a previous article on Drupal with Apache Solr 4.x, we described one way to install the latest stable Apache Solr 4.x. That article detailed a lot of manual steps involving downloading, extracting, setting permissions, creating a startup script, ...etc.
In this article, we describe another way for having a working Apache Solr installation for use with Drupal 7.x, on Ubunutu Server 12.04 LTS, as well as Ubuntu Server 14.04 LTS. This article uses an older version of Solr, 1.4 on the former, and 3.6 on the latter. But the methodology described uses the tried an tested apt dependency management system for Ubuntu, and other Debian based systems.
For this article, we focus on having an installation of Apache Solr with the following objectives:
- Use the version of Apache Solr in Ubuntu's repositories, therefore no searching for software, or dependencies, start/stop scripts, ...etc.
- Least amount of software dependencies, e.g. no need for 'heavy' components, such as a full Tomcat server
- Least amount of necessary complexity
- Least amount of software to install and maintain
- A secure installation
This installation can be done on the same host that runs Drupal, if it has enough memory and CPU, or it can be on a separate server dedicated for search, which is the preferred approach.
We start by installing the Java Development Kit, whatever the default is for our version of the distro. This is guaranteed to work with Solr, using the Debian dependency magic.
sudo aptitude update
sudo aptitude install solr-jetty default-jdk
Now, edit the file /etc/default/jetty, and change the following lines as follows:
For Ubuntu 12.04, also add the following line:
For Ubuntu 14.04, also add the following line:
Note that the JAVA_HOME line is dependent on your CPU architecture, and the JDK version that gets installed automatically by default, from the repositories. So if the above does not work, check what directories are installed under the directory /usr/lib/jvm, and choose a suitable version.
You can find out which JVM version and architecture is installed on your server by using the following command:
ls -l /usr/lib/jvm
Copying the Drupal schema and Solr configuration
We now have to copy the Drupal Solr configuration into Solr. Assuming your site is installed in /var/www, these commands achieve the tasks:
For Ubunutu 12.04, which installs Solr 1.4, use the following:
sudo cp /var/www/sites/all/modules/contrib/apachesolr/solr-conf/solr-1.4/* /etc/solr/conf
For Ubunutu 14.04, which installs Solr 3.6, use the following:
sudo cp /var/www/sites/all/modules/contrib/apachesolr/solr-conf/solr-3.x/* /etc/solr/conf
Then edit the file /etc/solr/conf/solrconfig.xml, and uncomment the following line:
Setting Apache Solr Authentication, using Jetty
By default, a Solr installation using Jetty, does not start at all, unless you configure /etc/default/jetty as above. The values above tells jetty to listen on the public Ethernet interface of a server, but has no protection whatsoever. Attackers can access Solr, and change its settings remotely. To prevent this, we set password authentication for Jetty, which in turn protest Solr.
Note: The following syntax is for Apache Solr 1.4 and 3.6 which depend on Jetty 6. Solr 4.x depends on Jetty 8, and use a different syntax described in our article linked above.
The following settings work well for a single core install, i.e. search for a single Drupal installation. If you want multi-core Solr, i.e. for many sites, then you want to fine tune this to add different roles to different cores.
First, edit the file: /etc/jetty/jetty.xml, and add this section before the last line:
<!-- ======= Securing Solr ===== -->
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
Then edit the file: /etc/jetty/webdefault.xml, and add this section, also before the last line:
Finally, edit the file: /etc/jetty/realm.properties, and comment out all the existing lines, then add this line:
user: password, search-role
Note that "search-role" must match what you put in webdefault.xml above.
You can change "user" and "password" to suitable values.
Finally, make sure that the file containing passwords is not readable to anyone but the owner.
chmod 640 /opt/solr/example/etc/realm.properties
To start Solr, you need to start Jetty, which runs Solr for you automatically.
service jetty start
Now Solr is up and running.
Verify that it is running by accessing the following URL, where you can check the progress of indexing once you start it (see below):
Replace x.x.x.x by the IP address of the server that is running Solr, or its fully qualified domain name.
Viewing the logs
You can view the logs at:
tail -f /var/log/jetty/*.log
Configuring Drupal's Apache Solr module
After you have successfully installed, configured and started Solr, you should configure your Drupal site to interact with the Solr seserver. First, go to this URL: admin/config/search/apachesolr/settings/solr/edit, and enter the information for your Solr server. You should use the URL as follows:
Now you can proceed to reindex your site, by sending all the content to Solr.
If you ever want to cleanly remove Apache Solr that you installed from the server using the above instructions, then use the sequence of the commands below:
sudo aptitude purge default-jdk solr-jetty
sudo rm -rf /var/lib/solr/ /etc/jetty /etc/solr