Tomcat

提供: ArchWiki
2016年6月17日 (金) 00:20時点におけるKusakata (トーク | 投稿記録)による版 (ページの作成:「Category:ウェブサーバー de:Apache Tomcat en:Tomcat Tomcat は Apache ソフトウェア財団によって開発されているオープンソースの...」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

Tomcat は Apache ソフトウェア財団によって開発されているオープンソースの Java Servlet コンテナです。

ノート: Tomcat には現在3つの安定ブランチが存在します: 6, 7, 8。新しいバージョンしか動作しないということはなく、各ブランチはそれぞれ "Servlet" と "JSP" の Java 標準仕様を実装 しています。Arch Linux では全てのバージョンが公式にサポートされています: tomcat6, tomcat7, tomcat8。使用したいウェブアプリケーションの要件にあわせて必要なバージョンは変わります。Tomcat を試してみたいだけであまり時間を割きたくない場合、tomcat7 を試してみると良いでしょう。この wiki ページでは tomcat7 について記述していますが、大体の内容は tomcat6 や tomcat8 でも同じです。

インストール

tomcat6, tomcat7, tomcat8 のどれかをインストールしてください。

Tomcat を本番環境にデプロイするときは、tomcat-native をインストールすることも考慮してください。Tomcat のネイティブライブラリは Apache Portable Runtime (APR) ライブラリのネットワーク接続 (ソケット) と RNG 実装を使用するようにサーバーを設定します。ネイティブの32・64ビットのコードを使うことでパフォーマンスが改善されるので、速度が重要な本番環境でしばしば使われています。デフォルトの Tomcat に設定を加える必要はありません。詳しくは 公式の Tomcat ドキュメント を参照。

tomcat-native を使用すると catalina.err の以下の警告が消えます:

INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path [...]

ファイルシステム階層

パス名 目的
/usr/share/tomcat7 スクリプトや他のディレクトリのリンクが存在する Tomcat のメインフォルダ。
/usr/share/java/tomcat7 Tomcat の Java ライブラリ (jar)。
/etc/tomcat7 設定ファイル。例: tomcat-users.xml (管理ツールを使用するユーザーやロールを定義), server.xml (Tomcat のメイン設定ファイル), catalina.policy (セキュリティポリシーの設定ファイル)。
/var/log/tomcat7 ログファイル。systemd からは処理されません (#ログを参照)。
/var/lib/tomcat7/webapps Tomcat がウェブアプリケーションをデプロイするフォルダ。
/var/tmp/tomcat7 Tomcat がウェブアプリのデータを保存するフォルダ。

初期設定

マネージャウェブアプリや管理者ウェブアプリを使用するには次のファイルを編集する必要があります: /etc/tomcat7/tomcat-users.xml

XML 宣言の "role and user" をアンコメントして、必要に応じてロール tomcat, {{Ic|admin-{gui,script} }}, {{Ic|manager-{gui,script,jmx,status} }} を有効にしてください (Configuring Manager Application Access を参照)。簡単に説明すると、tomcat は tomcat を実行するのに必須のロールで、manager-* はウェブアプリケーションを管理することができるロール、admin-* は Tomcat サーバーに関する全ての権限を持つ管理者です。

上記のロールとユーザー名、パスワードを定義すると以下のようになります ([CHANGE_ME] は適当なパスワードに置き換えてください):

/etc/tomcat7/tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <role rolename="manager-jmx"/>
  <role rolename="manager-status"/>
  <role rolename="admin-gui"/>
  <role rolename="admin-script"/>
  <user username="tomcat" password="[CHANGE_ME]" roles="tomcat"/>
  <user username="manager" password="[CHANGE_ME]" roles="manager-gui,manager-script,manager-jmx,manager-status"/>
  <user username="admin" password="[CHANGE_ME]" roles="admin-gui"/>
</tomcat-users>

上記のファイルに修正を加えたら、都度、Tomcat を再起動する必要があります。

ロールについては このブロク記事 に詳しい説明があります。

IDE と統合するために設定ファイルの読み込み権限を設定するには、tomcat グループ (使用しているバージョンによって tomcat6, tomcat7, tomcat8 となります) にユーザーを追加してください:

 gpasswd -a <user> tomcat<number>

Tomcat の起動・停止

次のコマンドで Tomcat を起動できます:

# systemctl start tomcat7

Tomcat を起動したら、次のページを開いて動作していることを確認します: http://localhost:8080。素敵な Tomcat のローカルホームページが表示されたら、Servlet コンテナが問題なく立ち上がっています。起動スクリプトが上手く動作しなかった場合、ブラウザには Java のエラーだけが表示されます。systemd の journalctl を使って起動ログを確認してください。Tomcat のログに書かれている問題の答えは全て Google にあります。

ノート: セキュリティを確保するために、Arch Linux の Tomcat パッケージは Apache の common-daemonsjsvc バイナリを使っています。Tomcat の systemd サービスは Apache のバイナリを root 権限で実行しますが、Tomcat は非特権ユーザーで起動されます (Arch Linux の場合 tomcat7:tomcat7)。これによって、悪意のあるウェブアプリケーションによる攻撃コードの実行を防ぐことができます。また、必要な場合、1024番ポートが使われます。/etc/conf.d/tomcat7CATALINA_OPTS 環境変数で指定することができるオプションについては man jsvc を参照してください。

手動で操作する方法

上流のスクリプトを使って Tomcat を直接制御することもできます:

/usr/share/tomcat/bin/{startup.sh,shutdown.sh,..}

アプリケーションや Tomcat をデバッグするときは上記のコマンドが役に立ちますが、一番最初に Tomcat を起動するときには systemd を使ってください。パーミッションが間違って設定されてウェブアプリが実行できなくなる可能性があります。また、上記のスクリプトを使用するには多少の設定が必要になります。上のノートに書かれている jsvc のセキュリティ機構が使えなくなることにも注意してください。

ウェブアプリケーションのデプロイと管理

Tomcat 7 には5つのウェブアプリケーションがバンドルされています (場合によっては localhost をサーバーの FQDN に変えてください):

GUI を使用する

マネージャウェブアプリ http://localhost:8080/manager/html を使用するのが一番簡単な方法です。tomcat-users.xmlmanager に定義したユーザー名・パスワードを使ってください。ログインしたら、既に5つのウェブアプリケーションがデプロイされていることが確認できます。"Deploy" エリアからアプリケーションを追加して "Applications" エリアでアプリケーションを停止・起動・削除します。

CLI を使用する

WAR ファイルをアプリケーションディレクトリにコピーする方法もあります: /usr/share/tomcat7/webapps。その場合、以下のように適切なホストで autoDeploy オプションが設定されていることを確認してください:

/etc/tomcat7/server.xml
...
<Host name="localhost"  appBase="webapps"
      unpackWARs="true" autoDeploy="true">
...

webapps フォルダ以外にあるファイルをホスト

If you want to keep your project outside the webapps folder this is possible by creating a Context. Go to /etc/tomcat<number>/Catalina/localhost/ and create your context. A context is a simple xml file which specifies where tomcat should look for the project. The basic format of the file is

/etc/tomcat7/Catalina/localhost/whatShouldFollowLocalhost.xml
<Context path="/whatSholdFollwLocalhost" docBase="/where/your/project/is/" reloadable="true"/>

A working example is as follows. This assumes that the project is hosted somewhere in the users /home-folder.

/etc/tomcat7/Catalina/localhost/myProject.xml
<Context path="/myProject" docBase="/home/archie/code/jsp/myProject" reloadable="true"/>

The files can now be hosted in /home/archie/code/jsp/myProject/. To see the project in your webbrowser, go to http://localhost:8080/myProject. If tomcat is unable to load the files, it might be an issue with permissions. chmod o+x /home/archie/code/jsp/myProject should fix the issue.

ログ

公式の Arch Linux パッケージの Tomcat は systemd の journalctl で起動ログを記録します。'/var/log/tomcat7/catalina.err/var/log/tomcat7/catalina.out ファイルは使われません。/etc/tomcat7/server.xmlValve と定義されている、アクセスログやビジネスログなどのログはデフォルトのまま /var/log/tomcat7/ に記録されます。

上流のようにログを保存したい場合、systemd ファイル /lib/systemd/system/tomcat7.service/etc/systemd/system/tomcat7.service にコピーして、ログファイルの絶対パスを指定する SYSLOG を変更してください。

Tips and tricks

Basic configuration can be made through the virtual host manager web application: http://localhost:8080/host-manager/html. Provide the username/password you set in tomcat-users.xml. Other options are tweaked in configuration files in /etc/tomcat7, the most important being server.xml. Using these files is out of the scope of this 101 wiki page. Please have a look at the official Tomcat 7 documentation for more details.

前のバージョンの Tomcat からの移行

As said in the introduction, Tomcat 7 does not deprecate Tomcat 6. They are all three, implementations of Servlet/JSP standards. Hence you must first determine which version of Tomcat you need depending on the versions of Servlet/JSP your application uses. If you need to migrate, the official website gives instructions on how to handle such a process.

別の JRE/JDK で Tomcat を使用する

推奨されている JRE/JDK をインストールしない場合、Tomcat の systemd サービスファイルで TOMCAT_JAVA_HOME 変数を設定してください。

Systemd#ユニットファイルの編集に書かれているように、カスタム設定で変数を上書きすることができます:

  1. /etc/systemd/system/tomcat7.service.d ディレクトリを作成
  2. 作成したディレクトリの中に以下の内容で start.conf ファイルを保存 (Oracle の JDK パッケージ jdkAUR を使用する場合、/usr/lib/jvm/java-8-jdk としてください):
[Service]
Environment=TOMCAT_JAVA_HOME=/usr/lib/jvm/java-8-openjdk

もしくは、サービスファイル /usr/lib/systemd/system/tomcat7.service/etc/systemd/system/ にコピーして次の行を:

Environment=TOMCAT_JAVA_HOME=/usr/lib/jvm/java-7-openjdk

以下のように置き換えてください (例: Oracle JDK):

Environment=TOMCAT_JAVA_HOME=/opt/java

セキュリティ設定

This page gives the bare minimum to get your first web application to run on Tomcat. It is not intended to be the definitive guide to administering Tomcat (it is a job of its own). The official Tomcat website will provide all necessary official matter. One could also refer to this O'Reilly page and this last one. Still, here are some security tips to get you started:

  • Keep your Tomcat installation up to date to get the latest fixes to security issues
  • Remove unwanted default applications such as examples, docs, default home page ROOT ("_" in the manager webapp). This prevents potential security holes to be exploited. Use the manager for that.

For more security you could even remove the host-manager and manager web applications. Keep in mind that the later is useful to deploy web applications.

  • Disable the WAR auto-deploy option. This would prevent someone who gained restricted access to the server to copy a WAR into the /usr/share/java/webapps directory to get it running. Edit server.xml and set the autoDeploy to false:
/etc/tomcat7/server.xml
...
<Host name="localhost"  appBase="webapps"
      unpackWARs="true" autoDeploy="false">
...
  • Anonymize Tomcat's default error page to prevent potential attackers to retrieve Tomcat's version. To see what Tomcat says by default, just visit an nonexistent page such as http://localhost:8080/I_dont_exist. You get a 404 error page with Tomcat's version at the bottom.

To anonymize this, edit/open the following JAR (Editors like vim can edit zips directly)

/usr/share/tomcat7/lib/catalina.jar

And edit the following file

org/apache/catalina/util/ServerInfo.properties
...
server.info=
server.number=
server.built=
...
  • Disable unused connectors in server.xml
  • Keep restricted access to /etc/tomcat7/server.xml. Only tomcat user and/or root should be able to read and write this.
  • Keep jsvc usage. Do not use upstream startup scripts unless particular reason as explained in the security note above.
  • Use strong different passwords for each user in tomcat-users.xml, give roles to users who really need them and even disable usernames/roles you do not use/need.

One can even crypt tomcat-users.xml passwords using the following upstream script:

/usr/share/tomcat7/bin/digest.sh -a SHA NEW_PASSWORD

This will output something like:

NEW_PASSWORD:b7bbb48a5b7749f1f908eb3c0c021200c72738ce

Paste the hashed part in place of the clear password in tomcat-users.xml and add the following to server.xml:

/etc/tomcat7/server.xml
<Host
  ...
  <Realm
    ...
    className="org.apache.catalina.realm.MemoryRealm" digest="SHA"
    ...
  />
  ...
/>

Note that this may not be relevant because only root and/or tomcat is supposed to have read/write access to that file. If an intruder manages to gain root access then he would not need such passwords to mess with your applications/data anyway. Be sure to keep restricted RW access to that file!

  • Always know what you are deploying