Tuesday, October 28, 2014

CentOS 7 - How to Install Tomcat and Configure Two Instances

OS: CentOS 7.0
Tomcat: 7.0.55
JDK: 1.8.0_20

In this tutorial I will show you how to install Tomcat 7.0 on a CentOS 7.0 server. Java JDK is required. I assume you already have it installed.

Download Tomcat:
# cd ~
# wget apache-tomcat-7.0.55.zip (Get the link from Apache)
# unzip apache-tomcat-7.0.55.zip
# mv apache-tomcat-7.0.55 /opt/tomcat

Set env variables by creating a file called “script.sh” in “/etc/profile.d” as:
# vi /etc/profile.d/script.sh
add
#!/bin/bash
CATALINA_HOME=/opt/tomcat
PATH=$CATALINA_HOME/bin:$PATH
export PATH CATALINA_HOME
export CLASSPATH=.
# chmod +x /etc/profile.d/script.sh
# source /etc/profile.d/script.sh

Now you can start Tomcat by typing:
# $CATALINA_HOME/bin/startup.sh
Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Tomcat started.

You can check port 8080 which is the default:
# netstat -atnp | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      20907/java

Create user accounts to secure and access admin/manager pages:
# vi $CATALINA_HOME/conf/tomcat-users.xml
add into tomcat-users>
 <role rolename="manager-gui"/>
 <role rolename="manager-script"/>
 <role rolename="manager-jmx"/>
 <role rolename="manager-status"/>
 <role rolename="admin-gui"/>
 <role rolename="admin-script"/>
 <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>

Make sure you changed the password.

Then restart Tomcat instace:
# $CATALINA_HOME/bin/catalina.sh stop
# $CATALINA_HOME/bin/catalina.sh start


Configure second Tomcat instance:

Make a copy of the first instance:
# cp -r /opt/tomcat /opt/tomcat2

Update the server.xml file:
# cd /opt/tomcat2/conf
# vi server.xml

Change Connector port to the port you want, for example, 8081
<Connector port="8081" protocol="HTTP/1.1"
 connectionTimeout="20000"
 redirectPort="8444" />

Change Connector executor:
<Connector executor="tomcatThreadPool"
 port="8081" protocol="HTTP/1.1"
 connectionTimeout="20000"
 redirectPort="8444" />

Change server port from 8005 to 8006:
<Server port="8006" shutdown="SHUTDOWN">

Update the env script file:
# vi /etc/profile.d/script2.sh
add
#!/bin/bash
CATALINA_HOME2=/opt/tomcat2
PATH=$CATALINA_HOME2/bin:$PATH
export PATH CATALINA_HOME2
export CLASSPATH=.
# source /etc/profile.d/script2.sh

Start the second instance:
# $CATALINA_HOME2/bin/startup.sh

Check the second instance is up and running:
# netstat -atnp | grep 8081
\tcp6       0      0 :::8081                 :::*                    LISTEN      22777/java

No comments: