一、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最新版本为10.0.23
二、Tomcat部署安装
关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0[root@localhost ~]# vim /etc/selinux/config
安装依赖包
[root@localhost ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
下载tomcat二进制包
[root@localhost ~]# yum -y install wget
[root@localhost ~]# wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.80/bin/apache-tomcat-9.0.80.tar.gz
解压tomcat包并安装
[root@localhost ~]# tar xf apache-tomcat-9.0.80.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ls
apache-tomcat-9.0.80 etc include lib64 sbin src
bin games lib libexec share
[root@localhost local]# ln -s apache-tomcat-9.0.80 tomcat
[root@localhost local]# ls
apache-tomcat-9.0.80 etc include lib64 sbin src
bin games lib libexec share tomcat
写一个Java页面
[root@localhost ~]# vim index.jsp
[root@localhost ~]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("chenxi");
%>
</body>
</html>
创建网页目录,把Java页面放进去
[root@localhost ~]# mkdir /usr/local/tomcat/webapps/test
[root@localhost ~]# cp /usr/local/index.jsp /usr/local/tomcat/webapps/test/
[root@localhost ~]# ll /usr/local/tomcat/webapps/test/
total 4
-rw-r--r--. 1 root root 113 Oct 11 16:10 index.jsp
[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
Using CATALINA_OPTS:
Tomcat started.
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*[root@localhost ~]# ps -ef|grep tomcat
root 11029 1 5 16:11 pts/0 00:00:02 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 11064 1597 0 16:12 pts/0 00:00:00 grep --color=auto tomcat
没有在8080后面添加test,访问到的是目录页面
如果要访问/usr/local/tomcat/webapps下的其他文件就需要修改配置文件
[root@localhost ~]# cd /usr/local/tomcat/conf
[root@localhost conf]# vim tomcat-users.xml
[root@localhost conf]# tail tomcat-users.xml
<role rolename="role1"/>
<user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
<user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
<user username="role1" password="<must-be-changed>" roles="role1"/>
-->
<role rolename="admin-gui"/>
<role rolename="manager-gui"/>
<user username="chenxi" password="wzctdxb" roles="admin-gui"/>
<user username="nene" password="chenxi123" roles="manager-gui"/>
</tomcat-users>
修改你想访问的/usr/local/tomcat/webapps/manager/META-INF/下的目录中的context.xml配置文件
将allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />中增加一个你本机的网段或者具体的ip
//比如说这样
allow="192\.168\.139\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
这表示在192.168.139这个网段上的所有主机
[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
Using CATALINA_OPTS:
[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
Using CATALINA_OPTS:
Tomcat started.[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
在/usr/local/tomcat/webapps/docs/META-INF/context.xml也需要放行自己的网段
在/usr/local/tomcat/webapps/host-manager/META-INF/context.xml也需要放行自己的网段