1. tomcat简介
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,Tomcat 5支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。 Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。 诀窍是,当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Apache服务器。目前Tomcat最新版本为9.0。
tomcat就是传说中的中间件之一,tomcat本身是一个容器,专门用来运行java程序,java语言开发的网页.jsp就应该运行于tomcat中。而tomcat本身的运行也依赖于jdk环境。 tomcat应用场景:lnmt tomcat官网 :http://tomcat.apache.org/
2.tomcat 项目部署
tomcat应用场景:lnmt tomcat官网 :链接: http://tomcat.apache.org/.
2.1 tomcat部署多个项目
以部署两个项目为例:
//关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# vim /etc/selinux/config
**//将第六行中的 enforcing 改为 disabled**
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
[root@localhost ~]# setenforce 0
//安装之前请确保本地仓库可用
//安装jdk环境
[root@localhost ~]# yum -y install java-1.8.0*
……下载过程略
[root@localhost src]# cd /usr/local/
[root@localhost local]# mkdir tomcat
//下载tomcat
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz
……下载过程略
[root@localhost src]# ls
apache-tomcat-9.0.35.tar.gz debug kernels
//部署项目一
[root@localhost src]# tar xf apache-tomcat-9.0.35.tar.gz
[root@localhost src]# ls
apache-tomcat-9.0.35 apache-tomcat-9.0.35.tar.gz debug kernels
[root@localhost src]# mv apache-tomcat-9.0.35 tomcat1
[root@localhost src]# mv tomcat1 /usr/local/tomcat/
//部署项目二
[root@localhost src]# tar xf apache-tomcat-9.0.35.tar.gz
[root@localhost src]# ls
apache-tomcat-9.0.35 apache-tomcat-9.0.35.tar.gz debug kernels
[root@localhost src]# mv apache-tomcat-9.0.35 tomcat2
[root@localhost src]# mv tomcat2 /usr/local/tomcat/
[root@localhost src]# cd /usr/local/tomcat/
[root@localhost tomcat]# ls
tomcat1 tomcat2
//分别在两个项目中进行测试
//tomcat1
[root@localhost tomcat]# cd tomcat1/webapps/
[root@localhost webapps]# ls
docs examples host-manager manager ROOT
//创建测试文档
[root@localhost webapps]# mkdir test
[root@localhost webapps]# cd test/
//编写一个java页面进行测试
[root@localhost test]# vim index.jsp
[root@localhost test]# cat index.jsp
<html>
<head>
<title>test</title>
</head>
<body>
<%
out.println("I am tomcat1");
%>
</body>
</html>
//在tomcat2中的操作与tomcat1中相同
[root@localhost tomcat]# cd tomcat2/webapps/
[root@localhost webapps]# ls
docs examples host-manager manager ROOT
//创建测试文档
[root@localhost webapps]# mkdir test
[root@localhost webapps]# cd test/
//编写一个java页面进行测试
[root@localhost test]# vim index.jsp
[root@localhost test]# cat index.jsp
<html>
<head>
<title>test</title>
</head>
<body>
<%
out.println("I am tomcat1");
%>
</body>
</html>
//启动tomcat
//启动tomcat1,端口为8080和8005
[root@localhost ~]# /usr/local/tomcat/tomcat1/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat/tomcat1
Using CATALINA_HOME: /usr/local/tomcat/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/tomcat1/bin/bootstrap.jar:/usr/local/tomcat/tomcat1/bin/tomcat-juli.jar
Tomcat started.
[root@localhost bin]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
//tomcat2无法与tomcat1一样启动,因为他们的服务端口是一样的,如果再启动tomcat2就会有冲突。
//要想启动tomcat2,需要更改tomcat2的服务端口
[root@localhost ~]# cd /usr/local/tomcat/tomcat2/conf/
[root@localhost conf]# ls
catalina.policy context.xml jaspic-providers.xsd server.xml tomcat-users.xsd
catalina.properties jaspic-providers.xml logging.properties tomcat-users.xml web.xml
[root@localhost conf]# vim server.xml
.......
//将prot后的8005端口改为你想改的。
<Server port="8006" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
.......
.......
//将prot后的8080端口改为你想改的。
//将redirectPort后的8443端口改为你想改的。
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8445" />
.......
//改完后就可以启动tomcat2
[root@localhost ~]# /usr/local/tomcat/tomcat2/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat/tomcat2
Using CATALINA_HOME: /usr/local/tomcat/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/tomcat2/bin/bootstrap.jar:/usr/local/tomcat/tomcat2/bin/tomcat-juli.jar
Tomcat started.
[root@localhost bin]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 :::8080 :::*
LISTEN 0 100 :::8081 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8006 :::*
2.2 验证
2.3 开启管理模块
当点击图中说指的内容时,会被拒绝。
修改配置文件/usr/local/tomcat/conf/tomcat-users.xml
[root@localhost ~]# vim /usr/local/tomcat/conf/tomcat-users.xml
......
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="123456" roles="manager-gui,admin-gui"/>
</tomcat-users> ## 在此行前面添加以上内容
**注意:admin-gui是管理Host Manager项的,manager-gui是管理Server status和Manager App项
修改配置文件/usr/local/tomcat/webapps/manager/META-INF/context.xml**
<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.100\.1|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> ##此处要允许宿主机的ip访问
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
重启tomcat
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh stop
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@localhost ~]# /usr/local/tomcat/bin/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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
现在已经可以进入Server status和Manager App了,点击对应的按钮,输入密码即可进入,页面如下:
**但是现在还不能进入Host Manager
修改配置文件/usr/local/tomcat/webapps/host-manager/META-INF/context.xml**
<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.100\.1|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> ##同样加上允许宿主机ip访问
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
重启tomcat服务
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh stop
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@localhost ~]# /usr/local/tomcat/bin/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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
点击Host Manager的按钮,输入密码即可进入,页面如下: