(Linux) Centos7.* バージョンは、Java 環境、Tomcat、Nginx およびパッケージをインストールして構成し、SSM フレームワーク Web システムをデプロイします

目次

1. ソフトウェアとインストール パッケージを準備する

(1) 必要なソフトウェア

1. クリックして Xshell をダウンロード

2. クリックして FileZilla をダウンロード

(2)、インストール パッケージを準備します。

1.クリックしてJDK1.8Linuxバージョンをダウンロードします

2. クリックしてNginxをダウンロード

3. クリックして Tomcat をダウンロード  

2. FileZilla ソフトウェアの使用方法

(1) FileZillaソフトウェアの開き方と説明

(2)、サーバーに接続するためのサーバーアドレスを設定します 

3. Xshell 設定接続と基本コマンド

(1)、Xshell ソフトウェアを使用する

(2)、Xshell関連の設定と接続

4. Xshell関連のショートカットキーとCentOs7.*のバージョン関連のコマンド説明

(1) Xshell でのショートカットキーの説明

(2)、Xshellの設定

(3), (Linux) CentOs7.* 簡単なコマンド

1. 私はどこにいますか? (システム内の私の現在の場所)

2. どこへ行くの? (目的の場所に行きます)

3. 現在のパスの内容は?

4. ファイルまたはフォルダーを作成する

5. ファイルまたはフォルダーを削除する

6. ファイルまたはフォルダーの名前を変更します。

7. ファイルを解凍します。

8. CentO のシステム バージョン番号を表示します。

9. プロセスまたはポート番号が実行されているかどうかを確認し、プロセスまたはサービスを停止します

5. CentOs7.* システムに Java 環境をインストールして構成する

(1) JDK インストール パッケージをサーバーの対応するディレクトリにアップロードします。

(2)、JDK圧縮パッケージを解凍する

(3)、JDK 環境変数の構成

1. vi または vim ディレクトリを使用して、/etc ディレクトリ内のプロファイル システム ファイルを編集します。

2. /etc/profile ファイルの末尾に次の内容を追加します。

3. 変更したばかりの環境構成を有効にする

4.JDKが正常に構成されているかどうかを確認します

6. CentOs7.* システムに Tomcat 環境をインストールして構成する

(1)、Tomcatを解凍

(2)、Tomcat 構成ファイル server.xml を変更します。

(3) tomcatインストールディレクトリ配下のbinディレクトリに入り、tomcatサービスを起動

(4)、Tomcat サービスの開始と停止

7. IDEA を使用して、Web プロジェクトを war パッケージとしてパッケージ化し、展開およびアクセスのためにサーバーにアップロードします。

(1) Web プロジェクトは、pom.xml でパッケージ方法、パッケージ名、およびパッケージ規則を構成する必要があります。

(2)、pom.xml

(3)、IDEA を使用して、クリア、インストール、コンパイル、パッケージ化のプロセスを実行します。

(4) FileZilla を使用して、demo.war をサーバーの /usr/local/tomcat/apache-tomcat-8.5.87/webapps ディレクトリにアップロードします。

(5) /usr/local/tomcat/apache-tomcat-8.5.87/conf/ ディレクトリにある server.xml 構成ファイルを変更します。

(6)、Tomcat サービスを開始する

(7) IP:PORTでアクセス

(8)、Tomcatサービス停止エラー

8. サーバーのサービスを開始するように tomcat を構成します。

(1)、Tomcat サービスを閉じる

(2) /usr/local/tomcat/apache-tomcat-8.5.87/bin/ ディレクトリに setenv.sh ファイルを作成し、内容を追加

(3) /usr/local/tomcat/apache-tomcat-8.5.87/bin/ ディレクトリに catalina.sh ファイルを設定します

(4) /usr/local/tomcat/apache-tomcat-8.5.87/bin/ディレクトリにsetclasspath.shファイルを設定

(5)、システム ファイルの構成

(6)、サービスを再起動し、関連するサービスのステータスを確認します


1. ソフトウェアとインストール パッケージを準備する

Lazy バージョンです。CSDN からアップロードしたリソース パックを直接クリックしてダウンロードできます (無料)。

CSDN のソフトウェア インストール パッケージおよび Linux 必須インストール パッケージ

以下のリンクをクリックして、Baidu Netdisk からダウンロードすることもできます。

Baidu Netdisk のソフトウェア インストール パッケージおよび Linux 必須インストール パッケージ

抽出コード: 1234

リソース バンドルの構造:

 バージョンの要件が異なる場合は、以下の対応するリンクに移動して、関連するバージョンを見つけてください。

(1) 必要なソフトウェア

1.クリックして Xshell をダウンロード

機能: Xshell は、主にサーバーへのリモート接続、サーバーを操作するための Linux コマンドの作成などに使用されます。

2.クリックして FileZilla をダウンロード

機能: FileZilla を使用して、ファイルまたはフォルダーをサーバーにアップロードします。

(2)、インストール パッケージを準備します。

1.クリックしてJDK1.8Linuxバージョンをダウンロードします

2.クリックしてNginxをダウンロード

3.クリックして Tomcat をダウンロード  

以上で、必要なソフトウェアとインストール パッケージの準備が整いました。

2. FileZilla ソフトウェアの使用方法

(1) FileZillaソフトウェアの開き方と説明

1. ダブルクリックして開きます。

2. 開くと、次のようになります。

(2)、サーバーに接続するためのサーバーアドレスを設定します 

1.左上隅のファイルをクリックし、[Site Manager]をクリックします。

2.サイト マネージャーが開いたら、次のように新しいサイトをクリックします。

3.サーバーの IP アドレス (パブリック アドレス)、アクセス プロトコルは SFTP、ポート番号: 22、ユーザー名: root を 構成します。

構成後は次のようになります。

注: [接続] をクリックしても接続に失敗したか、タイムアウトが表示された場合は、以下を確認してください。

1. サーバーのポート 22 が開いているかどうか

2. プロトコルが SFTP かどうか

3. ユーザー名とパスワードが正しいかどうかを確認します. Alibaba Cloud などのメーカーのサーバーである場合は、対応する Web サイトのインスタンスに移動してサーバーを見つけ、インスタンスのパスワードを変更できます.

 4. 接続が成功したら、サーバー サイトをクリックして、対応するマウント ポイントのフォルダーとファイルを表示できます。

 

 

注: このステップの後、対応する java、nginx、tomcat フォルダーが /usr/local の下にないため、Xshell を使用して対応するフォルダーを作成する必要があります。この部分の操作については、第 3 部を参照してください。 

5. 接続が成功したら、サーバー サイトをクリックして、対応するマウント ポイントのフォルダーとファイルを表示できます。

jdk、tomcat、nginx、および mysql に対応するインストール パッケージを対応するディレクトリに転送します。例として jdk を転送します。

以上で、FileZilla の設定と接続は完了です。

3. Xshell 設定接続と基本コマンド

(1)、Xshell ソフトウェアを使用する

1. Xshell を開く 

                

2.開封後はこんな感じ

(2)、Xshell関連の設定と接続

1. 新しいセッションを作成する

 2. クリックして接続

 3. [ユーザー認証]をクリックします。

4.セッションマネージャーをダブルクリックしてサーバーに接続します

5.接続成功

6. 接続失敗時の処理 

注: 接続に失敗した場合、またはエラー メッセージが表示された場合は、セッション マネージャー ---「aliyun----」プロパティを 1 回クリックしてください。

 これは、IP アドレス、ユーザー名、およびパスワードがサーバー アカウントのパスワードおよびその他の構成情報であるかどうかを確認することに焦点を当てて、プロパティをクリックした後のケースです。

 以上で、Xshell関連の設定と接続は完了です!

4. Xshell関連のショートカットキーとCentOs7.*のバージョン関連のコマンド説明

(1) Xshell でのショートカットキーの説明

通常、コンテンツを外部の Xshell にコピーするために使用し、Shift + Insert を使用してコンテンツを内部にコピーします。

Xshell でコンテンツをコピーするには、最初にコンテンツを選択し、ctrl + insertを押してコピーする必要があります。

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

(2)、Xshellの設定

多くの友人は、次のように設定できる簡潔な Xshell を好みます。

①テーマと色を設定する

②透明度を設定

 ③シンプルなインターフェースを設定する

(3), (Linux) CentOs7.* 簡単なコマンド

経験から言えば、Linux オペレーティング システムやそのコマンドを学習するのは非常に退屈です.学習するための最良の方法は、Windows システムと比較して対象を絞った演習を行うことです.

たとえば、Windows では、自分が現在どのフォルダーにいるかに常に注意を払っています。どのフォルダに移動しますか? 私は何をする必要がありますか?

1. 私はどこにいますか? (システム内の私の現在の場所)

ウィンドウズ:

        私はCドライブ---> Program Files---> Bonjourのパスにいます。

Linux:    

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

 Linuxにはドライブレターのようなものはなく、WindowsにはいわゆるCドライブやDドライブという概念がなく、Linuxはルートノードを頂点とする逆さツリー状のディレクトリです。図に示すように、ツリー:

つまり、現在の位置を表示する必要がある場合は、pwdコマンドを使用できます。

2. どこへ行くの? (目的の場所に行きます)

ウィンドウズ:

        CドライブからDドライブの特定のフォルダに移動し、対応するDドライブに対応するファイルをクリックして入力します。

Linux:

        cd target path このコマンドは、移動先のターゲットの場所を参照します。

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. CentO のシステム バージョン番号を表示します。

cat /etc/centos-release

9. プロセスまたはポート番号が実行されているかどうかを確認し、プロセスまたはサービスを停止します

次のコマンドを使用して、Tomcat が実際に実行されているかどうかを確認できます。
1. Tomcat プロセスが存在するかどうかを確認します。

ps -ef | grep tomcat

2. Tomcat がポート 8080 でリッスンしているかどうかを確認します

netstat -an | grep 8080

Tomcat プロセスが存在し、ポート 8080 でリッスンしている場合、Tomcat は実行されています。

まとめると、Xshell と CentOs7.* バージョンに関する簡単なコマンドが完成しました。

5. CentOs7.* システムに Java 環境をインストールして構成する

(1) JDK インストール パッケージをサーバーの対応するディレクトリにアップロードします。

FileZilla を使用してサーバーに接続し、サーバーの /usr/local/java ディレクトリを見つけます。ここの Java は、mkdir java を使用して自分で作成したものです。ない場合は作成できます。

(2)、JDK圧縮パッケージを解凍する

Xshell を使用してサーバーに接続し、cd コマンドを使用して /usr/local/java ディレクトリに入り、tar -zxvf jdk-8u361-linux-x64.tar.gz を使用して JDK 圧縮パッケージを解凍します。

(3)、JDK 環境変数の構成

1. vi または vim ディレクトリを使用して、/etc ディレクトリ内のプロファイル システム ファイルを編集します。

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

 次に、Enter キーを押します。

 :set nu を使用して、ファイルの行番号を表示できます。

 入力方法を英語に調整し、i を押してシステム ファイルを編集します。

2. /etc/profile ファイルの末尾に次の内容を追加します。

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

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编译命令

 要約すると、JDK 環境は CentOs7.* システムにインストールおよび構成されています。

6. CentOs7.* システムに Tomcat 環境をインストールして構成する

(1)、Tomcatを解凍

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

(2)、Tomcat 構成ファイル server.xml を変更します。

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

 構成ファイルを編集します。

 行番号を表示:

:セットなし

i キーを押して編集モードに入り、69 行目あたりでポート番号を変更して、次のコマンドを実行します: wq! Forced to save:

(3) tomcatインストールディレクトリ配下のbinディレクトリに入り、tomcatサービスを起動

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

(4)、Tomcat サービスの開始と停止

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

 注: 開始コマンドの前にダッシュがあります。この ./ を見逃すことはできません。./ の意味は、マウスをダブルクリックして Windows で .exe ファイルを実行することに似ています。

ブラウザに IP:port と入力します。つまり、アクセスする IP アドレスのコロンのポート番号です。

この時点で Tom cat が表示されると、Tomcat サービスが正常に使用できることが証明されます。

 

Tomcat 関連のコマンド:

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

7. IDEA を使用して、Web プロジェクトを war パッケージとしてパッケージ化し、展開およびアクセスのためにサーバーにアップロードします。

要約: この部分で解決すべき主な問題は、Tomcat のホームページがデフォルトで IP:PORT を使用してアクセスされることです。つまり、Tomcat を含むページに、Web プロジェクトにアクセスさせるにはどうすればよいでしょうか?

(1) Web プロジェクトは、pom.xml でパッケージ方法、パッケージ名、およびパッケージ規則を構成する必要があります。

(2)、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>

(3)、IDEA を使用して、クリア、インストール、コンパイル、パッケージ化のプロセスを実行します。

特に、アイデアのパッケージ化シーケンスは 12345 に従って実行されます。手順 2 のインストール後に一度コンパイルし、手順 4 のパッケージ化後に再度コンパイルすることに特に注意してください。

パッケージング操作が完了すると、web ディレクトリのプロジェクトの左側にあるターゲットに demo.war が見つかります。demo.war をデスクトップにコピーして、サーバーにアップロードする準備をします。

 

(4) 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 ディレクトリにドラッグします。

 送信:

 転送完了:

(5) /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 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 を再起動する前に必ずファイルをバックアップしてください。

(6)、Tomcat サービスを開始する

(7) IP:PORTでアクセス

このときアクセスするページは、Tomcat に付属している Tomcat インターフェイスではなく、自作の Web プロジェクトのインターフェイスになっているので、この構成で問題ありません。 

(8)、Tomcatサービス停止エラー

以下に示すように:

理由: ポートは使用中です。ほとんどのエラーは、Tomcat サービスを停止せずに Tomcat 構成ファイルを編集したことが原因です。

解決:

①設定したポートを占有しているサービスを確認する

 ps -ef | grep tomcat

② 次のコマンドを使用して、ポートを直接占有しているプログラムを強制終了できます。

kill -9 tomcat

或

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

または、サーバーを再起動すると、このタイプのポート占有の問題も解決できます。

8. サーバーのサービスを開始するように tomcat を構成します。

注: 上記の方法で Tomcat を起動します. 弊社の仮想マシンまたはサーバーが閉じていると、サーバーを再起動すると Tomcat が閉じてしまいます. 毎回手動で起動する必要があり面倒です. 仮想マシンまたはサーバの再起動後、Tomcatが自動起動できることを期待するため、Tomcatを起動項目に設定する必要があります具体的な操作については、以下の操作手順を参照してください。

(1)、Tomcat サービスを閉じる

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

./shutdown.sh # 关闭tomcat服务

(2) /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"

(3) /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

(4) /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

(5)、システム ファイルの構成

tomcat.service ファイルを /usr/lib/systemd/system パスの下に追加します。コマンドは次のとおりです。

# 编辑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 が長時間かかる場合にシステムによって終了されないようにすることができます。

コンテンツを挿入したら、est を押して編集モードを終了し、コマンド wq! を実行して変更したファイルを保存し、次のコマンドで変更したファイルを有効にします。

変更したファイルを有効にするには、必ずこのコマンドを使用してください。! !

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

tomcat サービスを boot self-start リストに追加してください! ! !

systemctl enable tomcat.service

(6)、サービスを再起動し、関連するサービスのステータスを確認します

サービスを再起動します。

reboot # 重启服务

このとき、すべてのサービスが再起動されますので、一度サーバーを再起動することをお勧めします。

サービスを再起動してサーバーを再起動すると、サービス リストを表示できます。

systemctl status tomcat # 查看tomcat服务

ブート リストを表示します。

systemctl list-unit-files | grep enabled

この時点で、IP: ポート アクセスを直接使用して、プロジェクトに既にアクセスできることを確認できます。これは、今の構成が成功し、効果的であることを示しています。

以上で、Tomcat の設定は完了です。

おすすめ

転載: blog.csdn.net/lishihuijava/article/details/130214435