Tomcat

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > Tomcat > tomcat服务安装及配置

tomcat服务安装步骤及详细配置实战教程

作者:碳烤小肥杨..

Tomcat是由Apache开发的一个开源Java WEB应用服务器,下面这篇文章主要给大家介绍了关于tomcat服务安装步骤及详细配置实战教程,文中通过代码介绍的非常详细,需要的朋友可以参考下

1.简介

Tomcat是一个开源的Java Servlet容器,可以作为Java Web应用程序的运行环境。它是Apache软件基金会的一个重要项目,具有高性能、稳定可靠以及跨平台等特点。它实现了Java Servlet和JavaServer Pages(JSP)规范,并提供了一个运行环境,使得Java代码可以在Web服务器上执行。

tomcat就是传说中的中间件之一,tomcat本身是一个容器,专门用来运行java程序,java语言开发的网页.jsp就应该运行于tomcat中。而tomcat本身的运行也依赖于jdk环境。

2.优缺点

优点:

缺点:

3.工作原理

4.工作流程

tomcat的工作原理基于Java Servlet和JSP规范。它充当一个Web服务器,接收HTTP请求并将其传递给合适的Servlet或JSP进行处理。以下是Tomcat的基本工作流程:

5.实战(tomcat项目部署)

服务器IP地址系统版本tomcat版本
tomcat192.168.195.137centos 8apache-tomcat-9.0.80.tar.gz

首先关闭防火墙和selinux

[root@tomcat ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@tomcat ~]# setenforce 0
[root@tomcat ~]# vim /etc/selinux/config 
[root@tomcat ~]# grep '^SELINUX=' /etc/selinux/config
SELINUX=disabled

5.1.java环境安装

JDK(Java Development Kit)和JRE(Java Runtime Environment)是Java开发和运行环境的两个重要组成部分,它们之间有以下区别:

JDK:

JRE:

简而言之,JDK适用于开发者,提供了编译、调试和开发所需的工具和库。而JRE适用于用户,用于运行Java应用程序,不包含开发工具。如果你只是要运行Java程序,只需要安装JRE即可。如果你要进行Java应用程序的开发,需要安装JDK。

//安装jdk环境
[root@tomcat ~]# rm -rf /etc/yum.repos.d/*
[root@tomcat ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@tomcat ~]# yum makecache
[root@tomcat ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel wget vim
省略 . . .

//查看安装的版本,能够查看到版本则说明安装成功
[root@tomcat ~]# java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

5.2.拉取tomcat软件包

进入tomcat官网

Apache Tomcat® - Welcome!

//拉取tomcat软件包
[root@tomcat ~]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.80/bin/apache-tomcat-9.0.80.tar.gz
--2023-10-10 13:41:29--  https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.80/bin/apache-tomcat-9.0.80.tar.gz
Resolving archive.apache.org (archive.apache.org)... 65.108.204.189, 2a01:4f9:1a:a084::2
Connecting to archive.apache.org (archive.apache.org)|65.108.204.189|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11692853 (11M) [application/x-gzip]
Saving to: ‘apache-tomcat-9.0.80.tar.gz'

apache-tomcat-9.0.80.tar.gz   100%[===============================================>]  11.15M  25.2KB/s    in 5m 55s  

2023-10-10 13:47:25 (32.1 KB/s) - ‘apache-tomcat-9.0.80.tar.gz' saved [11692853/11692853]

[root@tomcat ~]# ls
anaconda-ks.cfg  apache-tomcat-9.0.80.tar.gz

5.3.解压部署

//解压至指定目录
[root@tomcat ~]# tar xf apache-tomcat-9.0.80.tar.gz -C /usr/local/
[root@tomcat ~]# ls /usr/local/
apache-tomcat-9.0.80  bin  etc  games  include  lib  lib64  libexec  sbin  share  src

//设置tomcat软链接,方便后续如果更换tomcat版本后也能直接使用
[root@tomcat ~]# cd //usr/local/
[root@tomcat local]# ll
total 0
drwxr-xr-x  9 root root 220 Oct 10 13:49 apache-tomcat-9.0.80
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 Jul 20 11:24 share
drwxr-xr-x. 2 root root   6 Aug 12  2018 src
[root@tomcat local]# ln -s apache-tomcat-9.0.80 tomcat
[root@tomcat local]# ll
total 0
drwxr-xr-x  9 root root 220 Oct 10 13:49 apache-tomcat-9.0.80
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 Jul 20 11:24 share
drwxr-xr-x. 2 root root   6 Aug 12  2018 src
lrwxrwxrwx  1 root root  20 Oct 10 13:51 tomcat -> apache-tomcat-9.0.80
[root@tomcat local]# cd tomcat/
[root@tomcat tomcat]# pwd
//usr/local/tomcat
[root@tomcat tomcat]# ls
bin           conf             lib      logs    README.md      RUNNING.txt  webapps
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  RELEASE-NOTES  temp         work
[root@tomcat tomcat]# ls bin/
bootstrap.jar       ciphers.sh                    daemon.sh     setclasspath.bat  startup.sh            version.bat
catalina.bat        commons-daemon.jar            digest.bat    setclasspath.sh   tomcat-juli.jar       version.sh
catalina.sh         commons-daemon-native.tar.gz  digest.sh     shutdown.bat      tomcat-native.tar.gz
catalina-tasks.xml  configtest.bat                makebase.bat  shutdown.sh       tool-wrapper.bat
ciphers.bat         configtest.sh                 makebase.sh   startup.bat       tool-wrapper.sh

上述的bin目录中我们能够用到的catalina.sh、shutdown.sh、version.sh、startup.sh、configtest.sh
这几个执行文件,用途分别是:
catalina.sh 能够关闭和开启服务(常用)
shutdown.sh 只能关闭服务(不是关闭主机)
startup.sh 只能开启服务
version.sh 查看tomcat版本
configtest.sh 测试配置文件是否存在错误,检查配置文件

//将tomcat的lib位置存放在/etc/ld.so.conf/d/下面,命名一个自身名字的文件,方便查找
[root@tomcat tomcat]# ls
bin           conf             lib      logs    README.md      RUNNING.txt  webapps
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  RELEASE-NOTES  temp         work
[root@tomcat tomcat]# vim /etc/ld.so.conf.d/tomcat.conf
[root@tomcat tomcat]# cat /etc/ld.so.conf.d/tomcat.conf
/usr/local/tomcat/lib
[root@tomcat tomcat]# ldconfig -v
ldconfig: Can't stat /libx32: No such file or directory
ldconfig: Path `/usr/lib' given more than once
ldconfig: Path `/usr/lib64' given more than once
ldconfig: Can't stat /usr/libx32: No such file or directory
/usr/lib64//bind9-export:
	libisccfg-export.so.163 -> libisccfg-export.so.163.0.4
	libisc-export.so.1104 -> libisc-export.so.1104.0.0
	libirs-export.so.161 -> libirs-export.so.161.0.0
	libdns-export.so.1107 -> libdns-export.so.1107.1.0
/usr/local/tomcat/lib:
/lib:
/lib64:

5.4.启动tomcat服务

//使用绝对路径执行/usr/local/tomcat/bin/下面的脚本,tomcat不能写进环境变量,放置后续更改tomcat版本后环境变量仍是之前的tomcat版本
[root@tomcat tomcat]# pwd
//usr/local/tomcat
[root@tomcat tomcat]# cd bin
[root@tomcat bin]# pwd
//usr/local/tomcat/bin
[root@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 bin]# 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              1                      [::ffff:127.0.0.1]:8005                            *:*           
LISTEN         0              100                                     *:8080                            *:*           
LISTEN         0              128                                  [::]:22                           [::]:*
[root@tomcat test]# ps -ef | grep tomcat       //查看tomcat进程
root        1585       1  0 14:02 pts/0    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        1640    1455  0 14:22 pts/0    00:00:00 grep --color=auto tomcat

服务启动成功

5.5.访问tomcat的web页面

在部署tomcat过程中,网页存放目录在/usr/local/tomcat/webapps中,里面可以存放我们的网页,也可以放置app软件,而我们上述所访问到的页面则来自webapp目录中的ROOT目录中的项目

[root@tomcat tomcat]# ls
bin           conf             lib      logs    README.md      RUNNING.txt  webapps
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  RELEASE-NOTES  temp         work
[root@tomcat tomcat]# cd webapps/
[root@tomcat webapps]# ls
docs  examples  host-manager  manager  ROOT

我们新建一个项目目录进行测试

[root@tomcat webapps]# mkdir test && cd test
[root@tomcat test]# vim index.jsp
[root@tomcat test]# cat index.jsp 
<html>
<head>
        <title>test page</title>
</head>
<body>
        <%
            out.println("Hellow World");
        %>
</body>
</html>
[root@tomcat test]# 

访问test项目

访问那个项目就在端口后加哪个项目名,不加则默认是ROOT

5.6.尝试访问webapps目录中其他项目

解决方法:

进入tomcat的conf目录中修改tomcat-user.xml文件
[root@tomcat tomcat]# cd conf/
[root@tomcat conf]# ls
Catalina         catalina.properties  jaspic-providers.xml  logging.properties  tomcat-users.xml  web.xml
catalina.policy  context.xml          jaspic-providers.xsd  server.xml          tomcat-users.xsd
[root@tomcat conf]# vim tomcat-users.xml
[root@tomcat conf]# tail -3 tomcat-users.xml
<role rolename="manager-gui"/>              //设置角色
<user username="tomcat" password="tomcat123" roles="manager-gui"/>  //设置用户,密码,并指定角色
</tomcat-users>

进入tomcat的项目文件(webapps)中任选一个项目
[root@tomcat conf]# cd ../webapps/
[root@tomcat webapps]# ls
docs  examples  host-manager  manager  ROOT  test
[root@tomcat webapps]# cd manager/
[root@tomcat manager]# ls
css  images  index.jsp  META-INF  status.xsd  WEB-INF  xform.xsl
[root@tomcat manager]# cd META-INF/
[root@tomcat META-INF]# ls
context.xml
[root@tomcat META-INF]# vim context.xml   //在allow中加入本机的ip网段
[root@tomcat META-INF]# grep -A1 '<Valve className=' context.xml
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="192\.168\.195\.\d+|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

//修改配置文件后重启服务
[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]# 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 META-INF]# ps -ef | grep tomcat
ps -ef | grep tomcat
root        1839    1455  0 15:06 pts/0    00:00:00 grep --color=auto tomcat
[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.
[root@tomcat META-INF]# 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              1                      [::ffff:127.0.0.1]:8005                            *:*           
LISTEN         0              100                                     *:8080                            *:*           
LISTEN         0              128                                  [::]:22                           [::]:*           
[root@tomcat META-INF]# ps -ef | grep tomcat
ps -ef | grep tomcat
root        1852       1 11 15:06 pts/0    00:00:01 /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        1883    1455  0 15:06 pts/0    00:00:00 grep --color=auto tomcat

再次访问

其他项目目录亦是如此,找到项目文件中的context.xml文件添加本机IP网段即可

总结:

Tomcat是一个可靠、轻量级、跨平台的Java Web应用服务器,适用于许多中小型应用程序。它的工作原理基于Java Servlet和JSP规范,通过将请求分发给相应的Servlet或JSP来处理HTTP请求。虽然Tomcat在一些高级功能方面可能相对有限,但它作为一个开源项目,提供了一个可靠的基础,使得Java Web应用程序可以在其中运行。

到此这篇关于tomcat服务安装步骤及详细配置的文章就介绍到这了,更多相关tomcat服务安装及配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文