Tomcat使用详解
Posted ajunyu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat使用详解相关的知识,希望对你有一定的参考价值。
1.Tomcat
Tomcat是apache软件基金会(apache Software Foundation)的Jakarta 项目中的一个核心项目,由apache、Sun和其他一些公司及个人共同开发而成。
Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。
Tomcat和nginx、apache(httpd)、lighttpd等Web服务器一样,具有处理html页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTMl的能力不如Nginx/apache服务器。目前Tomcat最新版本为9.0。Java容器还有resin、weblogic等。
2.Tomcat安装
2.1 下载tomcat安装包
tomcat官网:https://tomcat.apache.org/
下载最新的apache-tomcat-9.0.19 .zip
2.2 上传至linux
[[email protected] ~]# rz
[[email protected] ~]# ls anaconda-ks.cfg apr-1.5.2.tar.gz httpd-2.4.20 apache-tomcat-9.0.19.zip apr-util-1.5.4 httpd-2.4.20.tar.gz apr-1.5.2 apr-util-1.5.4.tar.gz
2.3 解压
[[email protected] ~]#unzip apache-tomcat-9.0.19.zip [[email protected] ~]# ls apache-tomcat-9.0.19
2.4 复制到/usr/local/tomcat 下
[[email protected] ~]# cp -a apache-tomcat-9.0.19 /usr/local/tomcat
2.5 查看
[[email protected] ~]# cd /usr/local/tomcat [[email protected] tomcat]# ls bin conf lib logs README.md RUNNING.txt webapps BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RElEaSE-NOTES temp work #二进制包,不在需要编译安装
2.6 .sh脚本文件需要执行权限,授予执行权限
[[email protected] tomcat]# cd bin [[email protected] bin]# ll -rw-r--r--. 1 root root 2020 apr 12 15:24 shutdown.bat -rw-r--r--. 1 root root 1902 apr 12 15:24 shutdown.sh -rw-r--r--. 1 root root 2022 apr 12 15:24 startup.bat -rw-r--r--. 1 root root 1904 apr 12 15:24 startup.sh [[email protected] bin]# chmod -R a+x /usr/local/tomcat -rwxr-xr-x. 1 root root 1902 apr 12 15:24 shutdown.sh -rwxr-xr-x. 1 root root 2022 apr 12 15:24 startup.bat -rwxr-xr-x. 1 root root 1904 apr 12 15:24 startup.sh
2.7 下载java环境
选择yum安装tomcat需要的jdk,就不用再手动配置环境变量
[[email protected] /]# mount /dev/cdrom /mnt mount: /dev/sr0 is write-protected, mounting read-only [[email protected] /]# yum install java -y
2.8 启动tomcat
[[email protected] 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 Tomcat started. [[email protected] 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 Tomcat started. [[email protected] bin]# ss -tnl #查看端口 liSTEN 0 100 :::8080 :::* liSTEN 0 1 ::ffff:127.0.0.1:8005 :::* liSTEN 0 100 :::8009 :::* #三个端口均已开启
2.9 windows端访问,要加端口
2.10 关闭tomcat
[[email protected] bin]# ./shutdown.sh #当前目录下关闭
2.11 将命令写入环境变量的子配置文件,确保在任何目录下均可启动或关闭tomcat
[[email protected] ~]# vim /etc/profile.d/tomcat.sh export PATH=/usr/local/tomcat/bin/:$PaTH :wq [[email protected] ~]# source /etc/profile.d/tomcat.sh #重新加载文件确保其生效 [[email protected] ~]# startup.sh #再次启动 Tomcat started. [[email protected] ~]# ss -tnl State Recv-Q Send-Q local address:Port Peer address:Port liSTEN 0 100 :::8080 :::* liSTEN 0 1 ::ffff:127.0.0.1:8005 :::* liSTEN 0 100 :::8009 :::*
3.tomcat目录
[[email protected] ~]# cd /usr/local/tomcat/ [[email protected] tomcat]# tree -l 1 #以树形列出 . ├── bin #用以启动、关闭Tomcat或者其它功能的脚本(.bat文件和.sh文件) ├── BUilDiNG.txt ├── conf #用以配置Tomcat的XML及DTD文件 ├── CONTRiBUTiNG.md ├── lib #存放web应用能访问的JAR包 ├── LICENSE #许可证 ├── logs #Catalina和其它Web应用程序的日志文件 ├── NOTICE ├── README.md ├── RElEASE-NOTES ├── RUNNING.txt ├── temp #临时文件 ├── webapps #Web应用程序根目录 └── work #用以产生有JSP编译出的Servlet的.java和.class文件
4. tomcat日志
[[email protected] tomcat]# cd logs/ [[email protected] logs]# ls catalina.out #tomcat的实时日志
5.tomcat主配置文件server.xml详解
[[email protected] tomcat]# cd conf Catalina context.xml logging.properties tomcat-users.xsd catalina.policy jaspic-providers.xml server.xml web.xml catalina.properties jaspic-providers.xsd tomcat-users.xml [[email protected] conf]# vim srever.xml <Server port="8005" shutdown="SHUTDOWN"> <Connector port="8080" protocol="HTTP/1.1" <Connector port="8009" protocol="aJP/1.3" redirectPort="8443" /> ……….. <Host name="localhost" appBase="webapps" unpackWaRs="true" autoDeploy="true">
#WaR压缩包放在webapps下会自动解压 自动部署
6. tomcat的三个端口
8005端口:接收shutdown指令。
port指定Tomcat监听shutdown命令端口。终止服务器运行时,必须在Tomcat服务器所在的机器上发出shutdown命令,该属性是必须的。 shutdown指定终止Tomcat服务器运行时,发给Tomcat服务器的shutdown监听端口的字符串,该属性必须设置。
<Server port="8005" shutdown="SHUTDOWN">
8080端口:接收HTTP协议。
connector:接收用户请求,类似于httpd的listen配置监听端口。
port指定服务器端要创建的端口号,并在这个端口监听来自客户端的请求。
redirectPort指定服务器正在处理http请求时收到了一个SSl传输请求后重定向的端口号
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
8009端口:接收AJP协议
protocol连接器使用的协议,支持HTTP和AJP。AJP(Apache Jserv Protocol)专用于tomcat与apache建立通信的, 在httpd反向代理用户请求至tomcat时使用(可见Nginx反向代理时不可用aJP协议)
<Connector port="8009" protocol="aJP/1.3" redirectPort="8443" />
[[email protected] conf]# telnet 127.0.0.1 8005 #进入8005端口 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is ‘^]‘. SHUTDOWN #关闭端口 Connection closed by foreign host. [[email protected] conf]# ss -tnl State Recv-Q Send-Q local address:Port Peer address:Port liSTEN 0 128 *:111 *:* liSTEN 0 128 *:22 *:* liSTEN 0 100 127.0.0.1:25 *:* liSTEN 0 128 :::111 :::* liSTEN 0 128 :::80 :::* liSTEN 0 128 :::22 :::* liSTEN 0 100 ::1:25 :::* #tomcat关闭
/usr/local/tomcat/webapps/ROOT :网站根目录;网站访问默认为ROOT目录,访问文件也可以在webapps下创建目录将index.html拷入,再写入文件,访问时必须将非ROOT的目录跟上。如在webapps创建目录zxj,拷入index.html后写入211111111:telnet:远程登录指令
[[email protected] tomcat]# cd webapps/ [[email protected] webapps]# echo "2111111111" > index.html [[email protected] webapps]# cp index.html zxj
7.tomcat多实例部署
[[email protected] local]# cp -a tomcat tomcat1 #复制多实例,
[[email protected] local]# cp -a tomcat tomcat2 [[email protected] local]# ls tomcat tomcat1 tomcat2 #一共3个tomcat服务,为避免端口冲突, 需要修改复制实例的端口,使端口号不冲突即可 [[email protected] local]# vim tomcat1/conf/server.xml <Server port="8006" shutdown="SHUTDOWN"> <Connector port="8081" protocol="HTTP/1.1" ;wq [[email protected] local]# vim tomcat2/conf/server.xml <Server port="8007" shutdown="SHUTDOWN"> <Connector port="8082" protocol="HTTP/1.1" :wq [[email protected] local]# tomcat1/bin/startup.sh #启动tomcat1 [[email protected] local]# tomcat2/bin/startup.sh #启动tomcat2
windows端访问
多实例部署成功
8. tomcat搭建集群
以Nginx为调度器,三台tomcat为集群节点搭建集群
以192.168.16.部署Nginx
#Nginx: #检查80端口是否被占用,若有,则关掉占用端口的http [[email protected] ~]# ss -tnl [[email protected] ~]# systemctl restart nginx #启动Nginx [[email protected] ~]# ss -tnl liSTEN 0 128 :::80 :::*
#配置负载均衡 [[email protected] ~]# vim /etc/nginx/nginx.conf upstream zxj { #负载均衡 server 192.168.16.3:8080 weight=2 max_fails=2 fail_timeout=2 server 192.168.16.3:8081 weight=2 max_fails=2 fail_timeout=2 server 192.168.16.3:8082 weight=2 max_fails=2 fail_timeout=2 } server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /var/www/html; index index.php index.html; # load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://zxj; #集群名 } :wq [[email protected] ~]# nginx -t #检查 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [[email protected] ~]# systemctl restart nginx #重启
只在在浏览器查询.4即可,记得要关闭防火墙
9.tomcat上线jpress博客系统
jpress类似于wordpress的一个开源博客系统。
9.1 下载jpress
https://gitee.com/fuhai/jpress/blob/alpha/wars/jpress-web-newest.war
9.2 上传至ROOT下
[[email protected] ~]# cd /usr/local/tomcat/webapps/ROOT/ [[email protected] ROOT]# rz
[[email protected] ROOT]#unzip fuhai-jpress-alpha-zip [[email protected] ROOT]#ls fuhai-jpress-alpha-zip jpress #无法自动解压,继续手动解压 [[email protected] ROOT]# cd jpress [[email protected] jpress]# ls DOC.md jpress liCENSE REaDME.md wars [[email protected] jpress]# cd wars/ [[email protected] wars]# ls jpress-web-newest.war [[email protected] wars]# cp jpress-web-newest.war /usr/local/tomcat/webapps/ROOT [[email protected] wars]#cd /usr/local/tomcat/webapps/ROOT [[email protected] ROOT]#unzip jpress-web-newest.war
在windows端访问
配置数据库
[[email protected] ROOT]# cd [[email protected] ~]# systemctl restart mariadb [[email protected] ~]# mysql -uroot MariaDB [(none)]> create database zxj; MariaDB [(none)]> grant all on *.* to [email protected]‘localhost‘ identified by ‘123‘; #授权给所有用户 MariaDB [(none)]> exit Bye [[email protected] ~]# systemctl restart mariadb
重启tomcat
[[email protected] ~]# shutdown.sh [[email protected] ~]# ss -tnl liSTEN 0 100 :::8080 :::*
以上是关于Tomcat使用详解的主要内容,如果未能解决你的问题,请参考以下文章
14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段