Tomcat-7 to Linux Service
****************************************
-------------------Software Environment
· VMware Workstation 7.1.4 build-385536
· Red Hat Enterprise Linux (RHEL) 4
· Apche-Tomcat-7.0.6
--------------------Operation Steps
[1] Download Tomcat-7
URL:http://tomcat.apache.org/download-70.cgi
Tips:此处仅介绍关于Tomcat7配置Linux Service的部分,Tomcat的基础配置请参阅《Tomcat安装与配置》
[2] Configure Tomcat-7
[root@localhost ~]cd /tomcat7
[root@localhost tomcat7]cd /bin
[root@localhost bin]cp catalina.sh /etc/init.d
[root@localhost tomcat7]cd /etc/init.d
[root@localhost init.d]pwd
/etc/init.d
[root@localhost init.d]mv catalina.sh tomcat
[root@localhost init.d]vi tomcat
--------------------
在#!/bin/bash下输入
# chkconfig: 2345 10 90
# description:Tomcat service
在#OS specific support................上输入
CATALINA_HOME=/tomcat7
JAVA_HOME=/usr/Java/jdk1.6.0_10
保存并退出文件
--------------------
[root@localhost init.d]chmod +x tomcat
[root@localhost init.d]cd /
[root@localhost /]vi /etc/profile
--------------------
在最后一行输入
#CATALINA_HOME
CATALINA_HOME=/tomcat7
export CATALINA_HOME
保存并退出文件
--------------------
[root@localhost /]source /etc/profile
貌似这样子是不用重启就可以让环境变量生效
[3] Add tomcat to service list
[root@localhost /]chkconfig --add tomcat
[4] Check service list
[root@localhost /]chkconfig --list tomcat
tomcat0:关闭1:关闭2:启用3:启用 4:启用 5:启用 6:关闭
[5] Test tomcat service
[root@localhost /]service tomcat
--------------------
Using CATALINA_BASE: /tomcat7
Using CATALINA_HOME: /tomcat7
Using CATALINA_TMPDIR: /tomcat7/temp
Using JRE_HOME: /usr/java/jdk1.6.0_10
Using CLASSPATH: /tomcat7/bin/bootstrap.jar:/tomcat7/bin/tomcat-juli.jar
Usage: catalina.sh ( commands ... ).
--------------------
[6] Start tomcat service
[root@localhost /]service tomcat start
[7] OK!
-------------------------------
同步时间 命令 ntpdate us.pool.ntp.org
==========================
配置两个tomcat
1 在/etc/profile中添加以下粗体的配置
#原有的tomcat配置
TOMCAT_HOME=/usr/local/tomcat
CATALINA_HOME=/usr/local/tomcat
CATALINA_BASE=/usr/local/tomcat
export TOMCAT_HOME CATALINA_HOME CATALINA_BASE
#添加的tomcat配置,可照此样添加多个tomcat
TOMCAT_2_HOME=/usr/local/tomcat2
CATALINA_2_HOME=/usr/local/tomcat2
CATALINA_2_BASE=/usr/local/tomcat2
export TOMCAT_2_HOME CATALINA_2_HOME CATALINA_2_BASE
备注:然后就是conf里面的server.xml 更改端口,建议在原来的端口上面都加1;
不知为毛/etc/profile里面新增的内容,在终端中能echo出来,但就是不生效,不知道是不是在catalina.sh文件中被冲掉了。
2 设置JVM参数,
在catalina.sh文件的94行添加设置JVM参数的内容
94行代码块如下
- # $Id: catalina.sh 1202062 2011-11-15 06:50:02Z mturk $
- # -----------------------------------------------------------------------------
设置JVM参数的内容的参数如下
- # OS specific support. $var _must_ be set to either true or false.
- # chkconfig: 2345 10 90
- # description:Tomcat service
- CATALINA_BASE=/usr/local/tomcat
- CATALINA_HOME=/usr/local/tomcat
- CATALINA_TMPDIR=/usr/local/tomcat/temp
- JRE_HOME=/usr/java/jdk1.7.0_17/jre/
- JAVA_OPTS="-server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$CATALINA_HOME/logs "
- CATALINA_OPTS="$CATALINA_OPTS -Xms512m -Xmx1024m $JPDA_OPTS"
- # -------------------------------------------------------------------------------------------
注意:如果要将tomcat注册为linux服务,“chkconfig”这行的代码一定是要的,windows系统可以忽视
测试ok后,将catalina.sh文件copy到/etc/init.d/ 重命名为tomcat2
ok后,利用chkconfig 将tomcat2 注册为linux服务吧
------------------------------------
========================================================
查看正在运行的tomcat的JVM系统信息等相关操作
当tomcat总是出现OutOfMemoryError: PermGen space或者 java.lang.OutOfMemoryError: Java heap space。可以下面的方法看设置的一些参数到底生效没
windows平台就用jvisualvm之类的gui界面工具去看吧。
linux下可以用
#ps -ef|grep java
显示如下信息
- root 740 1 0 May07 ? 00:00:59 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat2/conf/logging.properties -server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512m -Xmx1024m -Djava.endorsed.dirs=/usr/local/tomcat2/endorsed -classpath /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat2 -Dcatalina.home=/usr/local/tomcat2 -Djava.io.tmpdir=/usr/local/tomcat2/temp org.apache.catalina.startup.Bootstrap start
- root 5371 1 0 May08 ? 00:01:07 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512m -Xmx1024m -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
- root 10031 9877 0 09:03 pts/0 00:00:00 grep java
或者 用java自带的工具 jinfo
#jinfo 5371 显示如下信息:
- Java System Properties:
-
- java.runtime.name = Java(TM) SE Runtime Environment
- java.vm.version = 23.7-b01
- sun.boot.library.path = /usr/java/jdk1.7.0_17/jre/lib/i386
- shared.loader =
- java.vendor.url = http://java.oracle.com/
- java.vm.vendor = Oracle Corporation
- path.separator = :
- file.encoding.pkg = sun.io
- java.vm.name = Java HotSpot(TM) Server VM
- java.util.logging.config.file = /usr/local/tomcat/conf/logging.properties
- tomcat.util.buf.StringCache.byte.enabled = true
- sun.os.patch.level = unknown
- sun.java.launcher = SUN_STANDARD
- user.country = US
- user.dir = /
- java.vm.specification.name = Java Virtual Machine Specification
- java.runtime.version = 1.7.0_17-b02
- java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
- os.arch = i386
- java.endorsed.dirs = /usr/local/tomcat/endorsed
- line.separator =
-
- java.io.tmpdir = /usr/local/tomcat/temp
- java.vm.specification.vendor = Oracle Corporation
- java.util.logging.manager = org.apache.juli.ClassLoaderLogManager
- java.naming.factory.url.pkgs = org.apache.naming
- os.name = Linux
- sun.jnu.encoding = UTF-8
- java.library.path = /usr/java/packages/lib/i386:/lib:/usr/lib
- tomcat.util.scan.DefaultJarScanner.jarsToSkip = bootstrap.jar,commons-daemon.jar,tomcat-juli.jar,annotations-api.jar,el-api.jar,jsp-api.jar,servlet-api.jar,catalina.jar,catalina-ant.jar,catalina-ha.jar,catalina-tribes.jar,jasper.jar,jasper-el.jar,ecj-*.jar,tomcat-api.jar,tomcat-util.jar,tomcat-coyote.jar,tomcat-dbcp.jar,tomcat-jni.jar,tomcat-spdy.jar,tomcat-i18n-en.jar,tomcat-i18n-es.jar,tomcat-i18n-fr.jar,tomcat-i18n-ja.jar,tomcat-juli-adapters.jar,catalina-jmx-remote.jar,catalina-ws.jar,tomcat-jdbc.jar,commons-beanutils*.jar,commons-codec*.jar,commons-collections*.jar,commons-dbcp*.jar,commons-digester*.jar,commons-fileupload*.jar,commons-httpclient*.jar,commons-io*.jar,commons-lang*.jar,commons-logging*.jar,commons-math*.jar,commons-pool*.jar,jstl.jar,geronimo-spec-jaxrpc*.jar,wsdl4j*.jar,ant.jar,ant-junit*.jar,aspectj*.jar,jmx.jar,h2*.jar,hibernate*.jar,httpclient*.jar,jmx-tools.jar,jta*.jar,log4j*.jar,mail*.jar,slf4j*.jar,xercesImpl.jar,xmlParserAPIs.jar,xml-apis.jar,dnsns.jar,ldapsec.jar,localedata.jar,sunjce_provider.jar,sunmscapi.jar,sunpkcs11.jar,jhall.jar,tools.jar,sunec.jar,zipfs.jar,apple_provider.jar,AppleScriptEngine.jar,CoreAudio.jar,dns_sd.jar,j3daudio.jar,j3dcore.jar,j3dutils.jar,jai_core.jar,jai_codec.jar,mlibwrapper_jai.jar,MRJToolkit.jar,vecmath.jar,junit.jar,junit-*.jar,ant-launcher.jar
- java.class.version = 51.0
- java.specification.name = Java Platform API Specification
- sun.management.compiler = HotSpot Tiered Compilers
- os.version = 2.6.18-308.11.1.el5
- user.home = /root
- user.timezone = PRC
- catalina.useNaming = true
- java.awt.printerjob = sun.print.PSPrinterJob
- file.encoding = UTF-8
- java.specification.version = 1.7
- catalina.home = /usr/local/tomcat
- user.name = root
- java.class.path = /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
- java.naming.factory.initial = org.apache.naming.java.javaURLContextFactory
- package.definition = sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.
- java.vm.specification.version = 1.7
- sun.arch.data.model = 32
- sun.java.command = org.apache.catalina.startup.Bootstrap start
- java.home = /usr/java/jdk1.7.0_17/jre
- user.language = en
- java.specification.vendor = Oracle Corporation
- awt.toolkit = sun.awt.X11.XToolkit
- java.vm.info = mixed mode
- java.version = 1.7.0_17
- java.ext.dirs = /usr/java/jdk1.7.0_17/jre/lib/ext:/usr/java/packages/lib/ext
- sun.boot.class.path = /usr/java/jdk1.7.0_17/jre/lib/resources.jar:/usr/java/jdk1.7.0_17/jre/lib/rt.jar:/usr/java/jdk1.7.0_17/jre/lib/sunrsasign.jar:/usr/java/jdk1.7.0_17/jre/lib/jsse.jar:/usr/java/jdk1.7.0_17/jre/lib/jce.jar:/usr/java/jdk1.7.0_17/jre/lib/charsets.jar:/usr/java/jdk1.7.0_17/jre/lib/jfr.jar:/usr/java/jdk1.7.0_17/jre/classes
- java.awt.headless = true
- server.loader =
- java.vendor = Oracle Corporation
- catalina.base = /usr/local/tomcat
- file.separator = /
- java.vendor.url.bug = http://bugreport.sun.com/bugreport/
- common.loader = ${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
- sun.io.unicode.encoding = UnicodeLittle
- sun.cpu.endian = little
- package.access = sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.
- sun.cpu.isalist =
-
- VM Flags:
-
- -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512m -Xmx1024m -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp