Setting up Apache Solr on Windows as a service

I had to set up Apache Solr 4 on Windows as a service using Jetty container. The following is the documentation on how to do it. I am not saying that this is the best way to get it to work. But it is one way that works and seems to be more recent and more comprehensive than the other approaches I found.

Initial setup

  1. Ensure Java version 1.6 or higher is present on the machine. If not, download it from http://www.java.com/ (Java 7 is actually also known as 1.7).
  2. Download latest Solr (here Solr 4.3.1) and unpack it somewhere.
  3. Create a directory where your Solr configuration and Jetty will live (e.g. C:\Services\SolrService, which we will refer to as <_SOLRDIR_>).
  4. If you don’t have Solr configuration defined yet, you can just copy Solr’s own example\solr directory to <_SOLRDIR_>, such that solr.xml is just inside <_SOLRDIR_> directory.
  5. Under the <_SOLRDIR_>, create subdirectory called jetty.
  6. Copy the following directories and the files into the jetty subdirectory from the Solr’s example directory:
    • contexts
    • etc
    • lib
    • logs
    • resources (Solr 4.3+)
    • webapps
    • start.jar
  7. From the command line in the jetty directory, run the Solr server to make sure it works:
    1. java -Dsolr.solr.home=.. -jar start.jar
    2. Ensure that Solr is accessible at: http://localhost:8983/solr/
    3. Stop the server (Ctrl-C)

Service setup for availability

  1. Download Apache Commons’ Daemon package for Windows (make sure to get the Windows package that includes .exe files).
  2. Copy platform-appropriate prunsrv.exe into the jetty directory and rename it to SolrService.exe – this is the actual Windows service executable.
  3. Copy prunmgr.exe into a new subdirectory jetty/serviceui and rename it also to SolrService.exe (to provide default service name) – this is the UI that allows to correct some of the parameters of the service after it had been setup via the command line.
  4. Make sure that computer’s environmental variables have JAVA_HOME defined for system variables, not just for user’s (run sysdm.cpl as Administrator). The variable value should look something like C:\Progra~1\Java\jre7 . Otherwise, attempting to start the service will produce the following message in the daemon/service logs: Unable to find Java Runtime Environment.
  5. Run the service registration (with renamed prunsrv) as the Administrator on the command prompt after replacing <_SOLRDIR_> with the actual path to the Solr installation as setup above.  The command below should be all on a single line (with spaces instead of line breaks):
     
    SolrService.exe //IS//SolrService
    --DisplayName="Solr Service"
    --Install=<_SOLRDIR_>\jetty\SolrService.exe
    --LogPath=<_SOLRDIR_>\jetty\logs
    --LogLevel=Debug
    --StdOutput=auto
    --StdError=auto
    --StartMode=java
    --StopMode=java
    --Jvm=auto
    ++JvmOptions=-Djetty.home=<_SOLRDIR_>\jetty
    ++JvmOptions=-DSTOP.PORT=8087
    ++JvmOptions=-DSTOP.KEY=stopsolr
    ++JvmOptions=-Djetty.logs=<_SOLRDIR_>\jetty\logs
    ++JvmOptions=-Dorg.eclipse.jetty.util.log.SOURCE=true
    ++JvmOptions=-XX:MaxPermSize=128M
    --Classpath=<_SOLRDIR_>\jetty\start.jar
    --StartClass=org.eclipse.jetty.start.Main
    ++StartParams=OPTION=ALL
    ++StartParams=<_SOLRDIR_>\jetty\etc\jetty.xml
    --StopClass=org.eclipse.jetty.start.Main
    ++StopParams=--stop
    ++JvmOptions=-Dsolr.solr.home=<_SOLRDIR_>
    --StartPath=<_SOLRDIR_>\jetty
  6. Run the service:
    1. SolrService.exe start
    2. Check the log files in the log subdirectory (usually commons-daemon.<date>.log) for messages.
    3. If there is an error message, run serviceui\SolrService.exe and try to correct possible problems. An account that runs the service may need to switch to one with full access to the directories (e.g. Administrator).
    4. Run SolrService.exe stop to stop the service and try starting again. If error log shows problems with stopping and the Services control panel shows the service as ‘starting’, see the troubleshooting section below on how to kill a run-away service.
    5. The success criteria here is being able to access Solr’ Web Admin UI from http://localhost:8983/solr (again).
  7. In the Services control panel, change status of the service Solr Service from manual to automatic to ensure it survives restart. Try stopping and starting the service from the Services control panel to test that it works. Use Solr’ Web Admin UI to verify that it has stopped/started correctly.

Updating Solr configuration

  1. Update would usually involve changes to schema.xml and – much more rarely – to solrconfig.xml .
  2. Most of the time, the core can be reloaded from the Solr’s Web Admin UI to account for the new configuration.
  3. If the changes are significant (e.g. new collection or some deep server settings), the service itself may need to be restarted.

Upgrading Solr

  1. Download and unpack the latest 4.x version of Solr.
  2. Stop Solr service.
  3. Delete and re-copy the directories into jetty subdirectory as per the initial-installation instructions above, with two exceptions:
    1. Keep logs directory, which contains production logs.
    2. Delete solr-webapp directory, which is an expanded version of – now obsolete - webapps/solr.war. If this is not done, you will get version mismatch between old and new content. The solr-webapp directory will be regenerated on the next Jetty restart.
  4. If you need to update schema or config, do it now while the service is down.
  5. Restart the service.
  6. If you are running some persistent client, you may need to restart that as well.
  7. Test from Web Admin UI interface that the service is running.

 Troubleshooting service setup

If a service is misconfigured, Java runtime not found or something else goes wrong, you may get a run-away service that refuses to start and stop properly from the control panel. In which case, you may need to use command line tools (possibly as Administrator) to find and kill the stuck service:

  1. sc queryex
  2. taskkill /PID <PID> /F

In here, <PID> is the process ID for the solr service that is stuck.


If you enjoyed this article, you may also like my Solr book: Instant Apache Solr for Indexing Data How-to.

You may also benefit from other information resources available at solr-start.com.

 

1 comment to Setting up Apache Solr on Windows as a service

  • Sarah

    Hi there, I love your instructions and I will be using them and great to know there is a book available too. I have eclipse already (latest version) and nutch on my laptop, however every single download for apache solr for windows i have tried so far downloads and then when i go to unzip it it says it is an empty file or does not work at all. I even tried the bitnami solr installer and that downloaded too but when I went to run it I got an error saying it would not run. REally lost right now and getting quite frustrated. I tried your link too but again it says it has a long time to download, then all of a sudden a few minutes later it says it is complete – i go to unzip it and get told it is an empty file. not sure what I am doing wrong .