tomcat部署实践

tomcat简介

Tomcat 是由 Apache 开发的一个 Servlet 容器,用来生成动态页面的。主要用来跑Java程序的Web应用服务器,属于轻量级应用服务器,

Tomcat和Nginx、Apache(httpd)、lighttpd等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Nginx/Apache服务器。

JDK是整个JAVA的核心,包括了JAVA运行环境,JAVA工具和基础类库等 (跑任何java程序或软件,都需要先安装jdk)

tomcat安装

准备好安装包

安装jdk

[root@tomcat opt]# tar xf jdk-8u45-linux-x64.tar.gz -C /usr/local/  --解压安装jdk
[root@tomcat opt]# ls /usr/local/jdk1.8.0_45/   --有目录文件确认是解压成功

设定环境变量
[root@tomcat local]# sed -i.ori '$a export JAVA_HOME=/usr/local/jdk1.8.0_45\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile

[root@tomcat local]# source /etc/profile

安装tomcat

[root@tomcat opt]# tar xf apache-tomcat-8.0.20.tar.gz -C /usr/local/    --解压安装tomcat
[root@tomcat local]# ln -s /usr/local/apache-tomcat-8.0.20 /usr/local/tomcat 

设定环境变量
[root@tomcat local]# echo 'export TOMCAT_HOME=/usr/local/tomcat'>>/etc/profile
[root@tomcat local]# source /etc/profile

注意授权,统一权限

[root@tomcat local]# chown root:root /usr/local/jdk1.8.0_45/  -R

检查tomcat是否安装成功

[root@tomcat local]# /usr/local/tomcat/bin/version.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_45/
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.20
Server built:   Feb 15 2015 18:10:42 UTC
Server number:  8.0.20.0
OS Name:        Linux
OS Version:     2.6.32-696.el6.x86_64
Architecture:   amd64
JVM Version:    1.8.0_45-b14
JVM Vendor:     Oracle Corporation

tomcat重要的目录介绍

[root@tomcat ~]# cd /usr/local/tomcat/
[root@tomcat tomcat]# ll
总用量 112
drwxr-xr-x 2 root root  4096 5月  12 12:25 bin       - Tomcat 脚本存放目录(如启动、关闭脚本)。
drwxr-xr-x 2 root root  4096 2月  16 2015 conf       - Tomcat 配置文件目录。
drwxr-xr-x 2 root root  4096 5月  12 12:25 lib       - web应用调用的jar包存放路径
-rw-r--r-- 1 root root 56977 2月  16 2015 LICENSE
drwxr-xr-x 2 root root  4096 2月  16 2015 logs       - Tomcat 默认日志目录。
-rw-r--r-- 1 root root  1397 2月  16 2015 NOTICE
-rw-r--r-- 1 root root  6781 2月  16 2015 RELEASE-NOTES
-rw-r--r-- 1 root root 16204 2月  16 2015 RUNNING.txt
drwxr-xr-x 2 root root  4096 5月  12 12:25 temp
drwxr-xr-x 7 root root  4096 2月  16 2015 webapps        - web程序存放目录。
drwxr-xr-x 2 root root  4096 2月  16 2015 work

[root@tomcat tomcat]# ll conf/
总用量 208
-rw------- 1 root root  12374 2月  16 2015 catalina.policy
-rw------- 1 root root   7074 2月  16 2015 catalina.properties
-rw------- 1 root root   1577 2月  16 2015 context.xml
-rw------- 1 root root   3387 2月  16 2015 logging.properties
-rw------- 1 root root   6458 2月  16 2015 server.xml        --tomcat 主配置文件
-rw------- 1 root root   1744 2月  16 2015 tomcat-users.xml
-rw------- 1 root root   1846 2月  16 2015 tomcat-users.xsd  --tomcat 管理用户配置文件
-rw------- 1 root root 163548 2月  16 2015 web.xml

[root@tomcat tomcat]# cd webapps/
[root@tomcat webapps]# ll
总用量 20
drwxr-xr-x 14 root root 4096 5月  12 12:25 docs          --帮助文档
drwxr-xr-x  6 root root 4096 5月  12 12:25 examples      --web应用实例
drwxr-xr-x  5 root root 4096 5月  12 12:25 host-manager
drwxr-xr-x  5 root root 4096 5月  12 12:25 manager
drwxr-xr-x  3 root root 4096 5月  12 12:25 ROOT          --默认网站根目录

tomcat管理命令

[root@tomcat tomcat]# /usr/local/tomcat/bin/startup.sh      --启动命令
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_45/
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat tomcat]# lsof -i:8080      --tomcat默认监听的端口是8080端口
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    1651 root   49u  IPv6  12410      0t0  TCP *:webcache (LISTEN)

[root@tomcat tomcat]# netstat -nltup|grep java      --tomcat的进程名是Java
tcp        0      0 ::ffff:127.0.0.1:8005       :::*              LISTEN      1709/java     
tcp        0      0 :::8009                     :::*              LISTEN      1709/java
tcp        0      0 :::8080                     :::*              LISTEN      1709/java           
[root@tomcat tomcat]# /usr/local/tomcat/bin/shutdown.sh     --关闭命令
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_45/
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

可以加到rc.local里做成开机自动启动
[root@tomcat tomcat]# echo /usr/local/tomcat/bin/startup.sh >> /etc/rc.local

在浏览器上进行访问 http://10.0.0.14:8080/ (tomcat的主机IP加8080端口)

查看主配置文件

[root@tomcat conf]# vim server.xml 
        注意: <!--    -->  这符号里的是注(下面是我截下来去掉了注释)
 1 <?xml version='1.0' encoding='utf-8'?>
 22 <Server port="8005" shutdown="SHUTDOWN">    --默认是8005端口监听来自本机的关闭口令
 23   <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
 28   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
 30   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
 31   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
 32   <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
 37   <GlobalNamingResources>
 41     <Resource name="UserDatabase" auth="Container"
 42               type="org.apache.catalina.UserDatabase"
 43               description="User database that can be updated and saved"
 44               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
 45               pathname="conf/tomcat-users.xml" />
 46   </GlobalNamingResources> 
 53   <Service name="Catalina">
 69     <Connector port="8080" protocol="HTTP/1.1"      --监听的默认端口和协议
 70                connectionTimeout="20000"            --超时时间(毫秒)
 71                redirectPort="8443" />
 91     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />    --AJP协议的监听端口(用不到注释掉就好)
103     <Engine name="Catalina" defaultHost="localhost">    --指定域名
114       <Realm className="org.apache.catalina.realm.LockOutRealm">
119         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
120                resourceName="UserDatabase"/>
121       </Realm>
123       <Host name="localhost"  appBase="webapps"     --配置虚拟主机名和指定站点目录
124             unpackWARs="true" autoDeploy="true">    --自动解压war包和自动加载(false不启用)
135         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
136                prefix="localhost_access_log" suffix=".txt"
137                pattern="%h %l %u %t "%r" %s %b" />  --配置日志相关的
139       </Host>
140     </Engine>
141   </Service>
142 </Server>

tomcat多实例

[root@tomcat local]# cd /usr/local/
[root@tomcat local]# cp -a apache-tomcat-8.0.20 tomcat1     --多一个配置文件就多一个实例
[root@tomcat local]# cp -a apache-tomcat-8.0.20 tomcat2

[root@tomcat local]# vim tomcat1/conf/server.xml    --修改配置文件下面几处
22 <Server port="8005" shutdown="SHUTDOWN">     --修改监听关闭口令的端口
69     <Connector port="8080" protocol="HTTP/1.1"   --修改每个实例监听不同的端口
91     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> --最好注释掉以免端口冲突被占用

sed命令修改端口的方法:
[root@tomcat local]# sed -i 's#8005#8011#;s#8080#8081#' tomcat1/conf/server.xml
[root@tomcat local]# sed -i 's#8005#8012#;s#8080#8082#' tomcat2/conf/server.xml

web站点部署
就是直接将程序目录放在webapps目录下面
或
将程序打包成war包,然后上传到webapps目录下面。

启动服务:
[root@tomcat local]# /usr/local/tomcat1/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat1
Using CATALINA_HOME:   /usr/local/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_45/
Using CLASSPATH:       /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.

[root@tomcat local]# /usr/local/tomcat2/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat2
Using CATALINA_HOME:   /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_45/
Using CLASSPATH:       /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.

[root@tomcat local]# netstat -nltup|grep java
tcp        0      0 ::ffff:127.0.0.1:8005       :::*              LISTEN      1644/java         tcp        0      0 :::8009                     :::*              LISTEN      1644/java         tcp        0      0 ::ffff:127.0.0.1:8011       :::*              LISTEN      2133/java         tcp        0      0 ::ffff:127.0.0.1:8012       :::*              LISTEN      2159/java         tcp        0      0 :::8080                     :::*              LISTEN      1644/java        tcp        0      0 :::8081                     :::*              LISTEN      2133/java         tcp        0      0 :::8082                     :::*              LISTEN      2159/java