(Linux) Centos7.* version installs and configures Java environment, Tomcat, Nginx and packages and deploys SSM framework web system

Table of contents

1. Prepare the software and installation package

(1) Necessary software

1. Click to download Xshell

2. Click to download FileZilla

(2), prepare the installation package

1. Click to download JDK1.8Linux version

2. Click to download Nginx

3. Click to download Tomcat  

2. Instructions for using FileZilla software

(1) Opening and description of FileZilla software

(2), configure the server address to connect to the server 

3. Xshell configuration connection and basic commands

(1), use Xshell software

(2), Xshell related configuration and connection

4. Xshell-related shortcut keys and CentOs7.* version-related command instructions

(1) Description of shortcut keys in Xshell

(2), Xshell settings

(3), (Linux) CentOs7.* simple commands

1. Where am I? (my current location in the system)

2. Where am I going? (I'm going to the target location)

3. What is the content of my current path?

4. Create a file or folder

5. Delete a file or folder

6. Modify the file or folder name:

7. Unzip the file:

8. View the system version number of CentOs:

9. Check whether a process or port number is running and stop the process or service

5. Install and configure Java environment in CentOs7.* system

(1) Upload the JDK installation package to the corresponding directory of the server

(2), unzip the JDK compressed package

(3), configure JDK environment variables

1. Use the vi or vim directory to edit the profile system file in the /etc directory.

2. Add the following content at the end of the /etc/profile file:

3. Make the environment configuration just modified take effect

4. Check whether the JDK is configured successfully

6. Install and configure Tomcat environment in CentOs7.* system

(1), unzip tomcat

(2), modify the tomcat configuration file server.xml

(3), enter the bin directory under the tomcat installation directory and start the tomcat service

(4), start and stop tomcat service

7. Use IDEA to package the web project as a war package and upload it to the server for deployment and access

(1) The web project needs to configure the packaging method, packaging name and packaging rules in pom.xml

(2), pom.xml

(3), use IDEA to carry out the process of clear, install, compile, package

(4) Use FileZilla to upload demo.war to the /usr/local/tomcat/apache-tomcat-8.5.87/webapps directory of the server

(5) Modify the server.xml configuration file in the /usr/local/tomcat/apache-tomcat-8.5.87/conf/ directory.

(6), start the tomcat service

(7) Use IP:PORT to access

(8), stop tomcat service error

8. Configure tomcat to start the service for the server

(1), close the tomcat service

(2) Create a setenv.sh file in the /usr/local/tomcat/apache-tomcat-8.5.87/bin/ directory and add content

(3), configure the catalina.sh file in the /usr/local/tomcat/apache-tomcat-8.5.87/bin/ directory

(4), configure the setclasspath.sh file in the /usr/local/tomcat/apache-tomcat-8.5.87/bin/ directory

(5), configure system files

(6), restart the service and check the status of related services


1. Prepare the software and installation package

Lazy version, you can directly click to download the resource pack I uploaded from CSDN (free):

Software installation package and Linux required installation package for CSDN

You can also click the link below to download from Baidu Netdisk:

Software installation package and Linux required installation package for Baidu Netdisk

Extract code: 1234

Resource bundle structure:

 If you have different requirements for the version, go to the corresponding link below to find the relevant version:

(1) Necessary software

1. Click to download Xshell

Function: Xshell is mainly used to remotely connect to the server, write Linux commands to operate the server, etc.

2. Click to download FileZilla

Function: Use FileZilla to upload files or folders to the server.

(2), prepare the installation package

1. Click to download JDK1.8Linux version

2. Click to download Nginx

3. Click to download Tomcat  

In summary, the required software and installation package are ready!

2. Instructions for using FileZilla software

(1) Opening and description of FileZilla software

1. Double-click to open:

2. After opening, it looks like this:

(2), configure the server address to connect to the server 

1. Click the file in the upper left corner , click Site Manager

2. After the site manager is opened, click the new site as follows :

3. Configure the server IP address (public address), the access protocol is SFTP, port number: 22, user name: root 

After configuration is like this:

Note: If clicking connect shows connection failed or timed out, please check:

1. Whether port 22 of the server is open

2. Whether the protocol is SFTP

3. Check whether the user name and password are correct. If it is a server of a manufacturer such as Alibaba Cloud, you can go to the instance of the corresponding website to find the server and modify the instance password

 4. After the connection is successful, you can click the server site to view the folders and files in the corresponding mount point

 

 

Note: After this step, there are no corresponding java, nginx, tomcat folders under your /usr/local, you need to use Xshell to create corresponding folders, please refer to the third part for the operation of this part. 

5. After the connection is successful, you can click the server site to view the folders and files in the corresponding mount point

Transfer the installation packages corresponding to jdk, tomcat, nginx and mysql to the corresponding directory, taking transferring jdk as an example:

In summary, the configuration and connection of FileZilla is complete!

3. Xshell configuration connection and basic commands

(1), use Xshell software

1. Open Xshell 

                

2. After opening, it looks like this

(2), Xshell related configuration and connection

1. Create a new session

 2. Click to connect

 3. Click User Authentication

4. Double-click the session manager to connect to the server

5. The connection is successful

6. Connection failure handling 

Note: If the connection fails or there is an error message, please click on the session manager---"aliyun----"properties once

 This is the case after clicking on the properties, focusing on checking whether the IP address, user name and password are the server account password and other configuration information:

 In summary, Xshell related configuration and connection are complete!

4. Xshell-related shortcut keys and CentOs7.* version-related command instructions

(1) Description of shortcut keys in Xshell

Usually we use to copy the content outside to Xshell, use shift + insert to copy the content in;

To copy the content in Xshell, you need to select the content first, and press ctrl + insert to copy.

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

(2), Xshell settings

Many friends like the concise Xshell, which can be set:

①Set the theme and color

②Set transparency

 ③Set up a simple interface

(3), (Linux) CentOs7.* simple commands

Speaking from experience: Learning the Linux operating system or its commands is very boring. The best way to learn is to conduct targeted exercises compared with the Windows system.

For example, in Windows, we always pay attention to which folder I am currently in? Which folder do I go to? What do I need to do?

1. Where am I? (my current location in the system)

windows:

        I am in the path of C drive--->Program Files--->Bonjour.

Linux:    

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

 There is no such thing as a drive letter in Linux, and there is no such concept as the so-called C drive and D drive in Windows. Linux is an upside-down tree-shaped directory with the root node as the top of the tree, as shown in the figure:

Therefore, we must always know where we are in this fallen tree, that is, if we need to view the current position, we can use: pwd command

2. Where am I going? (I'm going to the target location)

windows:

        I go from the C drive to a certain folder on the D drive, just click on the file corresponding to the corresponding D drive to enter.

Linux:

        cd target path This command refers to which target location you want to go to.

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

Example of use:

3. What is the content of my current path?

windows (what content can be seen at a glance):

        

Linux:

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

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

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

Example of use:

4. Create a file or folder

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

5. Delete a file or folder

When deleting files or folders, you need to pay attention to details. The rm command can only delete files, but cannot delete empty folders or directories with content in folders.

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

6. Modify the file or folder name:

7. Unzip the file:

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

8. View the system version number of CentOs:

cat /etc/centos-release

9. Check whether a process or port number is running and stop the process or service

You can use the following command to check whether tomcat is actually running:
1. Check whether the Tomcat process exists

ps -ef | grep tomcat

2. Check whether Tomcat is listening on port 8080

netstat -an | grep 8080

Tomcat is running if the Tomcat process exists and listens on port 8080

In summary, the simple commands about Xshell and CentOs7.* versions are finished!

5. Install and configure Java environment in CentOs7.* system

(1) Upload the JDK installation package to the corresponding directory of the server

Use FileZilla to connect to the server and find the /usr/local/java directory of the server. The java here is created by ourselves using mkdir java. If not, you can create one.

(2), unzip the JDK compressed package

Use Xshell to connect to the server, use the cd command to enter the /usr/local/java directory, and use tar -zxvf jdk-8u361-linux-x64.tar.gz to decompress the JDK compressed package.

(3), configure JDK environment variables

1. Use the vi or vim directory to edit the profile system file in the /etc directory.

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

 Then press enter:

 You can use :set nu to display file line numbers:

 Adjust the input method to English, press i to edit the system file:

2. Add the following content at the end of the /etc/profile file:

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

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

The content after JAVA_HOME= is the decompressed location of your server's JDK. You can find the location of the JDK through the following command:

① When the location of the JDK is known, it is as follows:

 ② When you forget the JDK installation path, as follows:

 The configured system files are as follows:

After editing, press the esc key to exit the editing mode, enter :wq to save, if the save fails, use :wq! to force the save, as follows:

Appears red:

Then use :wq! to force the save.

 

 Confirm whether the content just modified is saved successfully:

Use vim /etc/profile to enter the file you just edited to check whether there is any configuration content about JDK at the end. If there is, it has been saved successfully. If not, just execute the above editing operation again.

3. Make the environment configuration just modified take effect

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

4. Check whether the JDK is configured successfully

java -version # 查看JDK版本

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

 To sum up, the JDK environment has been installed and configured in the CentOs7.* system.

6. Install and configure Tomcat environment in CentOs7.* system

(1), unzip tomcat

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

(2), modify the tomcat configuration file server.xml

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

 Edit configuration file:

 Show line numbers:

:set no

Press the i key to enter the edit mode, modify the port number at about line 69, and execute: wq! Forced to save:

(3), enter the bin directory under the tomcat installation directory and start the tomcat service

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

(4), start and stop tomcat service

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

 Note: There is a dash in front of the start command, this ./ cannot be missing, the meaning of ./ is similar to double-clicking the mouse to run the .exe file in windows

Enter IP:port in the browser, that is, the IP address colon port number to access:

Seeing a Tom cat at this time proves that your tomcat service can be used normally.

 

Tomcat related commands:

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

7. Use IDEA to package the web project as a war package and upload it to the server for deployment and access

Summary: The main problem to be solved in this part is that the homepage of tomcat accessed by default using IP:PORT, that is, the page with Tomcat, how to make it access our web project?

(1) The web project needs to configure the packaging method, packaging name and packaging rules in pom.xml

(2), pom.xml

 For the convenience of checking, the pom.xml used by the moderator is attached

<?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>

(3), use IDEA to carry out the process of clear, install, compile, package

In particular, the packaging sequence in the idea is executed according to 12345. Pay special attention to compile once after the install in step 2, and compile again after the package in step 4.

After the packaging operation is completed, you can find demo.war in the target on the left side of the project in the web directory, copy demo.war to the desktop and prepare to upload it to the server.

 

(4) Use FileZilla to upload demo.war to the /usr/local/tomcat/apache-tomcat-8.5.87/webapps directory of the server

Stop the tomcat service before uploading:

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

 Go to the webapps directory of the tomcat directory to see if there are already running project files:

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

 If there are other project packages of *.war or *.jar, they can be backed up, and the backup operation can be performed by modifying the original *.war or *.jar name:

 Finally, use FileZilla to drag demo.war to the /usr/local/tomcat/apache-tomcat-8.5.87/webapps directory:

 Transmitting:

 Transfer complete:

(5) Modify the server.xml configuration file in the /usr/local/tomcat/apache-tomcat-8.5.87/conf/ directory.

Show line numbers:

 

 Modify the port number of server.xml to be consistent with the project port number:

 Modify the tomcat service file at about 151 in server.xml, and point the default access address to our own web project demo.war.

Attach the entire server.xml file code before modification:

<?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>

The content of the original entire Host tag is as follows:

<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>

The content of the replaced Host tag is as follows:

<!--将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>

Attach the modified entire server.xml file code:

<?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>

After modification, press esc to exit the editing mode, use :wq or :wq! to save the server.xml file and exit.

Configuration instructions:

In the `server.xml` file, the following steps are required to configure Tomcat so that we can access our own projects:

1. Confirm whether Tomcat has configured the correct port, which can be achieved by modifying the `<Connector>` element:

```
<Connector port="8086" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />
```
Among them, the `port` attribute indicates the port number for accessing Tomcat, which can be customized according to your needs to modify.

2. In the `<Host>` element, add a `<Context>` element to specify our project path:

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

      <!-- Add context -->
      <Context path="/" docBase="webapps/demo" debug="0" privileged="true" />
</Host>
```
Among them, the `path` attribute indicates access The URL of your own project, the `docBase` attribute indicates the path of your own project or the path of the WAR file, which can be modified to your own project path or the path of the WAR file.

3. Save the `server.xml` file and restart Tomcat, then you can access our own project by accessing the `path` attribute link defined in the previous step, for example, in the above example, you can access `http:// localhost:8080/` to visit our project.

Note: Modifying the `server.xml` configuration file requires careful operation, and be sure to back up the file before restarting Tomcat to prevent configuration errors.

(6), start the tomcat service

(7) Use IP:PORT to access

The page accessed at this time is no longer the Tomcat interface that comes with tomcat, but the interface of our own web project, which means that this configuration is OK. 

(8), stop tomcat service error

As shown below:

Reason: The port is in use. Most of the errors are caused by editing the tomcat configuration file without stopping the tomcat service.

solution:

① Check which services occupy the port you configured

 ps -ef | grep tomcat

② You can use the following command to forcibly kill the program that occupies the port directly:

kill -9 tomcat

或

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

Or restarting the server can also solve this type of port occupation problem.

8. Configure tomcat to start the service for the server

Note: Start Tomcat according to the above method. If our virtual machine or server is closed, Tomcat will be closed after restarting the server. It needs to be started manually every time, which is troublesome. We hope that after the virtual machine or server restarts, Tomcat can start by itself, so we need to set Tomcat as the startup item. For specific operations, refer to the following operation process:

(1), close the tomcat service

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

./shutdown.sh # 关闭tomcat服务

(2) Create a setenv.sh file in the /usr/local/tomcat/apache-tomcat-8.5.87/bin/ directory and add content

Enter the bin directory under the tomcat installation directory and create the setenv.sh file:

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

Add the following content to the setenv.sh file:

Here the tomcat.pid system in CATALINA_PID will be created automatically.

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

(3), configure the catalina.sh file in the /usr/local/tomcat/apache-tomcat-8.5.87/bin/ directory

First edit the catalina.sh file with the command:

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

 This file is very long, with a lot of comments in front, and add JDK environment variables after about line 126:

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

(4), configure the setclasspath.sh file in the /usr/local/tomcat/apache-tomcat-8.5.87/bin/ directory

Description: The general process of tomcat startup is startup.sh->catalina.sh->setclasspath.sh, and the function of setclasspath.sh is to load environment variables. Use the following command to edit setclasspath.sh:

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

Between make and if, add JDK environment variables between lines 24-25:

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

(5), configure system files

Add the tomcat.service file under the /usr/lib/systemd/system path, the command is as follows:

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

After opening, it is an empty file, insert the following code in the file:

[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

Explanation: The purpose of configuring TimeoutSec=0 is to make the tomcat startup timeout not processed when the boot is started, so as to ensure that the tomcat will not be terminated by the system when it takes too long.

After inserting the content, press est to exit the editing mode, execute the command: wq! to save the modified file, and make the modified file take effect with the following command:

Be sure to use this command to make the modified file take effect! ! !

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

Add the tomcat service to the boot self-start list! ! !

systemctl enable tomcat.service

(6), restart the service and check the status of related services

Restart the service:

reboot # 重启服务

At this time, all services will be restarted. It is recommended to restart the server once.

After restarting the service and restarting the server, you can view the service list:

systemctl status tomcat # 查看tomcat服务

View the boot list:

systemctl list-unit-files | grep enabled

At this time, you can directly use IP: port access to see that you can already access the project, indicating that the configuration just now is successful and effective:

In summary, the configuration of Tomcat is complete!

Guess you like

Origin blog.csdn.net/lishihuijava/article/details/130214435