首页 Tomcat教程tomcat安装

Tomcat经典入门教程

实战案例: 一键安装tomcat脚本

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

基于包安装

CentOS 8 包仓库中目前还没有提供tomcat相关包

CentOS 7 yum仓库源中自带的Tomcat 7.0版本安装,此方式安装tomcat版本较低,不推荐

范例:在CentOS 7 上安装tomcat

[root@centos7 ~]#yum list tomcat*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: 
Available Packages
tomcat.noarch                                               7.0.76-9.el7_6                                base
tomcat-admin-webapps.noarch                                 7.0.76-9.el7_6                                base
tomcat-docs-webapp.noarch                                   7.0.76-9.el7_6                                base
tomcat-el-2.2-api.noarch                                    7.0.76-9.el7_6                                base
tomcat-javadoc.noarch                                       7.0.76-9.el7_6                                base
tomcat-jsp-2.2-api.noarch                                   7.0.76-9.el7_6                                base
tomcat-jsvc.noarch                                          7.0.76-9.el7_6                                base
tomcat-lib.noarch                                           7.0.76-9.el7_6                                base
tomcat-native.x86_64                                        1.2.21-1.el7                                  epel
tomcat-servlet-3.0-api.noarch                               7.0.76-9.el7_6                                base
tomcat-webapps.noarch                                       7.0.76-9.el7_6                                base
tomcatjss.noarch                                            7.2.1-8.el7_6                                 base
[root@centos7 ~]#yum -y install tomcat tomcat-webapps tomcat-admin-webapps
[root@centos7 ~]#systemctl enable --now tomcat
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /usr/lib/systemd/system/tomcat.service.

[root@centos7 ~]#ss -ntl
State      Recv-Q Send-Q          Local Address:Port                         Peer Address:Port              
LISTEN     0      100                 127.0.0.1:25                                      *:*                  
LISTEN     0      128                         *:22                                      *:*                  
LISTEN     0      100                     [::1]:25                                   [::]:*                  
LISTEN     0      1          [::ffff:127.0.0.1]:8005                                 [::]:*                  
LISTEN     0      100                      [::]:8009                                 [::]:*                  
LISTEN     0      100                      [::]:8080                                 [::]:*                  
LISTEN     0      128                      [::]:22                                   [::]:* 

[root@centos7 ~]#getent passwd tomcat
tomcat:x:53:53:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
[root@centos7 ~]#ps aux|grep tomcat
tomcat     1328  0.4 11.2 2298188 112004 ?      Ssl  21:32   0:11 /usr/lib/jvm/jre/bin/java -classpath /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat -Dcatalina.home=/usr/share/tomcat -Djava.endorsed.dirs= -Djava.io.tmpdir=/var/cachetomcat/temp -Djava.util.logging.config.file=/usr/share/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager org.apache.catalina.startup.Bootstrap start
root       1521  0.0  0.0 112712   960 pts/0    R+   22:15   0:00 grep --color=auto tomcat

打开浏览器访问:http://tomcat:8080/

tomcat安装插图

二进制安装

可以从Apache官网下载二进制tomcat包8.x.x,推荐此方式

下载并安装
#官网或镜像网站下载:https://tomcat.apache.org/download-80.cgi
[root@centos8 ~]#wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.50/bin/apache-tomcat-8.5.50.tar.gz

[root@centos8 ~]#tar xf apache-tomcat-8.5.50.tar.gz  -C /usr/local/
[root@centos8 ~]#cd /usr/local/
[root@centos8 local]#ln -s apache-tomcat-8.5.50/ tomcat
[root@centos8 ~]#echo 'PATH=/usr/local/tomcat/bin:PATH'>/etc/profile.d/tomcat.sh
[root@centos8 ~]#. /etc/profile.d/tomcat.sh
[root@centos8 ~]#echoPATH
/usr/local/tomcat/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/bin:/root/bin
[root@centos8 ~]#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/local/jdk/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@centos8 ~]#ss -ntl
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                          *:*          
LISTEN      0             100                                    *:8009                          *:*
#查看进程是以root启动的
[root@centos8 ~]#ps aux|grep tomcat
root      12994 34.1  9.4 2155140 76912 pts/0   Sl   22:38   0:02 /usr/local/jdk/jre/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      13039  0.0  0.1  12108  1076 pts/0    R+   22:38   0:00 grep --color=auto tomcat

[root@centos8 ~]#shutdown.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@centos8 ~]#ss -ntl
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@centos8 ~]#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/local/jdk/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@centos8 ~]#ss -ntl
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                          *:*          
LISTEN      0             100                                    *:8009                          *:*          
[root@centos8 ~]#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/local/jdk/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

打开浏览器访问:http://tomcat:8080/,正常可以看到以下界面

tomcat安装插图(1)

扩展知识:tomcat和catalina关系

Tomcat的servlet容器在4.X版本中被Craig McClanahan重新设计为Catalina.即Catalina就是servlet容器。
Tomcat的核心分为3个部分: 
(1)Web容器—处理静态页面; 
(2)catalina — 一个servlet容器—–处理servlet; 
(3)还有就是JSP容器,它就是把jsp页面翻译成一般的servlet

Catalina是美国西海岸靠近洛杉矶22英里的一个小岛,因为其风景秀丽而著名,曾被评为全美最漂亮的小岛。Servlet运行模块的最早开发者Craig McClanahan因为喜欢Catalina岛故以Catalina命名他所开这个模块,另外在开发的早期阶段,Tomcat是被搭建在一个叫Avalon的服务器框架上,而Avalon则是Catalina岛上的一个小镇的名字,于是想一个与小镇名字相关联的单词也是自然而然。设计者估计是想把tomcat设计成最美的轻量级容器吧。下图为该小岛。

tomcat安装插图(2)

3.2.2.2 配置tomcat自启动的service文件
#创建tomcat专用帐户
[root@centos8 ~]#usermod -r -s /sbin/nologin tomcat

#准备service文件中相关环境文件
[root@centos8 ~]#vim /usr/local/tomcat/conf/tomcat.conf
[root@centos8 ~]#cat /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/usr/local/jdk
JRE_HOME=$JAVA_HOME/jre
[root@centos8 ~]#chown -R tomcat.tomcat /usr/local/tomcat/

#创建tomcat.service文件
[root@centos8 ~]#vim /lib/systemd/system/tomcat.service
[root@centos8 ~]#cat /lib/systemd/system/tomcat.service 
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target 

[Service]
Type=forking
EnvironmentFile=/usr/local/tomcat/conf/tomcat.conf
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat

[Install]
WantedBy=multi-user.target

[root@centos8 ~]#systemctl daemon-reload
[root@centos8 ~]#systemctl enable --now tomcat
Created symlink /etc/systemd/system/multi-user.target.wants/tomcat.service → /usr/lib/systemd/system/tomcat.service.
[root@centos8 ~]#systemctl status tomcat
● tomcat.service - Tomcat
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-02-08 23:37:02 CST; 5s ago
  Process: 14312 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 14320 (java)
    Tasks: 43 (limit: 4895)
   Memory: 64.2M
   CGroup: /system.slice/tomcat.service
           └─14320 /usr/local/jdk/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging>

Feb 08 23:37:02 centos8.localdomain systemd[1]: Starting Tomcat...
Feb 08 23:37:02 centos8.localdomain systemd[1]: Started Tomcat.

本文链接:http://www.yunweipai.com/35123.html

Tomcat经典入门教程

实战案例: 一键安装tomcat脚本

网友评论comments

发表评论

邮箱地址不会被公开。

暂无评论

Copyright © 2012-2021 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部