首页 > 代码库 > rhel下面omcat+jdk高级应用
rhel下面omcat+jdk高级应用
一、Tomcat安装
安装JVM
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)
安装配置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. 谷歌浏览器测试访问。如下图
二、运行多个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. 谷歌浏览器测试访问
如果想运行第三、四、五....个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的默认
本文出自 “从心开始” 博客,请务必保留此出处http://hao360.blog.51cto.com/5820068/1551008
rhel下面omcat+jdk高级应用