Home
Guide to Installing Apache Solr 4.9, Tomcat 7 on RHEL 7
Goals
- Serve up Apache Solr on port 80 via Tomcat
- Leave Tomcat running on port 8080 and forward traffic to it from port 80.
- Ensure Tomcat starts at server startup.
Background
Recently, I needed to install Apache Solr 4.9.0 and Apache Tomcat 7 on a RHEL7 server. I’m posting the details of how I set it up because someone may benefit from them, for two primary reasons: 1) RHEL does not have the latest and greatest packages (by design), unlike its Fedora and CentOS counterparts, so yum is not of much use here, and 2) RHEL7, released for general use in June 2014, replaced sysvinit with systemd, which requires that any custom initialization routines (in this case, starting Tomcat and persisting iptables rules) be written as systemd services.
I used an EC2 instance during this setup process, but nothing I did or made use of is specific to EC2, you can use any newly configured RHEL7 instance.
Initial Configuration of Amazon EC2 Instance
- If you’re not using EC2 and can already SSH in to your machine, you can skip this section.
- Added instance of ami-77d7a747 (RHEL-7.0GAHVM-x86_64-3-Hourly2) as m1.large
- Assigned Elastic IP to instance.
- Attached Security Group allowing inbound TCP traffic on port 22 and all ICMP traffic.
- Booted instance.
- Logged into instance as UNIX user
ec2-user.sudo -ito gain rootnano /etc/ssh/sshd_config- Changed
PermitRootLoginfromyestono
- Changed
systemctl restart sshdmkdir /usr/local/tomcat-> this is the$CATALINA_HOME/$CATALINA_BASEfor your Tomcat installuseradd -Mb /usr/local tomcat-> use$CATALINA_HOMEfor thetomcatuser’s base directory, and don’t install a home directorychown -R tomcat:tomcat /usr/local/tomcatuseradd mquinnpasswd mquinncp -R /home/ec2-user/.ssh /home/mquinn/.ssh-> copies the allowed public SSH key details from the default UNIX user set up by EC2 to themquinnuser, which I’ll be logging in as from now on.chown -R mquinn:mquinn /home/mquinn/.sshvisudoadded line:mquinn ALL=(ALL) ALL
- Logged out as UNIX user
ec2-user; for the rest of the process, I use themquinnUNIX user account I created above.
Install Apache Tomcat 7.0.54
- Logged into instance as UNIX user
mquinn cd ~sudo yum install java-1.7.0-openjdk java-1.7.0-opendjk-devel wget nanosudo wget http://www.webhostingjams.com/mirror/apache/tomcat/tomcat-7/v7.0.54/bin/apache-tomcat-7.0.54-fulldocs.tar.gztar xvf apache-tomcat*.tar.gzsudo cp -R apache-tomcat*/* /usr/local/tomcatsudo chown -R tomcat:tomcat /usr/local/tomcat- Modified the EC2 Security Group associated with the instance to allow inbound HTTP traffic on port 8080.
sudo -u tomcat /usr/local/tomcat/bin/startup.sh- Opened
http://<public_ip_of_instance>:8080in web browser successfully (shows Tomcat welcome page). sudo -u tomcat /usr/local/tomcat/bin/shutdown.sh
Install Apache Solr 4.9.0
- Logged into instance as UNIX user
mquinn cd ~sudo wget http://apache.mirrors.hoobly.com/lucene/solr/4.9.0/solr-4.9.0.tgz-> make sure you get the binary distribution, not the sourcetar xvf solr*tgzsudo -u tomcat mkdir /usr/local/tomcat/solr-> here I’m creating the so-called “Solr home,” a place for Solr configuration files, cores, etc.; you may or may not prefer that these be in a subdirectory of$CATALINA_HOMEas I have done here.sudo cp -R solr-4.9.0/example/solr/* /usr/local/tomcat/solrsudo chown -R tomcat:tomcat /usr/local/tomcat/solrsudo -u tomcat cp solr-4.9.0/dist/solr-4.9.0.war /usr/local/tomcat/webapps/solr.warsudo chown tomcat:tomcat /usr/local/tomcat/webapps/solr.warsudo cp solr-4.9.0/example/lib/ext/*.jar /usr/local/tomcat/lib-> these are the SLF4J and Log4j resources required by Solr and its pluginssudo cp solr-4.9.0/dist/solrj-lib/* /usr/local/tomcat/lib/sudo cp solr-4.9.0/dist/*.jar /usr/share/tomcat/libsudo cp solr-4.9.0/example/resources/log4j.properties /usr/local/tomcat/lib- I agree that
$CATALINA_HOME/libis a ridiculously illogical place for the Solr-specificlog4j.propertiesfile, but this file must be on the classpath and it is not packaged up inside the Solr WAR file as I would expect it to be;$CATALINA_HOME/libis the suggested place for this file according to the documentation, but I don’t think it’s the best place.
- I agree that
nano /usr/local/tomcat/lib/log4j.properties; changed value oflog4j.appender.file.Filefrom${solr.log}/solr.logto/usr/local/tomcat/logs/solr.log.sudo chown tomcat:tomcat /usr/local/tomcat/lib/*sudo -u tomcat nano /usr/local/tomcat/Catalina/localhost/solr.xmlto create the file that tells the Solr webapp where the Solr home directory (created earlier) is via JNDI lookup:
/usr/local/tomcat/Catalina/localhost/solr.xml
<!-- MQUINN 07-07-2014 : Configure Solr Home for Solr webapp. -->
<Context crossContext="true">
<Environment name="solr/home" type="java.lang.String"
value="/usr/local/tomcat/solr" override="true"/>
</Context>
sudo -u tomcat /usr/local/tomcat/bin/startup.sh- Opened
http://<public_ip_of_instance>:8080/solrin web browser successfully (shows Solr dashboard). sudo -u tomcat /usr/local/tomcat/bin/shutdown.sh
Forward Port 80 Traffic to Tomcat on Port 8080
- It is generally considered unwise to run Tomcat on port 80 directly; doing so would require running it as root, which is not the best idea considering that overlooked, poor, or malicious code could be executed by the container, thereby compromising your machine. Although there are ways to start Tomcat as root and downgrade its privileges after startup, I prefer to go with the simpler solution that just forwards traffic on port 80 to Tomcat on port 8080. The tricky part, documented herein, is getting the necessary forwarding rule to persist after reboots; this is accomplished via a custom
systemdservice. - Logged into instance as UNIX user
mquinn sudo -ito gain rootnano /lib/systemd/system/iptables.service; contents of file are as follows:
/lib/systemd/system/iptables.service
[Unit]
Description=Persists iptables rules
ConditionPathExists=/etc/sysconfig/iptables
[Service]
WorkingDirectory=/usr/local/sbin/
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/sh systemd-iptables start
[Install]
WantedBy=basic.target
ln -s /lib/systemd/system/iptables.service /etc/systemd/system/iptables.servicenano /usr/local/sbin/systemd-iptables; contents of file are as follows:
/usr/local/sbin/systemd-iptables
#!/bin/sh
case $1 in
start)
/sbin/iptables-restore < /etc/sysconfig/iptables
;;
save)
/sbin/iptables-save > /etc/sysconfig/iptables
;;
*)
echo "Unkown action '$1'"
;;
esac
chmod 755 /usr/local/sbin/systemd-iptablesiptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-port 8080-> this is the actual forwarding rule that will now be persisted by the custom service defined above./usr/local/sbin/systemd-iptables save-> saves the rule to file, which will be picked up on reboot.systemctl daemon-reload-> ensuresystemdis aware of our newly created service.systemctl enable iptables.service-> enable our newly created service so that it starts on future boots.- On the EC2 Security Group attached to the instance, removed the rule allowing inbound TCP traffic on port 8080 and replaced it with a rule allowing TCP traffic on port 80 instead.
sudo -u tomcat /usr/local/tomcat/bin/startup.sh- Without rebooting yet, you should be able to open
http://<public_ip_of_machine>in a browser and see the Tomcat welcome page from port 80. rebootsudo -u tomcat /usr/local/tomcat/bin/startup.sh- After rebooting and starting Tomcat, you should be able to open
http://<public_ip_of_machine>again and see the Tomcat welcome page, now that theiptables.serviceservice is being started at boot.- Note:
systemctl status iptables.servicewill confirm whether or not you have registered and enabled the service correctly, in addition to indicating whether it executed successfully at start-up.
- Note:
Running Tomcat at Startup
- Just as was done above with iptables, we’ll create a systemd service for Tomcat to ensure that it starts up with the machine.
- Logged in as UNIX user
mquinn sudo -ito gain rootnano /lib/systemd/system/tomcat.service; contents of file are as follows:
/lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat 7 service
After=network.target
[Service]
WorkingDirectory=/usr/local/tomcat/bin
RemainAfterExit=yes
ExecStart=/bin/sh systemd-tomcat start
Type=forking
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
# 10-12-2014 EDIT: Originally I had mistakenly written the line
# above as "WantedBy=multi.user.target"; thank you Profesesor Domingo
# Gallardo López for pointing this out.
- NOTE: For this service, the
UserandGroupdirectives are used to ensure that Tomcat starts as thetomcatUNIX user and group, and not asroot. ln -s /lib/systemd/system/tomcat.service /etc/systemd/system/tomcat.servicenano /usr/local/sbin/systemd-tomcat; file contents are as follows:
/usr/local/sbin/systemd-tomcat
#!/bin/sh
case $1 in
start)
/usr/local/tomcat/bin/startup.sh
;;
stop)
/usr/local/tomcat/bin/shutdown.sh
;;
*)
echo "Unkown action '$1'"
;;
esac
chmod 755 /usr/local/sbin/systemd-tomcatsystemctl daemon-reloadsystemctl enable tomcat.servicereboot- After waiting for a minute or two, you should be able to open
http://<public_ip_of_machine>in your browser and see the Tomcat welcome page, without SSH'ing in to start anything manually. If that’s the case, you now have a clean Solr/Tomcat install running on RHEL7. If Tomcat doesn’t come back up after the reboot, check the output ofsystemctl status tomcat.serviceand ensure you stepped through the procedure above correctly.
Final Notes
- At this point, I snapshotted the EBS volume underlying my EC2 instance for use in creating future Solr instances without needing to go through the steps above again.
- Remember to keep up with updates to Tomcat and Solr, since this procedure installed them from binary distributions, rather than via
yum. - The Tomcat 7 distribution comes with several webapps already deployed, such as a manager webapp to get container information; although no one can access it without you first modifying
$CATALINA_HOME/conf/tomcat-users.xml, it’s best to remove this webapp for security reasons if you don’t plan on using it. - Don’t forget that your Solr dashboard, as configured in this procedure, is wide open to the Internet. Your next move should be to lock down access to this; see https://wiki.apache.org/solr/SolrSecurity.
- The
systemdservices defined here are basic and may even have better alternative configurations than the ones I describe; aside from setting up these two services, I have little experience withsystemd, which is something I’m working to change given that it’s seeing increasing adoption by various Linux distributions.
Useful References
I found the following to be good resources while setting things up and troubleshooting: