Home

Tomcat Setup Memo

■JDKインストール
・最新化
kokaki@skynew:~$ sudo apt update -y && sudo apt upgrade -y
・インストール
kokaki@skynew:~$ sudo apt install openjdk-21-jdk -y
・バージョン確認
kokaki@skynew:~$ java -version
    openjdk version "21.0.5" 2024-10-15
    OpenJDK Runtime Environment (build 21.0.5+11-Ubuntu-1ubuntu124.04)
    OpenJDK 64-Bit Server VM (build 21.0.5+11-Ubuntu-1ubuntu124.04, mixed mode, sharing)
■Tomcatインストール
・ダウンロード
kokaki@skynew:~$ curl -O https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.2/bin/apache-tomcat-11.0.2.tar.gz
・展開
kokaki@skynew:~$ tar zxvf apache-tomcat-11.0.2.tar.gz
・移動
kokaki@skynew:~$ sudo mv apache-tomcat-11.0.2 /usr/libexec/tomcat11
・ユーザ作成
kokaki@skynew:~$ sudo useradd -M -d /usr/libexec/tomcat11 tomcat
・所有者変更
kokaki@skynew:~$ sudo chown -R tomcat /usr/libexec/tomcat11
・サービス作成
kokaki@skynew:~$ sudo vi /usr/lib/systemd/system/tomcat11.service
    [Unit]
    Description=Apache Tomcat 11
    After=network.target

    [Service]
    Type=oneshot
    ExecStart=/usr/libexec/tomcat11/bin/startup.sh
    ExecStop=/usr/libexec/tomcat11/bin/shutdown.sh
    RemainAfterExit=yes
    User=tomcat
    Group=tomcat

    [Install]
    WantedBy=multi-user.target
・サービス登録
kokaki@skynew:~$ sudo systemctl enable --now tomcat11
    Created symlink /etc/systemd/system/multi-user.target.wants/tomcat11.service → /usr/lib/systemd/system/tomcat11.service.
・Tomcatユーザ権限付与
kokaki@skynew:~$ sudo vi /usr/libexec/tomcat11/conf/tomcat-users.xml
    <role rolename="admin-gui"/>
    <role rolename="manager-gui"/>
    <user username="tomcat" password="admin" roles="admin-gui,manager-gui"/>
-- localhost以外からアクセスする場合
kokaki@skynew:~$ sudo vi /usr/libexec/tomcat11/webapps/manager/META-INF/context.xml
    <Context antiResourceLocking="false" privileged="true" >
    <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                    sameSiteCookies="strict" />
    <!--Valve className="org.apache.catalina.valves.RemoteAddrValve" ★無効化
            allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /-->      ★  〃
    <Manager sessionAttributeValueClassNameFilter=
        "java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\
        .filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
    </Context>
・Tomcat再起動
kokaki@skynew:~$ sudo systemctl restart tomcat11
・Tomcatアプリ用テーブル作成
kokaki@skynew:~$ mysql -udev -p dev
    Enter password:
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 57
    Server version: 8.0.40-0ubuntu0.24.04.1 (Ubuntu)

    Copyright (c) 2000, 2024, Oracle and/or its affiliates.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    mysql> source tomcat_war_table.sql
    Query OK, 0 rows affected, 1 warning (0.03 sec)

    Query OK, 0 rows affected, 2 warnings (0.09 sec)

    Query OK, 0 rows affected, 1 warning (0.01 sec)

    Query OK, 0 rows affected (0.06 sec)

    Query OK, 1 row affected (0.02 sec)

    Query OK, 1 row affected (0.01 sec)

    Query OK, 0 rows affected, 1 warning (0.01 sec)

    Query OK, 0 rows affected (0.06 sec)

    Query OK, 1 row affected (0.02 sec)

    Query OK, 1 row affected (0.01 sec)

    mysql> select * from user;
    +----+-----------------+-----------------------+---------------+---------------------+---------------------+-------------+
    | id | name            | address               | phone         | update_date         | create_date         | delete_date |
    +----+-----------------+-----------------------+---------------+---------------------+---------------------+-------------+
    |  1 | テスト太郎       | 東京都品川区1-1        | 090-0000-0000 | 2025-01-02 14:33:46 | 2025-01-02 14:33:46 | NULL        |
    |  2 | テスト次郎       | 東京都渋谷区1-1        | 080-0000-0000 | 2025-01-02 14:33:46 | 2025-01-02 14:33:46 | NULL        |
    +----+-----------------+-----------------------+---------------+---------------------+---------------------+-------------+
    2 rows in set (0.00 sec)

    mysql> select * from userinfo;
    +----+-----------------+-----------------------+---------------+---------------------+---------------------+-------------+
    | id | name            | address               | phone         | update_date         | create_date         | delete_date |
    +----+-----------------+-----------------------+---------------+---------------------+---------------------+-------------+
    |  1 | テスト太郎       | 東京都品川区1-1        | 090-0000-0000 | 2025-01-02 14:33:46 | 2025-01-02 14:33:46 | NULL        |
    |  2 | テスト次郎       | 東京都渋谷区1-1        | 080-0000-0000 | 2025-01-02 14:33:46 | 2025-01-02 14:33:46 | NULL        |
    +----+-----------------+-----------------------+---------------+---------------------+---------------------+-------------+
    2 rows in set (0.00 sec)

    mysql> quit
    Bye
■Apache2,Tomcat11連携
kokaki@skynew:~$ sudo a2enmod proxy
    Enabling module proxy.
    To activate the new configuration, you need to run:
    systemctl restart apache2

    kokaki@skynew:~$ sudo a2enmod proxy_ajp
    Considering dependency proxy for proxy_ajp:
    Module proxy already enabled
    Enabling module proxy_ajp.
    To activate the new configuration, you need to run:
    systemctl restart apache2
kokaki@skynew:~$ sudo vi /usr/libexec/tomcat11/conf/server.xml
    <Connector protocol="AJP/1.3"
                   address="0.0.0.0"
                      port="8009"
              redirectPort="8443"
            secretRequired="false" />
kokaki@skynew:~$ sudo vi /etc/apache2/sites-available/tomcat.conf
    ProxyPass /manager ajp://localhost:8009/manager
    ProxyPassReverse /manager ajp://localhost:8009/manager

    ProxyPass /springMyBatis ajp://localhost:8009/springMyBatis
    ProxyPassReverse /springMyBatis ajp://localhost:8009/springMyBatis

    ProxyPass /springJpa ajp://localhost:8009/springJpa
    ProxyPassReverse /springJpa ajp://localhost:8009/springJpa

    ProxyPass /memoapp ajp://localhost:8009/memoapp
    ProxyPassReverse /memoapp ajp://localhost:8009/memoapp
kokaki@skynew:~$ sudo a2ensite tomcat
kokaki@skynew:~$ sudo systemctl restart apache2
kokaki@skynew:~$ sudo systemctl restart tomcat11

http://localhost:8080/
Tomcat

http://localhost/manager
Tomcat Manager

http://localhost/memoapp
memoapp

http://localhost/memoapp?db=MySQL
memoapp(MySQL版)

http://localhost/springMyBatis
springMyBatis

http://localhost/springJpa
springJpa

Home

2025 kokaki.jp, Office.