Tomcat
Tomcat
tomcat简介
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
Tomcat是一个免费的开源的Serlvet容器,用来运行java程序的容器,是开发和调试Servlet(Server Applet)、JSP(Java Server Pages) 程序的首选;Tomcat就是传说中的中间件之一,tomcat本身是一个容器,专门用来运行java程序,java语言开发的网页.jsp就应该运行于tomcat中,而tomcat本身的运行也依赖于jdk环境。
tomcat的用处
常见的中间件
一般本地开发的话,小项目,或者是个人开发建议使用tomcat;
linux系统建议使用jetty或apache hpppd;
大型的项目就用JBOSS或webloigc;
大项目或者商业项目一般采用:weblgoic/webshere,其他的还有jboss、glasshfish等;
一些示例项目或者小项目常采用jetty;
tomcat , jboss, weblogic, websphere 一般项目tomcat就可以了。
部署tomcat
apache-tomcat-10.0.23安装包
关闭防火墙和selinux
[root@lnh ~]# systemctl stop firewalld.service
[root@lnh ~]# vim /etc/selinux/config
SELINUX=disabled
[root@lnh ~]# setenforce 0
[root@lnh ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
安装java
[root@lnh ~]# dnf -y install java-11-openjdk
下载tomcat并解压tomcat到/usr/local/目录下
[root@lnh ~]# wget https://downloads.apache.org/tomcat/tomcat-10/v10.0.23/bin/apache-tomcat-10.0.23.tar.gz
[root@lnh ~]# ls
anaconda-ks.cfg apache-tomcat-10.0.23.tar.gz
[root@lnh ~]# tar xf apache-tomcat-10.0.23.tar.gz -C /usr/local/
[root@lnh ~]# cd /usr/local/
[root@lnh local]# ln -s apache-tomcat-10.0.23/ tomcat
自定义一个Hello World的 java的测试网页
[root@lnh local]# mkdir /usr/local/tomcat/webapps/test
[root@lnh local]# cd /usr/local/tomcat/webapps/test
[root@lnh test]# vim index.jsp
[root@lnh test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("Hello World");
%>
</body>
</html>
启动tomcat
[root@lnh test]# cd /usr/local/tomcat
[root@lnh 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
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
//默认端口为8080
通过ps -ef | grep tomcat 可以查看到有java和tomcat的进程,说明服务已经启动成功
[root@lnh tomcat]# ps -ef | grep tomcat
root 36430 1 13 00:17 pts/2 00:00:06 /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 37846 4714 0 00:18 pts/2 00:00:00 grep --color=auto tomcat
[root@lnh tomcat]# 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 [::]:*
测试访问
访问tomcat首页:
http://192.168.222.250:8080
访问hello world测试页:
http://192.168.222.250:8080/test/
访问Host Manager界面
配置tomcat-users.xml文件,在行上面添加两行,设置角色,登录用户名和密码,角色名称只能是admin-gui 用户名和密码自定义
[root@lnh ~]# vim /usr/local/tomcat/conf/tomcat-users.xml
<role rolename="admin-gui"/> //添加
<user username="tomcat" password="123456" roles="admin-gui"/> //添加
</tomcat-users>
配置host-manager/META-INF/context.xml文件,允许192.168.222.0/24网段访问,在allow字段后添加
[root@lnh ~]# vim /usr/local/tomcat/webapps/host-manager/META-INF/context.xml
<Context antiResourceLocking="false" privileged="true" >
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.222\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
重启服务,先关服务,再启服务
[root@lnh ~]# /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:
NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@lnh ~]# /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.
登录网页测试登录Host Manager 输入对应的用户名和密码
用户名为tomcat,密码为123456
http://192.168.222.250:8080
访问Server Status
编辑tomcat-users.xml
[root@lnh ~]# vim /usr/local/tomcat/conf/tomcat-users.xml
<role rolename="admin-gui"/>
<role rolename="manager-gui"/> //添加
<user username="tomcat" password="123456" roles="admin-gui,manager-gui"/> //修改
</tomcat-users>
编辑manager/META-INF/context.xml,在allow字段添加允许192.168.222.0/24网段访问
[root@lnh ~]# vim /usr/local/tomcat/webapps/manager/META-INF/context.xml
<Context antiResourceLocking="false" privileged="true" >
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.222\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
重启服务,先关服务,再启服务
[root@lnh ~]# /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:
NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[root@lnh ~]# /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.
登录网页,访问Server Status,用户名tomcat 密码123456
访问Manager App的时候就不需要密码了,因为访问了Server Status界面,所以不用输入密码