首页 > 代码库 > Tomcat的安装和使用
Tomcat的安装和使用
Tomcat的安装和使用
===============================================================================
概述:
===============================================================================
Tomcat的安装及程序环境
1.安装Tomcat
★Tomcat binary release:
# tar xf apache-tomcat-VERSION.tar.gz -C /usr/local/
# cd /usr/local
# ln -sv apache-tomcat-VERSION tomcat
★Base Repo:
tomcat, tomcat-lib, tomcat-admin-webapps, tomcat-webapps, tomcat-docs-webapp
2.Tomcat程序环境
★二进制归档展开安装的程序环境
☉tomcat的目录结构
bin:脚本,及启动时用到的类;
conf:配置文件;
lib:库文件,Java类库,jar;
logs:日志文件目录;
temp:临时文件目录;
webapps:webapp的默认目录;
work:工作目录;
☉catalina.sh 启动时的脚本会调用参数(start、stop、version,configtest)
★rpm包安装的程序环境:
☉配置文件:
/etc/tomcat
☉webapps存放位置:/var/lib/tomcat/webapps/
examples
manager
host-manager
docs
★Tomcat的配置文件
☉server.xml:
主配置文件;
☉web.xml:
每个webapp只有“部署”后才能被访问,它的部署方式通常由web.xml进行定义,其存放位置为WEB-INF/目录中;此文件(主目录下的)为所有的webapps提供默认配置;
☉context.xml:
每个web都可以专用的配置文件,它通常由专用的配置文件context.xml来定义,其存放位置为WEB-INF/目录中;此文件为所有的webapps提供默认配置;
☉tomcat-users.xml
用户认证的账号和密码文件;
☉catalina.policy
当使用-security选项启动tomcat时,用于为tomcat设置安全策略;
☉catalina.properties:
Java属性的定义文件,用于设定类加载器路径,以及一些与JVM调优相关参数;
☉logging.properties:
日志系统相关的配置;
二进制程序包(binary)安装启动演示:
1.在官方下载tomcat的二进制程序包,并解压到/usr/local目录下,创建软连接
[root@centos7 tomcat]# ls apache-tomcat-8.0.23.tar.gz # 下载的二进制程序包 [root@centos7 tomcat]# tar xf apache-tomcat-8.0.23.tar.gz -C /usr/local #解压到/usr/local目录中 [root@centos7 tomcat]# cd /usr/local [root@centos7 local]# ll total 0 drwxr-xr-x 9 root root 149 Dec 16 15:53 apache-tomcat-8.0.23 drwxr-xr-x. 2 root root 6 Aug 12 2015 bin drwxr-xr-x. 2 root root 6 Aug 12 2015 etc drwxr-xr-x. 2 root root 6 Aug 12 2015 games drwxr-xr-x 14 root root 151 Aug 24 22:19 http2 drwxr-xr-x. 2 root root 6 Aug 12 2015 include drwxr-xr-x. 2 root root 6 Aug 12 2015 lib drwxr-xr-x. 2 root root 6 Aug 12 2015 lib64 drwxr-xr-x. 2 root root 6 Aug 12 2015 libexec drwxr-xr-x. 2 root root 6 Aug 12 2015 sbin drwxr-xr-x. 5 root root 46 Jul 25 23:57 share drwxr-xr-x. 2 root root 6 Aug 12 2015 src [root@centos7 local]# ln -sv apache-tomcat-8.0.23 tomcat # 创建软连接 ‘tomcat’ -> ‘apache-tomcat-8.0.23’ [root@centos7 local]# ll total 0 drwxr-xr-x 2 root root 6 Dec 16 15:54 apache-tomcat-8.0.23 drwxr-xr-x. 2 root root 6 Aug 12 2015 bin drwxr-xr-x. 2 root root 6 Aug 12 2015 etc drwxr-xr-x. 2 root root 6 Aug 12 2015 games drwxr-xr-x 14 root root 151 Aug 24 22:19 http2 drwxr-xr-x. 2 root root 6 Aug 12 2015 include drwxr-xr-x. 2 root root 6 Aug 12 2015 lib drwxr-xr-x. 2 root root 6 Aug 12 2015 lib64 drwxr-xr-x. 2 root root 6 Aug 12 2015 libexec drwxr-xr-x. 2 root root 6 Aug 12 2015 sbin drwxr-xr-x. 5 root root 46 Jul 25 23:57 share drwxr-xr-x. 2 root root 6 Aug 12 2015 src lrwxrwxrwx 1 root root 20 Dec 16 15:54 tomcat -> apache-tomcat-8.0.23
2.查看Tomcat程序环境的组成结构
[root@centos7 local]# cd tomcat/ [root@centos7 tomcat]# ll total 96 drwxr-xr-x 2 root root 4096 Dec 16 16:01 bin # 二进制程序,因为是java程序,所以主要为脚本 drwxr-xr-x 2 root root 4096 May 19 2015 conf # 配置文件目录 drwxr-xr-x 2 root root 4096 Dec 16 16:01 lib # 库文件,打包为.jar文件 -rw-r--r-- 1 root root 56977 May 19 2015 LICENSE drwxr-xr-x 2 root root 6 May 19 2015 logs # 日志文件 -rw-r--r-- 1 root root 1397 May 19 2015 NOTICE -rw-r--r-- 1 root root 6741 May 19 2015 RELEASE-NOTES -rw-r--r-- 1 root root 16204 May 19 2015 RUNNING.txt drwxr-xr-x 2 root root 29 Dec 16 16:01 temp # 临时文件 drwxr-xr-x 7 root root 76 May 19 2015 webapps # 应用程序的根目录 drwxr-xr-x 2 root root 6 May 19 2015 work # 工作目录。 [root@centos7 tomcat]# ls bin/ bootstrap.jar catalina-tasks.xml configtest.bat digest.bat setclasspath.sh startup.bat tomcat-native.tar.gz version.bat catalina.bat commons-daemon.jar configtest.sh digest.sh shutdown.bat startup.sh tool-wrapper.bat version.sh catalina.sh commons-daemon-native.tar.gz daemon.sh setclasspath.bat shutdown.sh tomcat-juli.jar tool-wrapper.sh [root@centos7 tomcat]# ls conf/ catalina.policy catalina.properties context.xml logging.properties server.xml tomcat-users.xml tomcat-users.xsd web.xml [root@centos7 tomcat]# ls lib/ annotations-api.jar catalina.jar ecj-4.4.2.jar jasper.jar tomcat-api.jar tomcat-i18n-es.jar tomcat-jdbc.jar tomcat-util-scan.jar catalina-ant.jar catalina-storeconfig.jar el-api.jar jsp-api.jar tomcat-coyote.jar tomcat-i18n-fr.jar tomcat-jni.jar tomcat-websocket.jar catalina-ha.jar catalina-tribes.jar jasper-el.jar servlet-api.jar tomcat-dbcp.jar tomcat-i18n-ja.jar tomcat-util.jar websocket-api.jar [root@centos7 tomcat]# ls webapps/ docs examples host-manager manager ROOT [root@centos7 tomcat]# ls work/ # 每一个jsp程序在运行前要想转化为servlet代码,然后编译。整个中间过程的存放位置
3.使用catalina.sh脚本调用version参数查看版本
[root@centos7 tomcat]# bin/catalina.sh version Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Server version: Apache Tomcat/8.0.23 Server built: May 19 2015 14:58:38 UTC Server number: 8.0.23.0 OS Name: Linux OS Version: 3.18.41-1.0-Taolinux Architecture: amd64 JVM Version: 1.8.0_25-b17 JVM Vendor: Oracle Corporation
4.tomcat的启用最好用普通用户,所以创建tomcat用户,并修改目录的属主和属组,启动服务,如下:
[root@centos7 tomcat]# useradd -r tomcat [root@centos7 tomcat]# chown -R tomcat.tomcat /usr/local/tomcat/* [root@centos7 tomcat]# ll total 96 drwxr-xr-x 2 tomcat tomcat 4096 Dec 16 16:01 bin drwxr-xr-x 2 tomcat tomcat 4096 May 19 2015 conf drwxr-xr-x 2 tomcat tomcat 4096 Dec 16 16:01 lib -rw-r--r-- 1 tomcat tomcat 56977 May 19 2015 LICENSE drwxr-xr-x 2 tomcat tomcat 6 May 19 2015 logs -rw-r--r-- 1 tomcat tomcat 1397 May 19 2015 NOTICE -rw-r--r-- 1 tomcat tomcat 6741 May 19 2015 RELEASE-NOTES -rw-r--r-- 1 tomcat tomcat 16204 May 19 2015 RUNNING.txt drwxr-xr-x 2 tomcat tomcat 29 Dec 16 16:01 temp drwxr-xr-x 7 tomcat tomcat 76 May 19 2015 webapps drwxr-xr-x 2 tomcat tomcat 6 May 19 2015 work # 使用tomcat用户启动服务 [root@centos7 tomcat]# su - tomcat -c ‘/usr/local/tomcat/bin/catalina.sh start‘ su: warning: cannot change directory to /home/tomcat: No such file or directory Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. # 提示启动成功 # 查看端口8080,8009 [root@centos7 tomcat]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 25 *:514 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 127.0.0.1:6010 *:* LISTEN 0 128 127.0.0.1:6011 *:* LISTEN 0 25 :::514 :::* LISTEN 0 100 :::8009 :::* LISTEN 0 100 :::8080 :::* LISTEN 0 128 :::22 :::* LISTEN 0 128 ::1:631 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 128 ::1:6010 :::* LISTEN 0 128 ::1:6011 :::*
5.浏览器访问如下:
6.编辑/etc/profile.d/配置文件,输出CATALINA_BASE环境变量
[root@centos7 ~]# vim /etc/profile.d/tomcat.sh # 添加环境变量 export CATALINA_BASE=/usr/local/tomcat export PATH=$CATALINA_BASE/bin:$PATH [root@centos7 ~]# . /etc/profile.d/tomcat.sh # 重读配置文件生效 [root@centos7 ~]# catalina.sh version # 查看版本 Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Server version: Apache Tomcat/8.0.23 Server built: May 19 2015 14:58:38 UTC Server number: 8.0.23.0 OS Name: Linux OS Version: 3.18.41-1.0-Taolinux Architecture: amd64 JVM Version: 1.8.0_25-b17 JVM Vendor: Oracle Corporation
--------------------------------------------------------------------------------
Base Repo安装启动演示:
1.安装程序包
[root@centos7 ~]# yum list all tomcat* # 查看tomcat相关程序包如下: Available Packages tomcat.noarch 7.0.54-2.el7_1 CDROM tomcat-admin-webapps.noarch 7.0.54-2.el7_1 CDROM tomcat-docs-webapp.noarch 7.0.54-2.el7_1 CDROM tomcat-el-2.2-api.noarch 7.0.54-2.el7_1 CDROM tomcat-javadoc.noarch 7.0.54-2.el7_1 CDROM tomcat-jsp-2.2-api.noarch 7.0.54-2.el7_1 CDROM tomcat-jsvc.noarch 7.0.54-2.el7_1 CDROM tomcat-lib.noarch 7.0.54-2.el7_1 CDROM tomcat-native.x86_64 1.1.34-1.el7 epel tomcat-servlet-3.0-api.noarch 7.0.54-2.el7_1 CDROM tomcat-webapps.noarch 7.0.54-2.el7_1 CDROM tomcatjss.noarch 7.1.2-1.el7 CDROM # 安装程序包,这里会自动解决依赖关系 [root@centos7 ~]# yum install -y tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp
2.查看程序环境
[root@centos7 ~]# rpm -ql tomcat /etc/logrotate.d/tomcat /etc/sysconfig/tomcat /etc/tomcat # 配置文件目录 /etc/tomcat/Catalina /etc/tomcat/Catalina/localhost /etc/tomcat/catalina.policy /etc/tomcat/catalina.properties /etc/tomcat/context.xml /etc/tomcat/log4j.properties /etc/tomcat/logging.properties /etc/tomcat/server.xml /etc/tomcat/tomcat-users.xml /etc/tomcat/tomcat.conf /etc/tomcat/web.xml /usr/bin/tomcat-digest /usr/bin/tomcat-tool-wrapper /usr/lib/systemd/system/tomcat.service # Until File文件 /usr/lib/systemd/system/tomcat@.service /usr/lib/tmpfiles.d/tomcat.conf /usr/libexec/tomcat /usr/libexec/tomcat/functions /usr/libexec/tomcat/preamble /usr/libexec/tomcat/server /usr/sbin/tomcat # 程序文件 /usr/share/doc/tomcat-7.0.54 /usr/share/doc/tomcat-7.0.54/LICENSE /usr/share/doc/tomcat-7.0.54/NOTICE /usr/share/doc/tomcat-7.0.54/RELEASE-NOTES /usr/share/tomcat /usr/share/tomcat/bin/bootstrap.jar /usr/share/tomcat/bin/catalina-tasks.xml /usr/share/tomcat/conf /usr/share/tomcat/lib /usr/share/tomcat/logs /usr/share/tomcat/temp /usr/share/tomcat/webapps /usr/share/tomcat/work /var/cache/tomcat /var/cache/tomcat/temp /var/cache/tomcat/work /var/lib/tomcat /var/lib/tomcat/webapps /var/lib/tomcats /var/log/tomcat /var/log/tomcat/catalina.out /var/run/tomcat.pid [root@centos7 ~]# ll /etc/tomcat/ # 配置文件 total 208 drwxrwxr-x 3 root tomcat 22 Dec 16 16:27 Catalina -rw-rw-r-- 1 tomcat tomcat 12257 May 13 2015 catalina.policy -rw-rw-r-- 1 tomcat tomcat 6294 May 13 2015 catalina.properties -rw-rw-r-- 1 tomcat tomcat 1394 May 13 2015 context.xml -rw-rw-r-- 1 tomcat tomcat 547 May 13 2015 log4j.properties -rw-rw-r-- 1 tomcat tomcat 3288 May 13 2015 logging.properties -rw-rw-r-- 1 tomcat tomcat 6536 May 13 2015 server.xml -rw-rw-r-- 1 tomcat tomcat 1568 May 13 2015 tomcat.conf -rw-rw---- 1 tomcat tomcat 1998 May 13 2015 tomcat-users.xml -rw-rw-r-- 1 tomcat tomcat 163385 May 13 2015 web.xml [root@centos7 ~]# ll /var/lib/tomcat # 查看webapps文件 total 0 drwxrwxr-x 8 root tomcat 89 Dec 16 16:27 webapps [root@centos7 ~]# ll /var/lib/tomcat/webapps/ total 8 drwxr-xr-x 14 root root 4096 Dec 16 16:27 docs drwxr-xr-x 8 tomcat tomcat 120 Dec 16 16:27 examples drwxr-xr-x 5 root tomcat 82 Dec 16 16:27 host-manager drwxr-xr-x 5 root tomcat 97 Dec 16 16:27 manager drwxr-xr-x 3 tomcat tomcat 4096 Dec 16 16:27 ROOT drwxr-xr-x 5 tomcat tomcat 81 Dec 16 16:27 sample
3.启动tomcat并查看端口8080
[root@centos7 ~]# systemctl start tomcat.service # 直接启动tomcat服务 [root@centos7 ~]# ss -tnl # 查看监听端口8080,8009 State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 127.0.0.1:6010 *:* LISTEN 0 100 :::8009 :::* LISTEN 0 100 :::8080 :::* LISTEN 0 128 :::22 :::* LISTEN 0 128 ::1:631 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 128 ::1:6010 :::*
4.在浏览器中访问如下:
本文出自 “逐梦小涛” 博客,请务必保留此出处http://1992tao.blog.51cto.com/11606804/1883477
Tomcat的安装和使用