Difference between revisions of "Tomcat"

 
(32 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Linux]]
 +
[[Category:Development]]
 +
 
Tomcat is a Java servlet container, it can be used to display simple JSP and run ''Spring'' applications.
 
Tomcat is a Java servlet container, it can be used to display simple JSP and run ''Spring'' applications.
  
However, it cannot run Java EE, you'll need a proper application server such IBM WAS, Glassfish, Jboss, etc. to do so.
+
However, it cannot run JavaEE, you'll need a proper application server such IBM WAS, Glassfish, Jboss, etc. to do so.
 
 
 
 
 
 
=installation=
 
 
 
 
 
==Automatic install==
 
This is the recommended installation.
 
<syntaxhighlight lang="bash">
 
apt-get install tomcat7 tomcat7-admin tomcat7-common tomcat7-docs tomcat7-examples
 
</syntaxhighlight>
 
On Debian, the service is not available. So you can add a symlink for tomcat7:
 
<syntaxhighlight lang="bash">
 
ln -s /etc/init.d/tomcat7 /usr/bin/tomcat7
 
</syntaxhighlight>
 
 
 
 
 
==Manual install==
 
 
 
===Get and install file===
 
Get Tomcat latest version from the official website: http://tomcat.apache.org/
 
 
 
Unzip Tomcat to /opt: => /opt/tomcat
 
 
 
===Adjust rights===
 
<syntaxhighlight lang="bash">
 
chown -R tomcat:tomcat /opt/tomcat
 
</syntaxhighlight>
 
 
 
===Add server to path===
 
You need to add an environment variable:
 
<syntaxhighlight lang="bash">
 
vim /etc/profile
 
</syntaxhighlight>
 
 
 
Add
 
<syntaxhighlight lang="bash">
 
export CATALINA_HOME="/opt/tomcat"
 
PATH="[...]:$JAVA_HOME/bin:$M2:$CATALINA_HOME/bin"
 
</syntaxhighlight>
 
 
 
Take changes into account
 
<syntaxhighlight lang="bash">
 
source /etc/profile
 
</syntaxhighlight>
 
 
 
===Startup script===
 
<syntaxhighlight lang="bash">
 
vim /etc/init.d/tomcat
 
</syntaxhighlight>
 
 
 
Put the following content:
 
<syntaxhighlight lang="bash">
 
#!/bin/bash
 
### BEGIN INIT INFO
 
# Provides:            tomcat
 
# Required-Start:      $remote_fs
 
# Required-Stop:        $remote_fs
 
# Default-Start:        2 3 4 5
 
# Default-Stop:        0 1 6
 
# Short-Description:    Tomcat7
 
### END INIT INFO
 
 
 
# description: tomcat Start - Stop - Restart
 
# processname: tomee
 
# chkconfig: 234 20 80
 
export JAVA_HOME="/usr/lib/jvm"
 
PATH="$PATH:$JAVA_HOME/bin"
 
export PATH
 
export CATALINA_HOME=/opt/tomcat
 
 
 
case $1 in
 
  start)
 
echo "Starting Tomcat"
 
sh $CATALINA_HOME/bin/startup.sh
 
;;
 
  stop)
 
echo "Stopping Tomcat"
 
sh $CATALINA_HOME/bin/shutdown.sh
 
;;
 
  restart)
 
echo "Restarting Tomcat"
 
sh $CATALINA_HOME/bin/shutdown.sh
 
sh $CATALINA_HOME/bin/startup.sh
 
;;
 
  *)
 
echo $"Usage: $0 {start|stop}"
 
exit 1
 
;;
 
esac
 
exit 0
 
</syntaxhighlight>
 
 
 
Update rights
 
<syntaxhighlight lang="bash">
 
chmod 750 /etc/init.d/tomcat
 
</syntaxhighlight>
 
 
 
 
 
===Update boot sequence===
 
<syntaxhighlight lang="bash">
 
cd /etc/init.d
 
update-rc.d tomcat defaults
 
</syntaxhighlight>
 
 
 
 
 
===Remove from boot sequence===
 
<syntaxhighlight lang="bash">
 
update-rc.d tomee remove
 
</syntaxhighlight>
 
 
 
 
 
 
 
=Server configuration=
 
 
 
 
 
==Create users and user-rights==
 
Manual installation
 
<syntaxhighlight lang="bash">
 
vim /opt/tomcat/conf/tomcat-users.xml
 
</syntaxhighlight>
 
 
 
Automatic installation
 
<syntaxhighlight lang="bash">
 
vim /etc/tomcat7/tomcat-users.xml
 
</syntaxhighlight>
 
 
 
 
 
Add / uncomment:
 
<syntaxhighlight lang="bash">
 
<role rolename="manager" />
 
<role rolename="admin" />
 
<role rolename="manager-gui" />
 
<role rolename="manager-script" />
 
<role rolename="admin-gui" />
 
 
 
<user username="tomcat" password="password"  roles="admin, admin-gui, manager, manager-gui, manager-script" />
 
</syntaxhighlight>
 
 
 
 
 
 
 
==Increase permgen space==
 
  
===Manual install===
 
Add JAVA_OPTS parameters as environment variable
 
<syntaxhighlight lang="bash">
 
vim /etc/profile
 
</syntaxhighlight>
 
  
Add following line:
 
<syntaxhighlight lang="bash">
 
export JAVA_OPTS="-Xms1024m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:+DisableExplicitGC"
 
</syntaxhighlight>
 
  
Take changes into account
 
<syntaxhighlight lang="bash">
 
source /etc/profile
 
</syntaxhighlight>
 
  
Check changes
+
{| class="wikitable sortable" style="margin: 1em auto 1em auto"
<syntaxhighlight lang="bash">
+
|-
echo $JAVA_OPTS
+
! Installation !! Server configuration !! Application configuration !! Other
</syntaxhighlight>
+
|-valign="top"
 +
|width="20%"| [[Tomcat linux apt-get setup|Linux apt-get setup]]
 +
|width="20%"| [[Tomcat users management|Users management]]
 +
|width="20%"| [[Tomcat MySQL datasource|MySQL datasource]]
 +
|width="20%"| [[Apache 2 - proxy]]
 +
|-
 +
| [[Tomcat linux manual setup|Linux manual setup]]
 +
|| [[Tomcat UTF-8|UTF-8]] ||  ||
 +
|-
 +
| [[Tomcat Linux startup|Linux Tomcat on boot]]
 +
|| [[Tomcat IPv4 over IPv6|IPv4 over IPv6]] ||  ||
 +
|-
 +
| [[Tomcat windows setup|Windows setup]]
 +
||  [[Tomcat security restricted access|Server access restriction]] ||  ||
 +
|-
 +
| || [[Tomcat JMX|JMX configuration]]  ||  ||
 +
|-
 +
| || [[Tomcat increase PermGen|Increase PermGen]] ||  ||
 +
|-
 +
| || [[Tomcat war deployment through manager|War deployment through manager]] ||  ||
 +
|}
  
===Automatic install===
 
  
<syntaxhighlight lang="bash">
 
vim /etc/default/tomcat7
 
</syntaxhighlight>
 
 
Add following line:
 
<syntaxhighlight lang="bash">
 
JAVA_OPTS="-Djava.awt.headless=true -XX:+UseConcMarkSweepGC -Xms1024m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:+DisableExplicitGC"
 
</syntaxhighlight>
 
 
Take changes into account
 
<syntaxhighlight lang="bash">
 
service tomcat7 restart
 
</syntaxhighlight>
 
 
 
==Add UTF-8 support on Tomcat==
 
By default Tomcat will rely on the O.S locale.
 
 
In order to support UTF-8 URLs, you’ve to manually update the server’s configuration.
 
* automatic install: $Tomcat = /etc/tomcat7
 
 
<syntaxhighlight lang="bash">
 
vim $TOMCAT/conf/server.xml
 
</syntaxhighlight>
 
 
~ Line 70 change the “<connector port=”8080” …” value.
 
 
*Before
 
<syntaxhighlight lang="bash">
 
<Connector port="8080" protocol="HTTP/1.1"
 
              connectionTimeout="20000"
 
              redirectPort="8443" />
 
</syntaxhighlight>
 
 
*After
 
<syntaxhighlight lang="bash">
 
<Connector port="8080" protocol="HTTP/1.1"
 
              connectionTimeout="20000"
 
              redirectPort="8443" URIEncoding="UTF-8" />
 
</syntaxhighlight>
 
 
 
Restart Tomcat server
 
<syntaxhighlight lang="bash">
 
service tomcat7 restart
 
</syntaxhighlight>
 
 
 
 
==War deployment==
 
There is 2 ways to deploy a war:
 
* By pushing the target war into ${Tomcat root} / webapps
 
* By using the graphical tool http://localhost:8080/manager
 
 
 
If you plan to use the graphical tool then you have to adjust the war file max size.
 
Edit:
 
<syntaxhighlight lang="bash">
 
${Tomcat root} / webapps / manager / WEB-INF / web.xml
 
</syntaxhighlight>
 
 
 
Adjust following values ~line 54 :
 
<syntaxhighlight lang="bash">
 
<multipart-config>
 
    <max-file-size>104857600</max-file-size>
 
    <max-request-size>104857600</max-request-size>
 
    <file-size-threshold>0</file-size-threshold>
 
</multipart-config>
 
</syntaxhighlight>
 
 
 
 
=add JMX management=
 
 
Tomcat can be remotely monitored through JMX.
 
That’s useful to check the status of the server: memory, threads and processes, performances, etc.
 
 
 
==Unsafe configuration==
 
Just edit your default Tomcat launcher:
 
<syntaxhighlight lang="bash">
 
vim /etc/default/tomcat7
 
</syntaxhighlight>
 
 
Add the following lines:
 
<syntaxhighlight lang="bash">
 
JAVA_HOME=/usr/lib/jvm/default-jvm/           → That must be the ORACLE JDK
 
 
# JMX configuration
 
JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote"
 
JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.port=8090"
 
JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.authenticate=false"
 
JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
 
 
# Remote access setup
 
JAVA_OPTS="${JAVA_OPTS} -Djava.rmi.server.hostname=dev.daxiongmao.eu"
 
</syntaxhighlight>
 
 
 
Restart tomcat
 
<syntaxhighlight lang="bash">
 
Service tomcat7 restart
 
</syntaxhighlight>
 
 
 
==Open firewall==
 
Edit your firewall script
 
<syntaxhighlight lang="bash">
 
vim /etc/firewall/firewall-start.sh
 
</syntaxhighlight>
 
 
'''Incoming connections'''
 
<syntaxhighlight lang="bash">
 
$IPTABLES -A INPUT -p tcp --dport 8090 -j ACCEPT    # Tomcat JMX
 
</syntaxhighlight>
 
 
'''Outgoing connections'''
 
<syntaxhighlight lang="bash">
 
$IPTABLES -t filter -A OUTPUT -p tcp -m state --state NEW --dport 8090 -j ACCEPT      # Tomcat JMX
 
</syntaxhighlight>
 
 
Just restart your firewall to apply changes
 
<syntaxhighlight lang="bash">
 
firewall restart
 
</syntaxhighlight>
 
 
 
==jConsole (simple viewer)==
 
Just execute '''jConsole''' from Linux or Windows
 
>> TODO: add picture
 
 
 
Just fill up the server:port
 
>> TODO: add picture
 
 
 
You should have the following warning:
 
>> TODO: add picture
 
 
 
 
==jVisualVM (advanced monitoring)==
 
Just execute '''jvisualvm'''
 
 
 
Add a remote host
 
>> TODO: add picture
 
 
 
Just type the server name and click OK
 
>> TODO: add picture
 
 
 
Then, add a JMX connection
 
>> TODO: add picture
 
 
 
Then you can access the server
 
 
 
==Restricted access==
 
You can also restricted the access
 
 
Create the JMX users rights
 
<syntaxhighlight lang="bash">
 
vim /var/lib/tomcat7/conf/jmxremote.access
 
 
Put the following
 
<syntaxhighlight lang="bash">
 
monitorRole readonly → replace monitorRole by your ''userName''
 
controlRole readwrite 
 
</syntaxhighlight>
 
 
Create the JMX users password
 
<syntaxhighlight lang="bash">
 
vim /var/lib/tomcat7/conf/jmxremote.password
 
</syntaxhighlight>
 
 
Put the following
 
<syntaxhighlight lang="bash">
 
monitorRole tomcat         → replace monitorRole by username | replace tomcat by password
 
controlRole tomcat
 
</syntaxhighlight>
 
 
Set rights and permissions upon login files
 
<syntaxhighlight lang="bash">
 
chmod 400 /var/lib/tomcat7/conf/jmxremote.*
 
chown tomcat7:tomcat7 /var/lib/tomcat7/conf/jmxremote.*
 
</syntaxhighlight>
 
 
Edit the default tomcat launcher
 
<syntaxhighlight lang="bash">
 
# JMX configuration
 
JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote"
 
JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.port=8090"
 
JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.authenticate=true"
 
JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.access.file=/var/lib/tomcat7/conf/jmxremote.access"
 
JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.password.file=/var/lib/tomcat7/conf/jmxremote.password"
 
JAVA_OPTS="${JAVA_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
 
 
# Remote access setup
 
JAVA_OPTS="${JAVA_OPTS} -Djava.rmi.server.hostname=dev.daxiongmao.eu"
 
</syntaxhighlight>
 
 
Restart tomcat
 
<syntaxhighlight lang="bash">
 
# service tomcat7 restart
 
</syntaxhighlight>
 
 
 
 
 
=Add MySQL datasource=
 
 
==Setup MySQL JDBC connector==
 
 
1. Download MySQL JDBC driver
 
http://dev.mysql.com/downloads/connector/j/
 
 
2. Decompress content and extract mysql-connector-java-XXX-bin.jar
 
 
3. Copy this file into $TOMCAT/libs
 
Automatic install: /usr/share/tomcat7/lib
 
 
==Declare MySQL datasource==
 
 
===Server.xml===
 
Automatic install: /etc/tomcat7/server.xml
 
 
<syntaxhighlight lang="bash">
 
$TOMCAT/server.xml
 
</syntaxhighlight>
 
 
Add
 
<syntaxhighlight lang="bash">
 
<host>
 
...
 
<GlobalNamingResources>
 
...
 
 
<!-- ####################################################################### -->
 
<!--                              MySQL datasource                          -->
 
<!-- ####################################################################### -->
 
 
<!-- maxActive: Maximum number of database connections in pool. Set to -1 for no limit. -->
 
<!-- maxIdle: Maximum number of idle database connections to retain in pool. Set to -1 for no limit.  -->
 
<!-- maxWait: Maximum time to wait for a database connection to become available in ms. Set to -1 to wait indefinitely. -->
 
<!-- driverClassName: Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver. -->
 
 
<Resource name="jdbc/myDataSource"
 
      auth="Container" type="javax.sql.DataSource"
 
      username="user" password="password"
 
      url="jdbc:mysql://localhost:3306/mySchema"
 
      maxActive="50" maxIdle="30" maxWait="10000"
 
      driverClassName="com.mysql.jdbc.Driver"
 
      factory="org.apache.commons.dbcp.BasicDataSourceFactory"
 
/>
 
 
 
</GlobalNamingResources>
 
</syntaxhighlight>
 
 
 
''Ubuntu Tomcat Fix''
 
 
You must use the Factory line for Ubuntu !
 
 
This will fix the “ClassNotFoundException: BasicDataSourceFactory”
 
 
You may encountered this error with a Tomcat out of the box.
 
 
Source:  http://stackoverflow.com/questions/14712308/ubuntu-tomcat7-java-lang-classnotfoundexception-org-apache-tomcat-dbcp-dbcp-bas
 
 
===Context.xml===
 
Edit:
 
<syntaxhighlight lang="bash">
 
$TOMCAT/context.xml
 
</syntaxhighlight>
 
 
Add the following declaration
 
<syntaxhighlight lang="bash">
 
<!-- ####################################################################### -->
 
<!--                              MySQL datasource                          -->
 
<!-- ####################################################################### -->
 
<ResourceLink name="jdbc/myDataSource"
 
              global="jdbc/myDataSource"
 
              type="javax.sql.datasource" />
 
</syntaxhighlight>
 
 
 
===web.xml===
 
Edit
 
<syntaxhighlight lang="bash">
 
$TOMCAT/web.xml
 
</syntaxhighlight>
 
 
Add the following declaration
 
<syntaxhighlight lang="bash">
 
<!-- ####################################################################### -->
 
<!--                              MySQL datasource                          -->
 
<!-- ####################################################################### -->
 
 
    <resource-ref>
 
    <description>RTD database</description>
 
    <res-ref-name>jdbc/VehcoData</res-ref-name>
 
    <res-type>javax.sql.DataSource</res-type>
 
    <res-auth>Container</res-auth>
 
    </resource-ref>
 
</syntaxhighlight>
 
 
 
===Take changes into account===
 
Restart tomcat
 
<syntaxhighlight lang="bash">
 
service tomcat7 restart
 
</syntaxhighlight>
 
 
Check result:
 
http://localhost:8080/manager/text/resources
 
 
 
==Use datasource==
 
To use the datasource with a JNDI name you must prefix it with:  java:comp/env/
 
 
<syntaxhighlight lang="bash">
 
      java:comp/env/jdbc/myDataSource
 
</syntaxhighlight>
 
 
 
==Datasource improvements==
 
You can tweak the datasource using some specific config parameters.
 
Edit:
 
<syntaxhighlight lang="bash">
 
$TOMCAT/server.xml
 
</syntaxhighlight>
 
 
Edit your JDBC resource:
 
<syntaxhighlight lang="bash">
 
<Resource auth="Container"
 
  name="jdbc/APP_NAME"
 
  username="user"
 
  password="password"
 
  type="javax.sql.DataSource"
 
 
  url="jdbc:oracle:thin:@server.domain:1521:development"    → ORACLE database
 
  driverClassName="oracle.jdbc.driver.OracleDriver"
 
 
  url="jdbc:mysql://localhost:3306/rtd" → MySQL database
 
  driverClassName="com.mysql.jdbc.Driver"
 
 
  maxActive="50" maxIdle="30" maxWait="10000" → Connection pool
 
  maxIdle="10"
 
  maxWait="5000"
 
  maxActive="30"              → To remove none close connections
 
 
  logAbandoned="true"     To report the stacktrace of the faulty code
 
  removeAbandoned="true"     To remedy connection starvation while leaky code is not fixed
 
  removeAbandonedTimeout="60"     Interval for fixing connection starvation
 
 
  validationQuery="select 1 from dual"    custom query to perform regular checks
 
  validationInterval="30000"     To be adjusted!  Interval in ms.
 
  testOnBorrow="true"
 
  testOnReturn="false"
 
  testWhileIdle="true"
 
  timeBetweenEvictionRunsMillis="5000"
 
  numTestsPerEvictionRun="3"
 
  minEvictableIdleTimeMillis="30000"
 
/>
 
</syntaxhighlight>
 
 
More tweaks: http://commons.apache.org/proper/commons-dbcp/configuration.html
 
  
  
Line 545: Line 40:
  
 
==Files location==
 
==Files location==
 +
 
The applications files are in $Tomcat/webapps
 
The applications files are in $Tomcat/webapps
 
* Automatic installation: /var/lib/tomcat/webapps  
 
* Automatic installation: /var/lib/tomcat/webapps  
 +
* Manual installation (tomcat instance): /opt/tomcat-base/webapps
 +
  
 
==Remove old temp files==
 
==Remove old temp files==
 +
 
In case of bugs, you can remove the working directory: $Tomcat/work/Catalina/localhost/*
 
In case of bugs, you can remove the working directory: $Tomcat/work/Catalina/localhost/*
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 +
# Package installation
 
rm -Rf /var/lib/tomcat7/work/Catalina/localhost/*
 
rm -Rf /var/lib/tomcat7/work/Catalina/localhost/*
 +
 +
# Manual installation, Tomcat instance
 +
rm -Rf /opt/tomcat-base/work/Catalina/localhost/*
 
</syntaxhighlight>
 
</syntaxhighlight>
 
==Server access==
 
http://server:8080
 

Latest revision as of 12:24, 1 April 2015


Tomcat is a Java servlet container, it can be used to display simple JSP and run Spring applications.

However, it cannot run JavaEE, you'll need a proper application server such IBM WAS, Glassfish, Jboss, etc. to do so.



Installation Server configuration Application configuration Other
Linux apt-get setup Users management MySQL datasource Apache 2 - proxy
Linux manual setup UTF-8
Linux Tomcat on boot IPv4 over IPv6
Windows setup Server access restriction
JMX configuration
Increase PermGen
War deployment through manager



Basic tasks

Files location

The applications files are in $Tomcat/webapps

  • Automatic installation: /var/lib/tomcat/webapps
  • Manual installation (tomcat instance): /opt/tomcat-base/webapps


Remove old temp files

In case of bugs, you can remove the working directory: $Tomcat/work/Catalina/localhost/*

# Package installation
rm -Rf /var/lib/tomcat7/work/Catalina/localhost/*

# Manual installation, Tomcat instance
rm -Rf /opt/tomcat-base/work/Catalina/localhost/*