Skip to content

Instantly share code, notes, and snippets.

@ryanguill
Last active December 31, 2015 03:39
Show Gist options
  • Save ryanguill/7929048 to your computer and use it in GitHub Desktop.
Save ryanguill/7929048 to your computer and use it in GitHub Desktop.

Preface

This guide is one section of a larger guide to installing a cent 6.x server in virtual box for development purposes with different applications. The top level guide with links to all of the sections is here: https://gist.github.com/ryanguill/5149058 Some instructions in this guide may assume a configuration from other parts of the guide.

#Install Apache Tomcat

References:

ensure you have the latest java (you should already if you followed the guide at the top)

# yum install java-1.7.0-openjdk
# yum install java-1.7.0-openjdk-devel
# java -version

You should see something like

java version "1.7.0_45"
OpenJDK Runtime Environment (rhel-2.4.3.2.el6_4-x86_64 u45-b15)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)

Run this:

#javac -version

You should see something like

javac 1.7.0_45

Run the following to set up your java home variable

# echo export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk.x86_64 > /etc/profile.d/java.sh
# source /etc/profile.d/java.sh

Now we need to download tomcat. Go to http://tomcat.apache.org/download-70.cgi to get the latest version. Find the tar.gz link and copy the link to use below.

Make sure you are in your home directory:

# cd ~
# wget http://mirror.reverse.net/pub/apache/tomcat/tomcat-7/v7.0.47/bin/apache-tomcat-7.0.47.tar.gz

Use tab completion to get the right name below

# tar xvzf apache-tomcat-7.0.47.tar.gz -C /opt

This created an apache-tomcat-7.0.47 directory under opt. Update the permissions:

# chmod -r /opt/apache-tomcat-7.0.47/conf/*

Export CATALINA_HOME:

# echo export CATALINA_HOME=/opt/apache-tomcat-7.0.47/ > /etc/profile.d/tomcat.sh
# source /etc/profile.d/tomcat.sh

Note: putting the *.sh files in /etc/profile.d/ will cause those scripts to be run any time a bash session starts, which well set those path variables for all shells. More info: http://www.linux-migration.org/ch02s03.html

Start tomcat:

# $CATALINA_HOME/bin/startup.sh

It should start immediately. Now we need to go poke a couple of holes in the firewall if they aren't there already:

# vi /etc/sysconfig/iptables

Add the following lines if they don't already exist in some form:

-A INPUT -s 0.0.0.0/0 -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -s 0.0.0.0/0 -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

Save and then restart iptables

# service iptables restart

Now you should be able to go to the 8080 port of this machine from the outside and see the tomcat manager

http://cent6-railo.local:8080/

Go ahead and stop the tomcat server:

# $CATALINA_HOME/bin/shutdown.sh

So now we need to create an init.d script so that tomcat starts automatically on boot and so that we can interact with it using the service command. This init.d script could likely be improved, but it should work for our purposes.

# vi /etc/init.d/tomcat

Copy and paste the following into the file, and then save and exit

#!/bin/bash
# description: Tomcat Start Stop
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk.x86_64
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/opt/apache-tomcat-7.0.47

case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;; 
stop)   
sh $CATALINA_HOME/bin/shutdown.sh
;; 
esac    
exit 0

Note: I would like to come back later and figure out how to reliably get tomcat to run as a different user, but after trying and failing to get permissions right for an afternoon, im punting on this for now.

Now chmod it and set the chkconfig levels

# chmod 755 /etc/init.d/tomcat
# chkconfig --add tomcat
# chkconfig --level 234 tomcat on
# chkconfig --list tomcat 

You should see

tomcat          0:off   1:off   2:on    3:on    4:on    5:off   6:off

Lets test it

# service tomcat start

You should be able to hit the 8080 port again

# service tomcat stop

Clean up after yourself

# rm -f apache-tomcat-7.0.47.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment