Thursday, October 24, 2013

Installing Solr 4.5 as a Windows service

I've recently had the good fortune to use the Solr search platform for a project. One thing, though, that is not straight forward is how to install Solr as a Windows service. There are several articles on the internet, but I couldn't find any that worked with Solr 4.5. So I gathered information from a number of out-dated articles and came up with the following approach, which has worked fine for me:

  1. Download Solr 4.5 (http://lucene.apache.org/solr/)
  2. Download the Non-Sucking Service Manager NSSM (http://nssm.cc/)
  3. Create a folder to be used for the Solr service (e.g. D:\solr). From now on this will be called <solrdir>
  4. From the example folder of the Solr package copy the following files and folders to <solrdir>:
    1. etc
    2. lib
    3. logs
    4. solr
    5. webapps
    6. start.jar
  5. In a command prompt go to <solrdir> and run the command java -jar start.jar to check that the Solr installation works. If it works then just stop it again.
  6. From the NSSM package copy nssm.exe to <solrdir>
  7. In a command prompt go to <solrdir> and run the command below. Note: it may be necessary to replace back-slashes in <solrdir> with forward-slashes.
    nssm.exe install Solr C:\Windows\System32\java.exe "-Dsolr.solr.home=<solrdir>/solr -Djetty.home=<solrdir>/ -Djetty.logs=<solrdir>/logs/ -cp <solrdir>/lib/*.jar;<solrdir>/start.jar -jar <solrdir>/start.jar"
To remove the service run the following command:
nssm.exe remove Solr

For convenience I've created the following batch script, which performs the nssm.exe install part. The script should be placed in <solrdir>.
@echo off
set currentdir=%~dp0
set parameters=-Dsolr.solr.home=""%currentdir%solr"" -Djetty.home=""%currentdir%"" -Djetty.logs=""%currentdir%logs/"" -cp ""%currentdir%lib/*.jar;%currentdir%start.jar"" -jar ""%currentdir%start.jar""

REM replace back-slashes with forward-slashes
set javaWeirdnessParameters=%parameters:\=/%

nssm.exe install Solr C:\Windows\System32\java.exe "%javaWeirdnessParameters%"