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
2. tomcat项目部署
2.1Java环境安装
//安装jdk环境
[root@tomcat ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
安装过程略...
//查看安装的版本
[root@tomcat ~]# java -version
openjdk version "1.8.0_272"
OpenJDK Runtime Environment (build 1.8.0_272-b10)
OpenJDK 64-Bit Server VM (build 25.272-b10, mixed mode)
2.2 tomcat部署
- 上官网下载tomcat
//下载tocmcat
[root@tomcat src]# wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.0-M10/bin/apache-tomcat-10.0.0-M10.tar.gz
--2020-11-26 10:40:19-- https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.0-M10/bin/apache-tomcat-10.0.0-M10.tar.gz
Resolving archive.apache.org (archive.apache.org)... 138.201.131.134, 2a01:4f8:172:2ec5::2
Connecting to archive.apache.org (archive.apache.org)|138.201.131.134|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11310078 (11M) [application/x-gzip]
Saving to: ‘apache-tomcat-10.0.0-M10.tar.gz’
apache-tomcat-10 100%[==========>] 10.79M 505KB/s in 70s
2020-11-26 10:41:31 (158 KB/s) - ‘apache-tomcat-10.0.0-M10.tar.gz’ saved [11310078/11310078]
//解压部署
[root@tomcat src]# ls
apache-tomcat-10.0.0-M10.tar.gz debug kernels
[root@tomcat src]# tar xf apache-tomcat-10.0.0-M10.tar.gz -C /usr/local/
[root@tomcat src]# cd /usr/local/
[root@tomcat local]# ls
apache-tomcat-10.0.0-M10 etc include lib64 sbin src
bin games lib libexec share
[root@tomcat local]# ln -s apache-tomcat-10.0.0-M10/ tomcat
[root@tomcat local]# ll
total 0
drwxr-xr-x 9 root root 220 Nov 26 10:57 apache-tomcat-10.0.0-M10
drwxr-xr-x. 2 root root 6 Aug 12 2018 bin
drwxr-xr-x. 2 root root 6 Aug 12 2018 etc
drwxr-xr-x. 2 root root 6 Aug 12 2018 games
drwxr-xr-x. 2 root root 6 Aug 12 2018 include
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib
drwxr-xr-x. 2 root root 6 Aug 12 2018 lib64
drwxr-xr-x. 2 root root 6 Aug 12 2018 libexec
drwxr-xr-x. 2 root root 6 Aug 12 2018 sbin
drwxr-xr-x. 5 root root 49 Aug 26 19:13 share
drwxr-xr-x. 2 root root 6 Aug 12 2018 src
lrwxrwxrwx 1 root root 25 Nov 26 10:57 tomcat -> apache-tomcat-10.0.0-M10/
//写一个hello world的Java页面
[root@tomcat ~]# cd /usr/local/tomcat/webapps/
[root@tomcat webapps]# ls
docs examples host-manager manager ROOT
[root@tomcat webapps]# mkdir test
[root@tomcat webapps]# cd test/
[root@tomcat test]# vim index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("hello world");
>%
</body>
</html>
[root@tomcat test]# ll index.jsp
-rw-r--r-- 1 root root 130 Nov 26 11:34 index.jsp
//启动tomcat
[root@tomcat ~]# /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@tomcat ~]# ps -ef|grep tomcat
root 10628 1 53 11:35 pts/1 00:00:03 /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 10658 1370 0 11:35 pts/1 00:00:00 grep --color=autotomcat
[root@tomcat ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN0 128 0.0.0.0:22 0.0.0.0:*
LISTEN0 100 *:8080 *:*
LISTEN0 128 [::]:22 [::]:*
LISTEN0 1 [::ffff:127.0.0.1]:8005 *:*
[root@tomcat ~]#
- 网页验证
3. 如何部署java项目
//下载复制的安装包
[root@tomcat ~]# wget http://dl.zrlog.com/release/zrlog.war
--2020-11-26 14:34:40-- http://dl.zrlog.com/release/zrlog.war
Resolving dl.zrlog.com (dl.zrlog.com)... 45.253.17.217, 45.253.17.211, 45.253.17.212, ...
Connecting to dl.zrlog.com (dl.zrlog.com)|45.253.17.217|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11596617 (11M) [application/zip]
Saving to: ‘zrlog.war’
zrlog.war 100%[==============>] 11.06M 191KB/s in 36s
2020-11-26 14:35:18 (313 KB/s) - ‘zrlog.war’ saved [11596617/11596617]
//把zrlog.war包放到指定目录
[root@tomcat ~]# ls
anaconda-ks.cfg zrlog.war
[root@tomcat ~]# cd /usr/local/tomcat/webapps/
[root@tomcat webapps]# cp ~/zrlog.war .
[root@tomcat webapps]# ls
docs examples host-manager manager ROOT test zrlog.war
//停止服务,确定进程和端口号停止
[root@tomcat webapps]# /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@tomcat webapps]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@tomcat webapps]# ps -ef|grep tomcat
root 11018 10836 0 14:40 pts/0 00:00:00 grep --color=auto tomca
//启动服务
[root@tomcat webapps]# /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@tomcat webapps]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
[root@tomcat webapps]# ps -ef|grep tomcat
root 11031 1 80 14:41 pts/0 00:00:04 /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 11062 10836 0 14:41 pts/0 00:00:00 grep --color=auto tomca
//查看zrlog.war包自动解压
[root@tomcat webapps]# ls
docs examples host-manager manager ROOT test zrlog zrlog.war
//安装数据库,设置开机自启
[root@tomcat ~]# yum -y install mariadb*
[root@tomcat ~]# systemctl enable --now mariadb
- 网页验证无法查看
4.页面权限管理
tomcat]# ls conf/
Catalina jaspic-providers.xml tomcat-users.xml //配置验证登录
catalina.policy jaspic-providers.xsd tomcat-users.xsd
catalina.properties logging.properties web.xml
context.xml server.xml //修改端口号
-
登录server status
-
修改配置文件
[root@tomcat tomcat]# cd conf/
[root@tomcat conf]# ls
Catalina jaspic-providers.xml tomcat-users.xml
catalina.policy jaspic-providers.xsd tomcat-users.xsd
catalina.properties logging.properties web.xml
context.xml server.xml
[root@tomcat conf]# vim tomcat-users.xml //此文件最后加上以下两行
....
<role rolename="manager-gui"/>
<user username="tomcat" password="123456" roles="manager-gui"/> //修改下密码
</tomcat-users>
~
[root@tomcat META-INF]# pwd
/usr/local/tomcat/webapps/manager/META-INF
[root@tomcat META-INF]# ls
context.xml
[root@tomcat META-INF]# vim context.xml
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.152\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> //找到此行,添加本机IP,d代表所有+表示多位
//重启服务
[root@tomcat META-INF]# /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@tomcat META-INF]# /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
- 登录host-manger
//修改配置文件
[root@tomcat conf]# pwd
/usr/local/tomcat/conf
[root@tomcat conf]# vim tomcat-users.xml
<role rolename="manager-gui"/>
<user username="tomcat" password="123456" roles="manager-gui"/>
<role rolename="admin-gui"/> //粘贴此两行
<user username="tomcat" password="123456" roles="admin-gui"/> //粘贴此两行,修改密码
</tomcat-users>
[root@tomcat META-INF]# pwd
/usr/local/tomcat/webapps/host-manager/META-INF
[root@tomcat META-INF]# ls
context.xml
[root@tomcat META-INF]# vim context.xml
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.152\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> //找到此行,添加本机IP
//重启服务
[root@tomcat META-INF]# /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@tomcat META-INF]# /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.
5. tomcat 部署 lamt
环境说明
系统版本 | IP | 需要安装的服务 |
---|---|---|
redhat8 | 192.168.152.135 | httpd |
redhat8 | 192.168.152.134 | mysql |
redhat8 | 192.168.152.133 | tomcat |
配置Apache
- 启用代理模块
//修改配置文件,取消下面几行内容的注释
[root@Apache ~]# vim /etc/httpd24/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Include /etc/httpd24/extra/httpd-vhosts.conf
- 配置虚拟主机
[root@Apache ~]# vim /etc/httpd24/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/usr/local/httpd/htdocs"
ProxyPass / http://192.168.152.133:8080/ //修改为tomcat主机IP
ProxyPassReverse / http://192.168.152.133:8080/ //修改为tomcat主机IP
<Directory "/usr/local/httpd/htdocs">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
//重启服务
[root@Apache ~]# apachectl restart
- 浏览器访问验证