Centos7 部署Tomcat

Posted 还行少年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7 部署Tomcat相关的知识,希望对你有一定的参考价值。

部署Tomcat

1.下载并安装JDK(编译器)

1.1 安装JDK

[root@localhost opt]# ls
apache-tomcat-9.0.16.tar.gz  jdk-8u201-linux-x64.rpm    nginx-1.15.9     
[root@localhost opt]# rpm -ivh jdk-8u201-linux-x64.rpm 

在这里插入图片描述

1.2 设置JAVA的环境变量

查看java的版本

[root@localhost ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

设置JAVA的环境变量

[root@localhost ~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export ClASSPATh=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH

[root@localhost ~]# source /etc/profile.d/java.sh   //加载生效
[root@localhost ~]# java -version   //查看版本
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
[root@localhost ~]# 

2.安装启动Tomcat

2.1 解压apache-tomcat源码包

[root@localhost opt]# ls
apache-tomcat-9.0.16.tar.gz  jdk-8u201-linux-x64.rpm    nginx-1.15.9     
[root@localhost opt]# tar xf apache-tomcat-9.0.16.tar.gz 

[root@localhost opt]# ls
apache-tomcat-9.0.16      rh   apache-tomcat-9.0.16.tar.gz  jdk-8u201-linux-x64.rpm  nginx-1.15.9      

[root@localhost opt]# 

2.2 将解压后的目录移至/usr/local/tomcat

[root@localhost opt]# mv apache-tomcat-9.0.16 /usr/local/tomcat

[root@localhost opt]# ls /usr/local/tomcat/
bin           conf             lib      logs    README.md      RUNNING.txt  webapps
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  RELEASE-NOTES  temp         work
[root@localhost opt]# 

2.3 优化管理tomcat

[root@localhost ~]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[root@localhost ~]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/

[root@localhost ~]# ll /usr/local/bin/
总用量 0
lrwxrwxrwx 1 root root 33 6月  30 21:47 shutdown.sh -> /usr/local/tomcat/bin/shutdown.sh
lrwxrwxrwx 1 root root 32 6月  30 21:47 startup.sh -> /usr/local/tomcat/bin/startup.sh
[root@localhost ~]# 

2.4 启动tomcat

[root@localhost ~]# 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/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@localhost ~]# netstat -antp | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      23280/java          
[root@localhost ~]# 

2.5 查看网页

在这里插入图片描述

2.6 优化Tomcat启动速度

[root@localhost ~]# vim /usr/java/jdk1.8.0_201-amd64/jre/lib/security/java.security 

在这里插入图片描述

3.Tomcat虚拟主机配置

3.1 创建hello和world项目的目录文件

[root@localhost ~]# mkdir /usr/local/tomcat/webapps/hello
[root@localhost ~]# mkdir /usr/local/tomcat/webapps/world
[root@localhost ~]# echo "this is hello page!" > /usr/local/tomcat/webapps/hello/index.jsp
[root@localhost ~]# echo "this is world page!" > /usr/local/tomcat/webapps/world/index.jsp

3.2 修改配置文件

[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml
<Host name="www.hello.com"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> 
   <Context docBase="/usr/local/tomcat/webapps/hello" path="" reloadable="true" />
  </Host>
  <Host name="www.world.com"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
   <Context docBase="/usr/local/tomcat/webapps/world" path="" reloadable="true" />
  </Host> 
Host name : 主机名
appBase : Tomcat程序工作目录
unpackWARs :tomcat在webapps文件夹中发现war文件时,是否自动将其解压
autoDeploy :设为true,则web.xml发送变化时,tomcat自动重新部署
xmlValidation ;是否开启对xml文件的验证
xmlNamespaceAware :是否启用xml命名空间
docBase : WEB应用的目录(本地路径)
path :设置访问URI为WEB应用的根目录(URL路径),“”表示默认
reloadable :是否在程序有改动时重新载入

3.3 重启服务,测试

[root@localhost ~]# 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/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

[root@localhost ~]# 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/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

在这里插入图片描述

小结

Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选

以上是关于Centos7 部署Tomcat的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot框架开发的Java项目在CentOS7上的部署

centos7 部署tomcat10.0.10

在CentOS7中部署tomcat9

Centos7 部署Tomcat

Centos7部署Tomcat项目

centos7部署Tomcat8