(Linux)Centos7.*版本安装配置Java环境、Tomcat、Nginx并打包部署SSM框架web系统

一、准备软件与安装包

(一)、必须的软件

1、点击下载Xshell

2、点击下载FileZilla

(二)、准备安装包

1、点击下载JDK1.8Linux版本

2、点击下载Nginx

3、点击下载Tomcat  

二、关于FileZilla软件的使用说明

(一)、FileZilla软件的打开和说明

(二)、配置服务器地址去连接服务器 

三、Xshell的配置连接和基本命令

(一)、使用Xshell软件

(二)、Xshell的相关配置和连接

四、Xshell相关快捷键和CentOs7.*版本相关命令说明

(一)、Xshell中快捷键说明

(二)、Xshell设置

(三)、(Linux)CentOs7.*简单命令

1、我在哪儿?(我在系统中的当前位置)

2、我要去哪儿?(我要去到目标位置)

3、我当前所在的路径下有什么内容?

4、创建一个文件或文件夹

5、删除一个文件或文件夹

6、修改文件或文件夹名称:

7、解压文件:

8、查看CentOs的系统版本号:

9、查看某个进程或端口号是否正在运行以及停止该进程或服务

五、在CentOs7.*系统中安装并配置Java环境

(一)、上传JDK安装包到服务器对应目录下

(二)、解压JDK压缩包

(三)、配置JDK环境变量

1、使用vi或vim目录编辑/etc目录下的profile系统文件.

2、在/etc/profile文件的末尾添加如下内容:

3、使刚刚修改的环境配置生效

4、检测JDK是否配置成功

六、在CentOs7.*系统中安装并配置Tomcat环境

(一)、解压tomcat

(二)、修改tomcat配置文件server.xml

(三)、进入tomcat安装目录下的bin目录并启动tomcat服务

(四)、启动和停止tomcat服务

七、使用IDEA打包web项目为war包并上传至服务器进行部署和访问

(一)、web项目需要在pom.xml配置打包方式、打包名字及打包规则

(二)、pom.xml

(三)、使用IDEA进行clear、install、compile、package的过程

(四)、使用FileZilla将demo.war上传至服务器的/usr/local/tomcat/apache-tomcat-8.5.87/webapps目录下

(五)、修改/usr/local/tomcat/apache-tomcat-8.5.87/conf/目录下的server.xml配置文件.

(六)、启动tomcat服务

(七)、使用IP:PORT进行访问

(八)、停止tomcat服务出错

八、配置tomcat为服务器开机启动服务

(一)、关闭tomcat服务

(二)、在/usr/local/tomcat/apache-tomcat-8.5.87/bin/目录下创建setenv.sh文件并添加内容

(三)、配置/usr/local/tomcat/apache-tomcat-8.5.87/bin/目录下的catalina.sh文件

​编辑

(四)、配置/usr/local/tomcat/apache-tomcat-8.5.87/bin/目录下的setclasspath.sh文件

(五)、配置系统文件

​编辑

(六)、重启服务并查看相关服务状态

(七)、如果是项目的.war或者.jar有问题,那也是不能访问项目的,比如数据库信息配置不对,如何检查呢?



一、准备软件与安装包

懒人版,可以直接点击从CSDN下载我上传的资源包(免费):

软件安装包和Linux所需安装包 for CSDN

也可以点下面的连接用百度网盘下载:

软件安装包和Linux所需安装包 for 百度网盘

提取码:1234

资源包结构:

 如果对版本有不同要求的去下面的对应连接里面找相关的版本:

(一)、必须的软件

1、点击下载Xshell

作用:Xshell主要是远程连接服务器,写Linux命令操作服务器等。

2、点击下载FileZilla

作用:使用FileZilla上传文件或文件夹到服务器上。

(二)、准备安装包

1、点击下载JDK1.8Linux版本

2、点击下载Nginx

3、点击下载Tomcat  

综上,所需软件及安装包准备完毕!

二、关于FileZilla软件的使用说明

(一)、FileZilla软件的打开和说明

1、双击打开:

2、打开后是这样:

(二)、配置服务器地址去连接服务器 

1、点击左上角的文件,点击站点管理器

2、站点管理器打开后如下,点新站点

3、配置服务器IP地址(公有地址)、访问的协议为SFTP、端口号:22、用户名:root 

配置好以后是这样:

注意:如果点击连接显示连接失败或超时,请检查:

1、服务器的端口22是否为打开状态

2、协议是否选择的是SFTP

3、用户名密码是否正确,如果是阿里云等厂商服务器,可以去对应网站的实例找到服务器并修改实例密码

 4、连接成功后可以点击服务器站点查看相应挂载点中的文件夹及文件

 

 

说明:到这一步以后,您的/usr/local下面是没有对应的java、nginx、tomcat文件夹的,需要使用Xshell去创建对应文件夹,这一部分的操作请看第三部分。 

5、连接成功后可以点击服务器站点查看相应挂载点中的文件夹及文件

将jdk、tomcat、nginx和mysql对应的安装包传到对应目录下,以传输jdk为例:

综上、FileZilla的配置和连接完成!

三、Xshell的配置连接和基本命令

(一)、使用Xshell软件

1、打开Xshell 

                

2、打开后是这样

(二)、Xshell的相关配置和连接

1、新建会话

 2、点击连接

 3、点击用户身份验证

4、双击会话管理器进行连接服务器

5、连接成功

6、连接失败处理 

说明:如果连接失败或者有错误提示信息,请一次点开会话管理器---》aliyun----》属性

 点开属性后是这样,着重检查IP地址,用户名和密码是否是服务器的账号密码等配置信息:

 综上、Xshell相关配置和连接完成!

四、Xshell相关快捷键和CentOs7.*版本相关命令说明

(一)、Xshell中快捷键说明

通常我们用到复制外面的内容到Xshell中比较多,使用shift + insert复制内容进去;

要把Xshell中的内容复制出来需要先选中内容,按ctrl + insert进行复制。

ctrl + insert # 复制Xshell中的选中内容
shift + insert # 将外面的内容复制到Xshell中

(二)、Xshell设置

许多朋友喜欢简洁的Xshell,可以进行设置:

①设置主题及颜色

②设置透明度

 ③设置简单界面

(三)、(Linux)CentOs7.*简单命令

经验之谈:学习Linux操作系统或是它的命令都是很枯燥的事情,最好的学习方法是对比windows系统来进行针对性的练习。

比如在Windows中我们时刻关注我自己当前在哪个文件夹下?我要去到哪个文件夹?我要执行什么操作?

1、我在哪儿?(我在系统中的当前位置)

windows:

        我在C盘--->Program Files--->Bonjour这个路径下。

Linux:    

pwd #输完pwd后回车就会显示出当前所在目录位置 /root即您当前所在的位置

 Linux中没有盘符这个说法,没有Windows中所谓的C盘、D盘这样的概念,Linux是以根节点为树顶的一棵倒过来的树形目录,如图:

因此我们必须时刻清楚我们在这颗倒着的树的哪个位置,即需要查看当前所在的位置就使用:pwd命令

2、我要去哪儿?(我要去到目标位置)

windows:

        我从C盘到D盘某个文件夹,只要去点击对应的D盘对应的文件就可以进入了。

Linux:

        cd 目标路径 这个命令指的是你要去到哪个目标位置。

cd / # 去到根节点
cd /usr # 去到根节点下的usr目录
cd /root # 去到根节点下的root目录
cd /usr/local # 去到根节点下的usr目录下的local目录
cd .. #返回上一级,比如:先执行cd /usr/local/tomcat那我现在的位置在/usr/local/tomcat下,执行cd ..就会返回到/usr/local目录下

运用举例:

3、我当前所在的路径下有什么内容?

windows(有什么内容是一眼可以看到的):

        

Linux:

ls # 查看当前文件夹下面有哪些内容,仅显示文件或文件夹名字

ls -l # 查看当前文件夹下面有哪些内容,显示文件或文件夹名字以及对应权限等

# 根据个人喜好决定使用ls还是ls -l

运用举例:

4、创建一个文件或文件夹

cd /usr/local # 进入usr目录下的local目录
mkdir java    # 创建java目录
mkdir tomcat  # 创建tomcat目录
mkdir nginx   # 创建nginx目录
mkdir mysql   # 创建mysql目录

5、删除一个文件或文件夹

删除文件或文件夹时需要注意细节,rm命令只能删除文件,不能删除空文件夹或者文件夹中有内容的目录。

rm t.java # 删除t.java文件
rm test1 # 尝试删除test1文件夹,无论test1文件夹中是否有内容,都无法删除
rm -rf test1 # 这样可以删除test1这个文件夹,连带删除它内部的所有内容

6、修改文件或文件夹名称:

7、解压文件:

tar -zxvf jdk-8u361-linux-x64.tar.gz  # 解压JDK压缩包

8、查看CentOs的系统版本号:

cat /etc/centos-release

9、查看某个进程或端口号是否正在运行以及停止该进程或服务

你可以使用以下命令来查询tomcat是否真正运行:
1. 检查Tomcat进程是否存在

ps -ef | grep tomcat

2. 查看Tomcat是否监听了端口8080

netstat -an | grep 8080

如果Tomcat进程存在并且监听了端口8080,则说明Tomcat正在运行

综上、关于Xshell和CentOs7.*版本的简单命令讲完!

五、在CentOs7.*系统中安装并配置Java环境

(一)、上传JDK安装包到服务器对应目录下

使用FileZilla连接服务器,找到服务器的/usr/local/java目录,这里的java是我们自己使用mkdir java创建的,如果没有可以创建一个。

(二)、解压JDK压缩包

使用Xshell连接到服务器,使用cd 命令进入/usr/local/java目录,使用tar -zxvf jdk-8u361-linux-x64.tar.gz 解压JDK压缩包。

(三)、配置JDK环境变量

1、使用vi或vim目录编辑/etc目录下的profile系统文件.

vim /etc/profile # 编辑系统文件

 接着按下回车:

 可以使用:set nu显示文件行号:

 将输入法调整为英文状态,按i对系统文件进行编辑:

2、在/etc/profile文件的末尾添加如下内容:

cd /etc/profile # 编辑系统文件

# 在打开的文件最末端添加如下内容
# 配置jdk全局变量-灰灰老师
JAVA_HOME=/usr/local/java/jdk1.8.0_361
CLASSPATH=$:CLASSPATH:$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH

其中JAVA_HOME=后面的内容是你服务器的JDK解压后的位置,可以通过如下命令找到JDK所在位置:

① 已知JDK所在位置时,如下:

 ② 忘记JDK安装路径时,如下:

 配置好的系统文件如下:

编辑完成后按esc键退出编辑模式,输入:wq进行保存,如果保存失败则使用:wq!强制保存,如下:

出现红色:

那就使用 :wq!强制保存.

 

 确认一下刚刚修改的内容是否保存成功:

使用vim /etc/profile进入刚刚编辑的文件核对最后是否有关于JDK的配置内容了,有即已保存成功,如果没有重新执行上面的编辑操作即可。

3、使刚刚修改的环境配置生效

source /etc/profile # 使刚刚配置的系统文件生效

4、检测JDK是否配置成功

java -version # 查看JDK版本

java # java编译命令
javac # javac编译命令

 综上、CentOs7.*系统中就已经安装和配置好JDK的环境了。

六、在CentOs7.*系统中安装并配置Tomcat环境

(一)、解压tomcat

cd /usr/local/tomcat/  # 去到放apache-tomcat-8.5.87.tar.gz安装包的目录下
ls -l                  # 显示目录下的内容
tar -zxvf apache-tomcat-8.5.87.tar.gz # 执行解压

(二)、修改tomcat配置文件server.xml

cd apache-tomcat-8.5.87/        # 去到tomcat文件目录下
ls -l                           # 查看目录文件
cd conf/                        # 去到conf文件目录下
ls -l                           # 查看目录文件

 编辑配置文件:

 显示行号:

:set nu

按i键进入编辑模式,大约在69行的位置将端口号进行修改,并执行 :wq!强制保存:

(三)、进入tomcat安装目录下的bin目录并启动tomcat服务

cd /usr/local/tomcat/apache-tomcat-8.5.87/bin/ # 进入tomcat解压文件
ls -l # 查看目录

(四)、启动和停止tomcat服务

cd /usr/local/tomcat/apache-tomcat-8.5.87/bin/ # 去到tomcat的bin目录下
./startup.sh  # 启动tomcat服务
./shutdown.sh # 停止tomcat服务

 注意:启动命令前面是点杠, 这个 ./ 不能少,./的意思类似于双击鼠标运行windows里面的.exe文件

在浏览器中输入IP:port,即IP地址冒号端口号进行访问:

此时看到一只汤姆猫就证明你的tomcat服务是能够正常使用的。

 

tomcat相关命令:

./startup.sh  # 启动tomcat服务
./shutdown.sh # 停止tomcat服务

七、使用IDEA打包web项目为war包并上传至服务器进行部署和访问

综述:这部分主要解决的问题是,使用IP:PORT默认访问到的tomcat的首页,也就是有汤姆猫的页面,怎么让它访问到我们的web项目呢?

(一)、web项目需要在pom.xml配置打包方式、打包名字及打包规则

(二)、pom.xml

 为了便于核对,附上版主所用的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sizpx</groupId>
    <artifactId>demo</artifactId>
    <!--    <version></version>-->
    <version>0.0.1-SNAPSHOT</version>
    <!--    指定打包方式为war,方便服务器部署-->
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <dependencies>
        <!-- 热部署依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--通用mapper场景启动器-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>
        <!--阿里druid数据源启动器-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.1</version>
        </dependency>
        <!--pagehelper-spring-boot-starter依赖-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--    配置单元测试依赖jar-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--        配置json解析类-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.41</version>
        </dependency>
        <!--        导入Jquery依赖包-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.6.4</version>
        </dependency>
        <!--部署成war包时开启【同时支持war和jar启动部署】↓↓↓↓-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>compile</scope>
            <!--            本地编译运行时使用compile,服务器编译运行时使用provided-->
            <!--            <scope>provided</scope>-->
        </dependency>
        <!--部署成war包时开启【同时支持war和jar启动部署】↑↑↑↑-->
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
        <!--如果想在没有web.xml文件的情况下构建WAR,请设置为false。-->
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!--        添加mybatis加载配置文件-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <!--包含了src/main/java目录下的所有xml资源配置文件-->
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <!--        最终打包的名字与项目名称一致-->
        <finalName>demo</finalName>
    </build>
</project>

(三)、使用IDEA进行clear、install、compile、package的过程

特别说明,在idea中打包顺序按12345执行,特别注意在第2步的install之后需要compile一次,在第4步的package后再执行一次compile进行编译。

打包操作完成后,可以在web目录项目左侧的target中找到demo.war,将demo.war复制到桌面上准备上传至服务器中。

 

(四)、使用FileZilla将demo.war上传至服务器的/usr/local/tomcat/apache-tomcat-8.5.87/webapps目录下

在进行上传之前先停止tomcat服务:

cd /usr/local/tomcat/apache-tomcat-8.5.87/bin/ # 去到tomcat的bin目录下
./shutdown.sh  # 停止tomcat服务

 去到tomcat目录的webapps目录下,查看是否有已经在运行的项目文件:

cd /usr/local/tomcat/apache-tomcat-8.5.87/webapps/ # 放demo.war的目录
ls -l # 查看是否存在*.war结尾的其他项目正在运行的

 如果存在*.war或*.jar的其他项目包,可以进行备份,可以通过修改原有的*.war或*.jar名字进行备份操作:

 最后使用FileZilla将demo.war拖拽到/usr/local/tomcat/apache-tomcat-8.5.87/webapps目录下:

 正在传输:

 传输完成:

(五)、修改/usr/local/tomcat/apache-tomcat-8.5.87/conf/目录下的server.xml配置文件.

显示行号:

 

 修改server.xml的端口号,使其与项目端口号保持一致:

 在server.xml中大约151的位置进行修改tomcat服务文件,将默认访问的地址指向我们自己的web项目demo.war。

附上修改前的整个server.xml文件代码:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!-- APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation. The default
         SSLImplementation will depend on the presence of the APR/native
         library and the useOpenSSL attribute of the AprLifecycleListener.
         Either JSSE or OpenSSL style configuration may be used regardless of
         the SSLImplementation selected. JSSE style configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
         This connector uses the APR/native implementation which always uses
         OpenSSL for TLS.
         Either JSSE or OpenSSL style configuration may be used. OpenSSL style
         configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
               maxThreads="150" SSLEnabled="true" >
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
            <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
                         certificateFile="conf/localhost-rsa-cert.pem"
                         certificateChainFile="conf/localhost-rsa-chain.pem"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <!--
    <Connector protocol="AJP/1.3"
               address="::1"
               port="8009"
               redirectPort="8443" />
    -->

    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

原来的整个Host标签的内容如下:

<Host name="localhost"  appBase="webapps"
      unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>

替换后的Host标签内容如下:

<!--将appBase由webapps改为"" -->
<Host name="localhost"  appBase=""
      unpackWARs="true" autoDeploy="true">
		<!--新增自主项目访问更目录 -->
		<Context path="/" docBase="webapps/demo"  debug="0" privileged="true" reloadable="true"  />	

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

附上修改后的整个server.xml文件代码:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!-- APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8086" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation. The default
         SSLImplementation will depend on the presence of the APR/native
         library and the useOpenSSL attribute of the AprLifecycleListener.
         Either JSSE or OpenSSL style configuration may be used regardless of
         the SSLImplementation selected. JSSE style configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->
    <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
         This connector uses the APR/native implementation which always uses
         OpenSSL for TLS.
         Either JSSE or OpenSSL style configuration may be used. OpenSSL style
         configuration is used below.
    -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
               maxThreads="150" SSLEnabled="true" >
        <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
        <SSLHostConfig>
            <Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
                         certificateFile="conf/localhost-rsa-cert.pem"
                         certificateChainFile="conf/localhost-rsa-chain.pem"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <!--
    <Connector protocol="AJP/1.3"
               address="::1"
               port="8009"
               redirectPort="8443" />
    -->

    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <!--将appBase由webapps改为"" -->
      <Host name="localhost"  appBase=""
            unpackWARs="true" autoDeploy="true">
		<!--新增自主项目访问更目录 -->
		<Context path="/" docBase="webapps/demo"  debug="0" privileged="true" reloadable="true"  />	

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

修改完成后按esc退出编辑模式,使用:wq或:wq!保存server.xml文件并退出.

配置说明:

在`server.xml`文件中,需要以下几个步骤来配置Tomcat以使我们能够访问自己的项目:

1. 确认Tomcat是否有配置好正确的端口,可以通过修改`<Connector>`元素来实现:

```
<Connector port="8086" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />
```
其中,`port`属性表示访问Tomcat的端口号,可以根据自己的需要进行修改。

2. 在`<Host>`元素中,添加一个`<Context>`元素来指定我们的项目路径:

```
<Host name="localhost"  appBase=""
            unpackWARs="true" autoDeploy="true">

      <!-- 添加context -->
      <Context path="/" docBase="webapps/demo" debug="0" privileged="true" />
</Host>
```
其中,`path`属性表示访问自己的项目的URL,`docBase`属性表示自己的项目的路径或者WAR文件的路径,可以修改为你自己的项目路径或WAR文件路径。

3. 保存`server.xml`文件并重启Tomcat,然后就可以通过访问上一步中定义的`path`属性链接来访问我们自己的项目了,比如上面的例子中就可以通过访问`http://localhost:8080/`来访问我们的项目。

注意:修改`server.xml`配置文件需要谨慎操作,并且在重启Tomcat之前务必备份好该文件,以防止配置错误。

(六)、启动tomcat服务

(七)、使用IP:PORT进行访问

此时访问的页面不再是tomcat自带的汤姆猫界面了,而是我们自己的web项目的界面,就说明这个配置是OK的。 

(八)、停止tomcat服务出错

如下图:

原因:端口被占用。造成错误的原因大多是因为未停止tomcat服务就对tomcat的配置文件进行编辑造成的。

解决方案:

①查看哪些服务占用了您配置的端口

 ps -ef | grep tomcat

②可以使用如下命令直接强行杀死占用端口的程序:

kill -9 tomcat

或

kill -9 1458 # 这里的1458就是占用的进程ID

或者重启服务器也可以解决这一类端口占用问题。

八、配置tomcat为服务器开机启动服务

说明:按照上面的方式启动Tomcat,如果我们的虚拟机或者服务器关闭了,重启服务器后Tomcat是关闭的,每次都需要手动启动,比较麻烦。我们希望虚拟机或者服务器重启后,Tomcat可以自己启动,所以我们需要设置Tomcat为开机启动项,具体操作参考下面的操作流程:

(一)、关闭tomcat服务

cd /usr/local/tomcat/apache-tomcat-8.5.87/bin/ # 进入tomcat安装目录的bin目录下

./shutdown.sh # 关闭tomcat服务

(二)、在/usr/local/tomcat/apache-tomcat-8.5.87/bin/目录下创建setenv.sh文件并添加内容

进入tomcat的安装目录下的bin目录并创建setenv.sh文件:

# 进入tomcat的bin目录
cd /usr/local/tomcat/apache-tomcat-8.5.87/bin/
# 查看已有文件
ll
# 创建setenv.sh文件
vim setenv.sh

在setenv.sh文件中添加如下内容:

此处CATALINA_PID中的tomcat.pid系统会自动创建。

# 设置Tomcat的PID文件
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
# 添加JVM选项
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"

(三)、配置/usr/local/tomcat/apache-tomcat-8.5.87/bin/目录下的catalina.sh文件

首先使用命令编辑catalina.sh文件:

# 进入tomcat的bin目录
cd /usr/local/tomcat/apache-tomcat-8.5.87/bin/
# 查看目录结构
ll
# 编辑catalina.sh文件
vim catalina.sh

 此文件很长,前面是很多注释,大概在126行的后面添加JDK的环境变量:

export JAVA_HOME=/usr/local/java/jdk1.8.0_361
export JRE_HOME=/usr/local/java/jdk1.8.0_361/jre

(四)、配置/usr/local/tomcat/apache-tomcat-8.5.87/bin/目录下的setclasspath.sh文件

说明:tomcat启动的大致流程是startup.sh->catalina.sh->setclasspath.sh,而setclasspath.sh的作用就是加载环境变量,使用如下命令对setclasspath.sh进行编辑:

vim /usr/local/tomcat/apache-tomcat-8.5.87/bin/setclasspath.sh

在make和if之间,大致24-25行之间添加JDK环境变量:

export JAVA_HOME=/usr/local/java/jdk1.8.0_361

(五)、配置系统文件

在/usr/lib/systemd/system路径下添加tomcat.service文件,命令如下:

# 编辑tomcat.service文件,如果原来没有会新建立一个tomcat.service文件
vim /usr/lib/systemd/system/tomcat.service 

打开后是一个空的文件,在该文件中插入如下代码:

[Unit]
Description=Tomcat
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
TimeoutSec=0
# 其中/usr/local/tomcat/apache-tomcat-8.5.87/是tomcat的安装目录,此部分内容中有这个地址的都要替换
PIDFile=/usr/local/tomcat/apache-tomcat-8.5.87/tomcat.pid
ExecStart=/usr/local/tomcat/apache-tomcat-8.5.87/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

说明:配置TimeoutSec=0的目的是让开机启动不处理tomcat启动超时,保证tomcat耗时过长时不会被系统terminating。

插入内容完成后按est退出编辑模式,执行命令:wq!保存改文件,并使如下命令使修改过的文件生效:

一定要使用此命令让修改后的文件生效!!!

# 使修改过的文件生效
systemctl daemon-reload

将tomcat服务加入开机自启动列表!!!

systemctl enable tomcat.service

(六)、重启服务并查看相关服务状态

重启服务:

reboot # 重启服务

此时会重启所有服务,建议将服务器重启一次。

重启服务和重启服务器完成后可以查看服务列表:

systemctl status tomcat # 查看tomcat服务

查看开机启动列表:

systemctl list-unit-files | grep enabled

此时直接使用IP:端口访问可以看到已经能访问到项目,说明刚刚的配置是成功的、生效的:

(七)、如果是项目的.war或者.jar有问题,那也是不能访问项目的,比如数据库信息配置不对,如何检查呢?

可以打开两个Xshell窗口,第一个用来启动和停止Tomcat服务,另外一个用来实时查看控制台信息。

当左边执行时,右侧可以看到具体的启动信息,从启动信息中可以看出常规的错误,特别是.war或.jar中如果有错,可以从日志显示中看出来并解决。

综上、Tomcat相关配置完结!

  • 34
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰灰老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值