Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughConfiguring Apache Solr 3.6 for Drupal on Ubuntu 14.04, with password authentication
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.
Objectives
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.
Installing Java
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
Configuring Jetty
Now, edit the file /etc/default/jetty, and change the following lines as follows:
NO_START=0
JETTY_HOST=0.0.0.0
JETTY_PORT=8983
For Ubuntu 12.04, also add the following line:
JAVA_HOME=/usr/lib/jvm/java-6-openjdk-amd64
For Ubuntu 14.04, also add the following line:
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
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:
<dataDir>${solr.data.dir:./solr/data}</dataDir>
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="UserRealms">
<Array type="org.mortbay.jetty.security.UserRealm">
<Item>
<New class="org.mortbay.jetty.security.HashUserRealm">
<Set name="name">Solr</Set>
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
</New>
</Item>
</Array>
</Set>
Then edit the file: /etc/jetty/webdefault.xml, and add this section, also before the last line:
<security-constraint>
<web-resource-collection>
<web-resource-name>Solr</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>search-role</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Solr</realm-name>
</login-config>
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
Starting Solr
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):
http://x.x.x.x:8983/solr/admin/stats.jsp
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:
http://user:[email protected]:8983/solr/
Now you can proceed to reindex your site, by sending all the content to Solr.
Removing 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
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web