转载:http://www.cnblogs.com/kerrycode/p/3588816.html
系统环境: Red Hat Enterprise Linux Server release 5.7 (Tikanga) 64位
Tomcat下载
从官方网站 http://tomcat.apache.org/下载你需要的Tomcat版本,目前Tomcat主要版本有Tomcat 6.0、Tomcat 7.0、Tomcat 8.0三个版本,下面我们以6.0(6.0.39)版本为例, 下载URL地址:http://tomcat.apache.org/download-60.cgi ,如果你需要下载Tomcat 6.0下的其它版本,点击链接地址“Archives“ http://archive.apache.org/dist/tomcat/tomcat-6/ 即可查看到你需要的版本。
Tomcat安装
Step 1:安装配置JDK
关于JDK的安装配置细节可以参考我以前的博客lRedhat Linux安装JDK 1.7,JDK最好选择合适的版本,这里我为了方便省事,就安装了JDK1.7.
1: [root@DB-Server tmp]# chmod +x jdk-7u25-linux-x64.rpm
2:
3: [root@DB-Server tmp]# rpm -ivh jdk-7u25-linux-x64.rpm
4: Preparing... ########################################### [100%]
5: 1:jdk ########################################### [100%]
6: Unpacking JAR files...
7: rt.jar...
8: jsse.jar...
9: charsets.jar...
10: tools.jar...
11: localedata.jar...
12: [root@DB-Server tmp]# cd /usr/java/
配置环境变量,在etc/profile文件下添加
export JAVA_HOME=/usr/java/jdk1.7.0_25
export CLASSPATH=/usr/java/jdk1.7.0_25/lib
export PATH=$JAVA_HOME/bin:$PATH
1: [root@DB-Server jdk1.7.0_25]# vi /etc/profile
2: [root@DB-Server jdk1.7.0_25]# source /etc/profile --生效修改配置信息
3:
4: [root@DB-Server jdk1.7.0_25]# echo $JAVA_HOME
5: /usr/java/jdk1.7.0_25
6: [root@DB-Server jdk1.7.0_25]# echo $CLASSPATH
7: /usr/java/jdk1.7.0_25/lib
8: [root@DB-Server jdk1.7.0_25]# java -version
9: java version "1.7.0_25"
10: Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
11: Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
12: [root@DB-Server jdk1.7.0_25]#
Step 2:安装Tomcat
1: [root@DB-Server tmp]#tar zxvf apach-tomcat-6.0.16.tar.gz
2:
3: [root@DB-Server tmp]#mkdir /usr/local/tomcat
4:
5: [root@DB-Server tmp]# mv apache-tomcat-6.0.39 /usr/local/ tomcat
6:
7:
8: [root@DB-Server tomcat]# vi /etc/profile
9:
10: export TOMCAT_HOME=/usr/local/tomcat
11:
12:
13: [root@DB-Server tomcat]# source /etc/profile
14: [root@DB-Server tomcat]# echo $TOMCAT_HOME
15: /usr/local/tomcat
16:
17:
18: [root@DB-Server bin]# pwd
19: /usr/local/tomcat/apache-tomcat-6.0.39/bin
20: [root@DB-Server bin]# ./startup.sh
21: Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-6.0.39
22: Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-6.0.39
23: Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-6.0.39/temp
24: Using JRE_HOME: /usr/java/jdk1.7.0_25
25: Using CLASSPATH: /usr/local/tomcat/apache-tomcat-6.0.39/bin/bootstrap.jar
如果需要指定Tomcat在某个账号下运行或管理,则可以通过下面脚本:例如,我要创建一个tomcat的账号
1: [root@DB-Server ~]# groupadd tomcat
2:
3: [root@DB-Server ~]# useradd -g tomcat -d /home/tomcat -u 502 tomcat
4:
5: Creating mailbox file: File exists
6:
7: [root@DB-Server tomcat]# chown -Rf tomcat.tomcat /usr/local/tomcat/
8:
9: [root@DB-Server tomcat]#passwd tomcat
10:
11: Changing password for user tomcat.
12:
13: New UNIX password:
14:
15: Retype new UNIX password:
16:
17: passwd: all authentication tokens updated successfully.
在浏览器里面输入http://localhost:8080/ (默认端口为8080,可以修改该端口号) 验证Tomcat是否安装成功,如果安装成功,则会看到Tomcat的index.jsp并证明Tomcat启动成功了
Tomcat配置
关于Tomcat的目录结构,可以参考下面简单的描述,
[root@DB-Server apache-tomcat-6.0.39]# ls
bin lib logs RELEASE-NOTES temp work
conf LICENSE NOTICE RUNNING.txt webapps
主目录下面有bin、lib等目录
bin 存放Tomcat启动、停止服务程序以及一些其他脚本程序
lib 存放Tomcat运行需要的jar包
log 存放Tomcat运行的相关日志文件
conf 存放Tomcat配置文件。例如server.xml 、 web.xml等
temp 存放Tomcat运行的临时文件。
webapps Web应用的发布目录,默认情况下把Web应用文件放于此目录。
work Tomcat的工作目录,默认情况下把编译JSP文件生成的servlet类文件放于此目录下
1:启动Tomcat服务
1: [root@DB-Server bin]# ./startup.sh
2:
3: Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-6.0.39
4:
5: Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-6.0.39
6:
7: Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-6.0.39/temp
8:
9: Using JRE_HOME: /usr/java/jdk1.7.0_25
10:
11: Using CLASSPATH: /usr/local/tomcat/apache-tomcat-6.0.39/bin/bootstrap.jar
2:关闭Tomcat服务
1: [root@DB-Server bin]# ./shutdown.sh
2:
3: Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-6.0.39
4:
5: Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-6.0.39
6:
7: Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-6.0.39/temp
8:
9: Using JRE_HOME: /usr/java/jdk1.7.0_25
10:
11: Using CLASSPATH: /usr/local/tomcat/apache-tomcat-6.0.39/bin/bootstrap.jar
12:
13: [root@DB-Server bin]#
3:查看Tomcat版本
有时候,如果当你接手去管理Tomcat, 可能你要了解一下Tomcat的版本信息,以及配置,那么怎么查看呢,去Tomcat的主目录下的bin目录下,执行version.sh,你就能查看到很多详细信息。
1: [root@DB-Server bin]# ./version.sh
2:
3: Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-6.0.39
4:
5: Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-6.0.39
6:
7: Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-6.0.39/temp
8:
9: Using JRE_HOME: /usr/java/jdk1.7.0_25
10:
11: Using CLASSPATH: /usr/local/tomcat/apache-tomcat-6.0.39/bin/bootstrap.jar
12:
13: Server version: Apache Tomcat/6.0.39
14:
15: Server built: Jan 27 2014 10:40:33
16:
17: Server number: 6.0.39.0
18:
19: OS Name: Linux
20:
21: OS Version: 2.6.18-274.el5
22:
23: Architecture: amd64
24:
25: JVM Version: 1.7.0_25-b15
26:
27: JVM Vendor: Oracle Corporation
28:
4:设置管理密码
如果你要进入Tomcat的管理页面:http://localhost:8080/manager/html ,这时会弹出下面认证界面,需要你输入用户名和密码,如果你没有配置过或者你忘了用户名、密码,那么应该找那个配置文件呢?其实你都可以通过配置文件 tomcat-users.xml 来解决
1: [root@DB-Server conf]# more tomcat-users.xml
2:
3: xml version='1.0' encoding='utf-8'?>
4:
5: --
6:
7: Licensed to the Apache Software Foundation (ASF) under one or more
8:
9: contributor license agreements. See the NOTICE file distributed with
10:
11: this work for additional information regarding copyright ownership.
12:
13: The ASF licenses this file to You under the Apache License, Version 2.0
14:
15: (the "License"); you may not use this file except in compliance with
16:
17: the License. You may obtain a copy of the License at
18:
19: http://www.apache.org/licenses/LICENSE-2.0
20:
21: Unless required by applicable law or agreed to in writing, software
22:
23: distributed under the License is distributed on an "AS IS" BASIS,
24:
25: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26:
27: See the License for the specific language governing permissions and
28:
29: limitations under the License.
30:
31: >
32:
33: omcat-users>
34:
35: --
36:
37: NOTE: By default, no user is included in the "manager-gui" role required
38:
39: to operate the "/manager/html" web application. If you wish to use this app,
40:
41: you must define such a user - the username and password are arbitrary.
42:
43: >
44:
45: --
46:
47: NOTE: The sample user and role entries below are wrapped in a comment
48:
49: and thus are ignored when reading this file. Do not forget to remove
50:
51: <!.. ..> that surrounds them.
52:
53: >
54:
55: --
56:
57: <role rolename="tomcat"/>
58:
59: <role rolename="role1"/>
60:
61: <user username="tomcat" password="tomcat" roles="tomcat"/>
62:
63: <user username="both" password="tomcat" roles="tomcat,role1"/>
64:
65: <user username="role1" password="tomcat" roles="role1"/>
66:
67: >
68:
69: tomcat-users>
70:
修改配置文件前,要养成首先备份一份配置文件的习惯,以防需要回滚到最初设置时,忘了自己修改过哪些配置。一般建议源文件+日期+bak
[root@DB-Server conf]# cp tomcat-users.xml tomcat-users.xml.20130308.bak
[root@DB-Server conf]# vi tomcat-users.xml
<role rolename="admin" />
<role rolename="manager" />
<user username="admin" password="Qwe!23" roles="admin,manager" />
修改了配置信息后,一般需要重新启动一下Tomcat才能生效,否则依然进入不了管理界面
Tomcat 7 下与Tomcat 6 稍有不同,如下所示:
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<user username="admin" password="xxxx" roles="admin-gui , manager-gui "/>
5:修改Tomcat的默认端口
有时候由于特性情况,需要修改Tomcat的默认端口8080,例如端口号被其它应用程序占用了等,这时你需要修改server.xml,在里面
找到如下图所示地方,修改为port的值。注意:修改后需要重启Tomcat服务。
6:修改Tomcat使用的JDK
首先我们先安装JDK1.6,如下所示
1: [root@DB-Server tmp]# chmod +x jdk-6u31-linux-x64-rpm.bin
2: [root@DB-Server tmp]# ./jdk-6u31-linux-x64-rpm.bin
3: Unpacking...
4: Checksumming...
5: Extracting...
6: UnZipSFX 5.50 of 17 February 2002, by Info-ZIP (Zip-Bugs@lists.wku.edu).
7: inflating: jdk-6u31-linux-amd64.rpm
8: inflating: sun-javadb-common-10.6.2-1.1.i386.rpm
9: inflating: sun-javadb-core-10.6.2-1.1.i386.rpm
10: inflating: sun-javadb-client-10.6.2-1.1.i386.rpm
11: inflating: sun-javadb-demo-10.6.2-1.1.i386.rpm
12: inflating: sun-javadb-docs-10.6.2-1.1.i386.rpm
13: inflating: sun-javadb-javadoc-10.6.2-1.1.i386.rpm
14: Preparing... ########################################### [100%]
15: 1:jdk ########################################### [100%]
16: Unpacking JAR files...
17: rt.jar...
18: jsse.jar...
19: charsets.jar...
20: tools.jar...
21: localedata.jar...
22: plugin.jar...
23: javaws.jar...
24: deploy.jar...
25: Installing JavaDB
26: Preparing... ########################################### [100%]
27: 1:sun-javadb-common ########################################### [ 17%]
28: 2:sun-javadb-core ########################################### [ 33%]
29: 3:sun-javadb-client ########################################### [ 50%]
30: 4:sun-javadb-demo ########################################### [ 67%]
31: 5:sun-javadb-docs ########################################### [ 83%]
32: 6:sun-javadb-javadoc ########################################### [100%]
33: Java(TM) SE Development Kit 6 successfully installed.
34: Product Registration is FREE and includes many benefits:
35: * Notification of new versions, patches, and updates
36: * Special offers on Oracle products, services and training
37: * Access to early releases and documentation
38: Product and system data will be collected. If your configuration
39: supports a browser, the JDK Product Registration form will
40: be presented. If you do not register, none of this information
41: will be saved. You may also register your JDK later by
42: opening the register.html file (located in the JDK installation
43: directory) in a browser.
44: For more information on what data Registration collects and
45: how it is managed and used, see:
46: http://java.sun.com/javase/registration/JDKRegistrationPrivacy.html
47: Press Enter to continue.....
48:
49: Done.
修改catalina.sh,增加export JAVA_HOME=/usr/java/jdk1.6.0_31即可。如下所示,似乎不用重启,Tomcat使用的JDK就改为了jdk1.6
[root@DB-Server bin]# ./shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-6.0.39
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-6.0.39
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-6.0.39/temp
Using JRE_HOME: /usr/java/jdk1.6.0_31
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-6.0.39/bin/bootstrap.jar
[root@DB-Server bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-6.0.39
Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-6.0.39
Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-6.0.39/temp
Using JRE_HOME: /usr/java/jdk1.6.0_31
Using CLASSPATH: /usr/local/tomcat/apache-tomcat-6.0.39/bin/bootstrap.jar
[root@DB-Server bin]#
另外,似乎网上也有文章介绍,在文件setclasspath.sh下加入export JAVA_HOME=/usr/java/jdk1.6.0_31 设置即可,那种比较合理,目前不得而知。
7:Tomcat端口被占用分析
有时候明明Tomcat配置OK,Tomcat服务启动正常,但是打开页面的时候出错。这时可能是Tomat端口被占用了,你可以查看一下那些程序占用了Tomcat的端口
[root@DB-Server bin]# netstat -an | grep 8080
[root@DB-Server bin]# netstat -apn | grep 8080
一般你也会在catalina.out日志里面找到”SEVERE: Error initializing endpoint java.net.BindException: Permission denied<null>:8080“之类的错误。
1: [root@DB-Server logs]$ more catalina.out
2: Mar 4, 2014 4:22:14 PM org.apache.catalina.core.AprLifecycleListener init
3: INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the ja
4: va.library.path: /usr/java/jdk1.6.0_31/jre/lib/amd64/server:/usr/java/jdk1.6.0_31/jre/lib/amd64:/usr/java/jdk1.6.0_31/jre/../lib/amd
5: 64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
6: Mar 4, 2014 4:22:15 PM org.apache.coyote.http11.Http11Protocol init
7: SEVERE: Error initializing endpoint
8: java.net.BindException: Permission denied<null>:8080
9: at org.apache.tomcat.util.net.JIoEndpoint.init(JIoEndpoint.java:502)
10: at org.apache.coyote.http11.Http11Protocol.init(Http11Protocol.java:176)
11: at org.apache.catalina.connector.Connector.initialize(Connector.java:1058)
12: at org.apache.catalina.core.StandardService.initialize(StandardService.java:677)
13: at org.apache.catalina.core.StandardServer.initialize(StandardServer.java:795)
14: at org.apache.catalina.startup.Catalina.load(Catalina.java:530)
15: at org.apache.catalina.startup.Catalina.load(Catalina.java:550)
16: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
17: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
18: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
19: at java.lang.reflect.Method.invoke(Method.java:597)
20: at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:260)
21: at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:412)
22: Mar 4, 2014 4:22:15 PM org.apache.catalina.startup.Catalina load
23: SEVERE: Catalina.start
这时要么修改Tomcat的默认端口号,要么修改其它应用程序的端口号。
Tomcat卸载
Tomcat的卸载非常简单,只需要将Tomcat的CATALINA_BASE目录删除,然后删除一些环境变量即可。
[root@DB-Server ~]# rm -rf /usr/local/tomcat/