rhel下面tomcat+jdk高级应用

本文转载:http://467754239.blog.51cto.com/4878013/1405217 

          http://pvbutler.blog.51cto.com/7662323/1550525 

一、Tomcat安装

系统环境

[root@localhost ~]# file /sbin/init

/sbin/init: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

[root@localhost ~]# cat /etc/redhat-release 

rhel release 6.5 (Final)


安装JVM

下载:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html 

1. 安装JDK

[root@allentuns src]# rpm -ivh jdk-7u45-linux-x64.rpm

2. 配置JDK的环境变量

[root@allentuns src]# find / -name java  #查看jdk的安装路径

var/lib/alternatives/java
/etc/java
/etc/pki/java
/etc/alternatives/java
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64/bin/java
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64/jre/bin/java
/usr/lib/jvm/java
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/bin/java
/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/bin/java
/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre/bin/java
/usr/lib/java
/usr/lib/jvm-exports/java
/usr/bin/java
/usr/lib64/java
/usr/share/doc/db4-devel-4.7.25/java
/usr/share/doc/db4-devel-4.7.25/ref/java
/usr/share/javadoc/java-1.6.0-openjdk/jre/api/plugin/dom/com/sun/java
/usr/share/javadoc/java-1.6.0-openjdk/api/java
/usr/share/javadoc/java-1.6.0-openjdk/api/org/omg/stub/java
/usr/share/javadoc/java
/usr/share/swig/1.3.40/java
/usr/share/java
/usr/java
/usr/java/jdk1.7.0_67/bin/java
/usr/java/jdk1.7.0_67/jre/bin/java
[root@allentuns src]# vim /etc/profile.d/java.sh #指明jdk中二进制文件的路径
export JAVA_HOME=/usr/java/jdk1.7.0_67/
export PATH=$PATH:$JAVA_HOME/bin/
[root@allentuns src]# source /etc/profile #重新载入环境变量,立刻生效

3. 验证JDK是否安装成功;如果出现如下图,说明JDK安装成功

[root@allentuns src]# java -version
java version "1.7.0_09-icedtea"
OpenJDK Runtime Environment (rhel-2.3.4.1.el6_3-x86_64)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)


或者按下面方法配置java

选择要安装java的位置,如/usr/目录下,新建文件夹java(mkdir java)

将文件jdk-7u67-linux-x64.tar.gz移动到/usr/java/

tar -xf jdk-7u67-linux-x64.tar.gz -C /usr/java/

打开/etc/profile.d/

vim java.sh

export JAVA_HOME=/usr/java/jdk1.7.0_67
CLASSPATH=.:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME CLASSPATH PATH

source /etc/profile

验证是否安装成功:java -version

java -version
java version "1.7.0_09-icedtea"
OpenJDK Runtime Environment (rhel-2.3.4.1.el6_3-x86_64)
OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)



安装配置Tomcat

1. 解压、重命名tomcat

wget 
[root@allentuns src]# tar xf apache-tomcat-7.0.55.tar.gz -C /usr/local/
[root@allentuns src]# cd /usr/local/
[root@allentuns local]# mv apache-tomcat-7.0.55 tomcat

2. 配置Tomcat的环境变量

[root@allentuns local]# vim /etc/profile.d/tomcat.sh
export CATALINE_HOME=/usr/local/tomcat/
export PATH=$PATH:$CATALINE_HOME/bin/
[root@allentuns local]# source /etc/profile

3. 验证Tomcat是否安装成功;如果出现如下图,说明Tomcat安装成功

[root@allentuns local]# catalina.sh -h
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_55/
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Usage: catalina.sh ( commands ... )
commands:
debug             Start Catalina in a debugger
debug -security   Debug Catalina with a security manager
jpda start        Start Catalina under JPDA debugger
run               Start Catalina in the current window
run -security     Start in the current window with security manager
start             Start Catalina in a separate window
start -security   Start in a separate window with security manager
stop              Stop Catalina, waiting up to 5 seconds for the process to end
stop n            Stop Catalina, waiting up to n seconds for the process to end
stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running
configtest        Run a basic syntax check on server.xml - check exit code for result
version           What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is

4. 启动Tomcat服务

[root@allentuns local]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_67/
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

5. 查看Tomcat的启动、停止日志输出记录

[root@allentuns local]# tail -f /usr/local/tomcat/logs/catalina.out
May 02, 2014 12:59:36 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
May 02, 2014 12:59:36 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1456 ms
May 02, 2014 12:59:36 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
May 02, 2014 12:59:36 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.55
May 02, 2014 12:59:36 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/examples
May 02, 2014 12:59:53 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [15,044] milliseconds.
May 02, 2014 12:59:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/docs
May 02, 2014 12:59:54 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/host-manager
May 02, 2014 12:59:54 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/ROOT
May 02, 2014 12:59:54 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/manager
May 02, 2014 12:59:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
May 02, 2014 12:59:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
May 02, 2014 12:59:54 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 18072 ms

6. 为Tomcat提供SysV脚本

[root@allentuns local]# vim /etc/init.d/tomcat
#!/bin/sh
# Tomcat init script for Linux.
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
JAVA_HOME=/usr/java/jdk1.7.0_67/
CATALINA_HOME=/usr/local/tomcat/
export JAVA_HOME CATALINA_HOME
# export CATALINA_OPTS="-Xms128m -Xmx256m"
# Java使用的内存:-Xms128m默认分配的内存大小,-Xmx256m最大使用的内存。
exec $CATALINA_HOME/bin/catalina.sh $*

7. 为Tomcat脚本赋予执行权限并加入到服务控制列表,设置开机启动

[root@allentuns local]# chmod +x /etc/init.d/tomcat
[root@allentuns local]# chkconfig --add tomcat
[root@allentuns local]# chkconfig tomcat on
[root@allentuns local]# chkconfig tomcat --list
tomcat          0:off   1:off   2:on    3:on    4:on    5:on    6:off

8. 脚本测试,观察日志的记录

[root@Test03 init.d]# service tomcat stop
Using CATALINA_BASE:   /usr/local/tomcat/
Using CATALINA_HOME:   /usr/local/tomcat/
Using CATALINA_TMPDIR: /usr/local/tomcat//temp
Using JRE_HOME:        /usr/java/jdk1.7.0_67/
Using CLASSPATH:       /usr/local/tomcat//bin/bootstrap.jar:/usr/local/tomcat//bin/tomcat-juli.jar
[root@Test03 init.d]# service tomcat start
Using CATALINA_BASE:   /usr/local/tomcat/
Using CATALINA_HOME:   /usr/local/tomcat/
Using CATALINA_TMPDIR: /usr/local/tomcat//temp
Using JRE_HOME:        /usr/java/jdk1.7.0_67/
Using CLASSPATH:       /usr/local/tomcat//bin/bootstrap.jar:/usr/local/tomcat//bin/tomcat-juli.jar
Tomcat started.

9. 谷歌浏览器测试访问。如下图

wKiom1QRDnjzhUAYAAKi7FGvKpE937.jpg

二、运行多个tomcat Server

以下操作是在上一步tomcat1的基础上,再次安装配置运行tomcat2

1. 解压、重命名tomcat2

[root@allentuns src]# tar xf apache-tomcat-7.0.55.tar.gz -C /usr/local/
[root@allentuns src]# cd /usr/local/
[root@allentuns local]# mv apache-tomcat-7.0.55 tomcat2

2. 配置Tomcat2的环境变量

[root@allentuns ~]# vim /etc/profile.d/tomcat2.sh
export CATALINE_HOME=/usr/local/tomcat2/
export PATH=$PATH:$CATALINE_HOME/bin/
[root@allentuns ~]# source /etc/profile

3. 修改Tomcat2的配置文件;如下图

[root@allentuns ~]# cd /usr/local/tomcat2/conf/
[root@allentuns conf]# cp server.xml server.xml.bak
[root@allentuns conf]# vim server.xml
修改配置文件中第22、70、91行的端口号,默认端口+1
22 <Server port="8006" shutdown="SHUTDOWN">
70 <Connector port="8081" protocol="HTTP/1.1"
91 <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

4. 为Tomcat2脚本赋予执行权限并加入到服务控制列表

[root@allentuns conf]# vim /etc/init.d/tomcat2
#!/bin/sh
# Tomcat init script for Linux.
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
JAVA_HOME=/usr/java/jdk1.7.0_67/
CATALINA_HOME=/usr/local/tomcat2/
export JAVA_HOME CATALINA_HOME
# export CATALINA_OPTS="-Xms128m -Xmx256m"
# Java使用的内存:-Xms128m默认分配的内存大小,-Xmx256m最大使用的内存。
exec $CATALINA_HOME/bin/catalina.sh $*
[root@allentuns conf]# chmod +x /etc/init.d/tomcat2
[root@allentuns conf]# chkconfig --add tomcat2

注意:Tomcat2最好不要设置为开机启动,使Tomcat和Tomcat2先后启动,否则会有意外错误

5. 分别启动Tomca和Tomcat2

[root@allentuns ~]# netstat -tnlp |grep java
[root@allentuns ~]# service tomcat start  #启动tomcat1
Using CATALINA_BASE:   /usr/local/tomcat/
Using CATALINA_HOME:   /usr/local/tomcat/
Using CATALINA_TMPDIR: /usr/local/tomcat//temp
Using JRE_HOME:        /usr/java/jdk1.7.0_45/
Using CLASSPATH:       /usr/local/tomcat//bin/bootstrap.jar:/usr/local/tomcat//bin/tomcat-juli.jar
[root@allentuns ~]# netstat -tnlp |grep java
tcp        0      0 :::8080                     :::*                        LISTEN      2638/java
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      2638/java
tcp        0      0 :::8009                     :::*                        LISTEN      2638/java
[root@allentuns ~]# service tomcat2 start #启动tomcat2
Using CATALINA_BASE:   /usr/local/tomcat2/
Using CATALINA_HOME:   /usr/local/tomcat2/
Using CATALINA_TMPDIR: /usr/local/tomcat2//temp
Using JRE_HOME:        /usr/java/jdk1.7.0_45/
Using CLASSPATH:       /usr/local/tomcat2//bin/bootstrap.jar:/usr/local/tomcat2//bin/tomcat-juli.jar
[root@allentuns ~]# netstat -tnlp |grep java
tcp        0      0 :::8080                     :::*                        LISTEN      2638/java
tcp        0      0 :::8081                     :::*                        LISTEN      2701/java
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      2638/java
tcp        0      0 ::ffff:127.0.0.1:8006       :::*                        LISTEN      2701/java
tcp        0      0 :::8009                     :::*                        LISTEN      2638/java
tcp        0      0 :::8010                     :::*                        LISTEN      2701/java

6. 分别为Tomcat和Tomcat2提供不同的访问页面,测试两个Tomcat是否都能正常工作

[root@allentuns ~]# echo "Hello,I am Tomcat1" > /usr/local/tomcat/webapps/ROOT/index.html  #为Tomcat1提供测试页面
[root@allentuns ~]# echo "Hello,I am Tomcat2" > /usr/local/tomcat2/webapps/ROOT/index.html #为Tomcat2提供测试页面

7. 谷歌浏览器测试访问

wKiom1QREeLC-AtVAABzxz9Ig0A820.jpg

wKioL1QREiqAsT_8AABsc-vdRN0753.jpg如果想运行第三、四、五....个Tomcat;只需要重复Tomcat2操作即可

参考学习:http://tonychiu.blog.51cto.com/656605/947266 


三、tomcat虚拟主机简单应用

接下来虚拟主机的实验是在tomcat1的基础上进行完成,首先停掉tomcat2的服务

1. 停止tomcat2服务

[root@allentuns local]# service tomcat2 stop
Using CATALINA_BASE:   /usr/local/tomcat2/
Using CATALINA_HOME:   /usr/local/tomcat2/
Using CATALINA_TMPDIR: /usr/local/tomcat2//temp
Using JRE_HOME:        /usr/java/jdk1.7.0_45/
Using CLASSPATH:       /usr/local/tomcat2//bin/bootstrap.jar:/usr/local/tomcat2//bin/tomcat-juli.jar
[root@allentuns local]# netstat -tnlp |grep java
tcp        0      0 :::8080                     :::*                        LISTEN      2638/java
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      2638/java
tcp        0      0 :::8009                     :::*                        LISTEN      2638/java

2. tomcat的默认

vim /usr/local/tomcat/conf/server.xml
<Host name="localhost"  appBase="webapps"     #定义一个虚拟主机
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" #定义日志的格式
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>
参数解释
<
Host:主机 定义一个虚拟主机
name:要访问的虚拟主机域名或者ip地址
appBase="webapps":定义这个主机应用程序路径,这里是相对于tomcat的安装路径而言
npackWARs="true":让tomcat可以自动解压war文件
autoDeploy="true":自动部署
>

3. 自定义虚拟主机

<Host name="www.allentunsart.com"  appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/www/allentuns" reloadable="true" />

参数解释

path="":相对于docBase的路径

docBase="/www/allentuns":网页文件的存放路径,

reloadable="true":是否允许自动装载

4. 重启tomcat服务

service tomcat stop
service tomcat start

5. 创建站点目录和测试文件

mkdir  -pv /www/allentuns
echo "<h1>zhengyansheng</h1>" > /www/allentuns/index.html

6. 由于没有DNS服务器做域名解析,采用物理主机的hosts文件解析C:\Windows\System32\drivers\etc\hosts

192.168.1.6     www.allentuns.com

7. 谷歌浏览器访问 http://www.allentuns.com:8080

wKioL1Njk27ga-nIAAEefPIYKVg010.jpg

 8 自由的tomcat虚拟主机

第一种方法
放弃默认的ROOT目录,该成我们想要的目录站点,如下设置
<Host name="web1.allentuns.com"  appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="allentuns" reloadable="true" />
</Host>
注释:Context中的docBase来指定要访问的站点目录
第二种方法:
用绝对路径来替换tomcat的默认安装路径webapps!如下
<Host name="web2.allentuns.com"  appBase="/www/html"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/www/html" reloadable="true" />
</Host>
注释:/www/html可以随便定义

四、自定义虚拟主机的注意事项 参考博文http://xyuex.blog.51cto.com/5131937/1040006

1.  介绍WEB-INF

WEB-INF:这个目录必须要有,存放的是当前应用程序的私有资源,如部署描述符文件web.xml ,所有放在 "WEB-INF" 这个目录下的 ‘资源(内容)’ 都是 “私有资源”,只能在当前服务器上被当前应用程序所访问.

[root@AY140417124338794186Z live]# cd WEB-INF/
[root@AY140417124338794186Z WEB-INF]# ll
total 16
drwxr-xr-x 4 root root 4096 Apr 22 15:01 classes
drwxr-xr-x 2 root root 4096 Apr 23 13:07 lib
drwxr-xr-x 8 root root 4096 Apr 18 14:09 view
-rw-r--r-- 1 root root 2215 Apr 18 14:09 web.xml
web.xml     当前应用程序的部署描述符文件,此文件可以复制 conf/web.xml  来获得
classes/*    可能存放的是编译好的类的字节码(可执行的字节码)
lib/*        打包好的jar文件,包含定义好的类

2. 定义一个完整的虚拟主机:域名www.virtual.com

<Host name="www.virtual.com"  appBase="/www/virtual"
unpackWARs="true" autoDeploy="true">
Context path="" docBase="/www/virtual" reloadable="true" />
</Host>

3. 创建虚拟主机所需的相关目录和文件

[root@allentuns ~]# mkdir /www/virtual
[root@allentuns ~]# mkdir /www/virtual/WEB-INF
[root@allentuns ~]# mkdir /www/virtual/WEB-INF/classes
[root@allentuns ~]# mkdir /www/virtual/WEB-INF/lib
[root@allentuns ~]# cp /usr/local/tomcat/conf/web.xml  /www/virtual/WEB-INF/
[root@allentuns ~]# tree -a /www/virtual/
/www/virtual/
├── index.jsp
└── WEB-INF
├── classes
├── lib
└── web.xml
3 directories, 2 files
[root@allentuns ~]# vim /www/virtual/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA </font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("abc","abc"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>

4. 重新启动tomcat服务

service tomcat stop
service tomcat start
tail -f /usr/local/tomcat/logs/catalina.out

5. 同上修改hosts文件对应的域名

6. 谷歌浏览器访问站点

wKiom1Njo7Sj4L6AAAFNT7TJ7xU336.jpg

附上一份nginx反向代理tomcat的配置文件

proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=static:10m inactive=1h max_size=1g;
 
upstream tomcat {
        ip_hash ;
        #hash           $remote_addr consistent;
        server          127.0.0.1:8081 max_fails=1 fail_timeout=2s ;
        server          127.0.0.1:8082 max_fails=1 fail_timeout=2s ; ;
        keepalive       16;
}
 
server {
        listen          80;
        server_name     tomcat.example.com;
 
        charset         utf-8;
        access_log      /var/log/nginx/tomcat.access.log  main;
        root            /usr/share/nginx/html;
        index           index.html index.htm index.jsp;
 
        location / {
                proxy_pass              http://tomcat;
                proxy_redirect          off;
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;
 
                proxy_connect_timeout   300;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";
 
                add_header              X-Backend "$upstream_addr";
        }
 
        location ~* ^.+\.(js|css|ico|gif|jpg|jpeg|png)$ {
                proxy_pass              http://tomcat ;
                proxy_redirect          off;
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;
 
                proxy_connect_timeout   300;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";
 
                proxy_cache             static;
                proxy_cache_key         $host$uri$is_args$args;
                proxy_cache_valid       200 302 5m;
                proxy_cache_valid       404 1m;
                proxy_cache_valid       any 1h;
                add_header              X-Cache $upstream_cache_status;
 
                #log_not_found off;
                #access_log off;
                expires max;
        }
 
        location ~ /\.ht {
                deny  all;
        }
 
}


五、Tomcat优化

参考:http://www.cnblogs.com/ggjucheng/archive/2013/04/16/3024731.html 

     http://www.oschina.net/translate/tomcat-performance-tuning 

     http://feihan21.blog.51cto.com/1364153/1320295 

你可能感兴趣的:(Tomcate)