Sometimes you need to choose a different servlet container other than Jetty. Maybe it's because your client has other applications running on another servlet container, maybe it's because you just don't like Jetty. Whatever your requirements are that put Jetty out of the scope of your interest, the first thing that comes to mind is a popular and powerful servlet container—Apache Tomcat. This recipe will give you an idea of how to properly set up and run Solr in the Apache Tomcat environment.
First of all, we need an Apache Tomcat servlet container. It can be found at the Apache Tomcat website—http://tomcat.apache.org. I concentrated on Tomcat version 6.x because of two things—version 5 is pretty old right now, and version 7 is the opposite—it's too young in my opinion. That is why I decided to show you how to deploy Solr on Tomcat version 6.0.29, which was the newest one while writing this book.
To run Solr on Apache Tomcat, we need to perform the following six simple steps:
Firstly, you need to install Apache Tomcat. The Tomcat installation is beyond the scope of this book, so we will assume that you have already installed this servlet container in the directory specified by the
$TOMCAT_HOME
system variable.The next step is preparing the Apache Tomcat configuration files. To do that, we need to add the following inscription to the connector definition in the
server.xml
configuration file:URIEncoding="UTF-8"
The portion of the modified
server.xml
file should look like this:<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
Create a proper context file. To do that, create a
solr.xml
file in the$TOMCAT_HOME/conf/Catalina/localhost
directory. The contents of the file should look like this:<Context path="/solr"> <Environment name="solr/home" type="java.lang.String" value="/home/solr/configuration/" override="true"/> </Context>
The next thing is the Solr deployment. To do that, we need a
solr.war
file that contains the necessary files and libraries to run Solr to be copied to the Tomcatwebapps
directory. If you need some additional libraries for Solr to see, you should add them to the$TOMCAT_HOME/lib
directory.The last thing we need to do is add the Solr configuration files. The files that you need to copy are files like
schema.xml
,solrconfig.xml
, and so on. Those files should be placed in the directory specified by thesolr/home
variable (in our case,/home/solr/configuration/
). Please don't forget that you need to ensure the proper directory structure. If you are not familiar with the Solr directory structure, please take a look at the example deployment that is provided with standard Solr package.Now we can start the servlet container by running the following command:
bin/catalina.sh start
In the log file, you should see a message like this:
Info: Server startup in 3097 ms
To ensure that Solr is running properly, you can run a browser, and point it to an address where Solr should be visible, like the following: http://localhost:8080/solr/
.
If you see the page with links to administration pages of each of the cores defined, that means that your Solr is up and running.
Let's start from the second step, as the installation part is beyond the scope of this book. As you probably know, Solr uses UTF-8 file encoding. That means that we need to ensure that Apache Tomcat will be informed that all requests and responses made should use that encoding. To do that, we modify the server.xml
in the way shown in the example.
The Catalina context file (called solr.xml
in our example) says that our Solr application will be available under the /solr
context (the path
attribute), the war file will be placed in the /home/tomcat/webapps/
directory. solr/home
is also defined, and that is where we need to put our Solr configuration files. The shell command that is shown starts Apache Tomcat. I think that I should mention some other options of catalina.sh
(or catalina.bat
) script:
stop
—stops Apache Tomcatrestart
—restarts Apache Tomcatdebug
—start Apache Tomcat in debug mode
After running the example address in a web browser, you should see a Solr front page with a core (or cores if you have a multicore deployment). Congratulations, you have just successfully configured and ran the Apache Tomcat servlet container with Solr deployed.
Here are some other tasks that are common problems when running Solr on Apache Tomcat.
Sometimes it is necessary to run Apache Tomcat on a port other than the 8080 default one. To do that, you need to modify the port
variable of the connector definition in the server.xml
file located in the $TOMCAT_HOME/conf
directory. If you would like your Tomcat to run on port 9999, this definition should look like this:
<Connector port="9999" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />
While the original definition looks like this:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />