ResinはJavaベースの高性能アプリケーションサーバーであり、ServletやJSPの実行環境を提供するだけでなく、PHPの実行やキャッシュ機能、負荷分散を含むクラスタ構成もサポートしています。本記事では、Resinの基本的なインストール手順から始まり、Apache HTTP Serverとの連携による動的コンテンツと静的コンテンツの分離処理、そして複数のバーチャルホスト運用までを解説します。
Java実行環境のセットアップ
Resinを実行するには、事前にJDK(Java Development Kit)を導入する必要があります。以下の手順でJDKを展開し、環境変数を設定します。
# JDKアーカイブの展開とシンボリックリンク作成
tar -xzf jdk-8uXXX-linux-x64.tar.gz -C /opt/
ln -s /opt/jdk1.8.0_XXX /opt/java
# 環境変数の設定 (/etc/profile または ~/.bash_profile に追記)
cat << 'EOF' >> /etc/profile
# Java Environment Configuration
export JAVA_HOME=/opt/java
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
EOF
# 設定の反映とバージョン確認
source /etc/profile
java -version
Resinのインストールと基本設定
次に、Resinのアーカイブを展開し、ディレクトリ構成を整えます。設定ファイルである resin.conf を編集し、サーバーインスタンスの定義を行います。
# ソフトウェアの展開と配置
cd /usr/local/src/
tar -xzf resin-4.0.XX.tar.gz
mv resin-4.0.XX /opt/resin
cd /opt/resin
# 設定ファイルの編集 (resin.conf)
vim conf/resin.conf
resin.conf 内のサーバー定義部を以下のように修正します。ここでは、サーバーIDを app-srv01、監視ポートを 6900、サーバーポートを 6800 として設定し、Webサーバー用のポートを 8080 に設定しています。JVMのヒープサイズも適切に調整してください。
<!-- Resin Server Definition -->
<server id="app-srv01" address="192.168.1.10" port="6800" watchdog-port="6900">
<!-- HTTP服务端口 -->
<http address="*" port="8080"/>
<!-- JVM Arguments -->
<jvm-arg>-Xms512m</jvm-arg> <!-- 初期ヒープサイズ -->
<jvm-arg>-Xmx1024m</jvm-arg> <!-- 最大ヒープサイズ -->
<jvm-arg>-Xdebug</jvm-arg>
<jvm-arg>-Dcom.sun.management.jmxremote</jvm-arg>
<!-- Thread and Memory Settings -->
<thread-max>512</thread-max>
<socket-timeout>60s</socket-timeout>
<keepalive-max>256</keepalive-max>
<keepalive-timeout>20s</keepalive-timeout>
</server>
Resinの起動と動作確認
設定完了後、Resinを起動し、プロセスとポートリッスン状態を確認します。
# Resinの起動 (定義したサーバーIDを指定)
/opt/resin/bin/resin.sh start -server app-srv01
# プロセスとポートの確認
netstat -lntup | grep -E "6800|6900|8080"
ps -ef | grep java
動作確認のため、簡易的なJSPファイルを作成してアクセスします。
# テストページの作成
echo "<%= 10 * 10 %>" > /opt/resin/webapps/ROOT/calc.jsp
# カールコマンドによる確認
curl http://192.168.1.10:8080/calc.jsp
# 出力: 100
サービス管理スクリプトの作成
OS起動時の自動実行やサービス管理を容易にするため、SysVスタイルの起動スクリプトを作成します。
#!/bin/sh
# chkconfig: 35 85 15
# description: Resin Application Server Control Script
# Source function library
. /etc/rc.d/init.d/functions
RESIN_HOME="/opt/resin"
RESIN_USER="root"
LOG_DIR="/var/log/resin"
EXEC_CMD="$RESIN_HOME/bin/resin.sh -server app-srv01"
# Ensure log directory exists
[ ! -d "$LOG_DIR" ] && mkdir -p "$LOG_DIR"
start() {
echo -n "Starting Resin: "
daemon --user "$RESIN_USER" $EXEC_CMD start >> "$LOG_DIR/startup.log" 2>&1
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/resin
return $RETVAL
}
stop() {
echo -n "Stopping Resin: "
killproc java
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/resin
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $RETVAL
このスクリプトを /etc/init.d/resin として保存し、実行権限を付与して chkconfig に登録します。
chmod +x /etc/init.d/resin
chkconfig --add resin
chkconfig resin on
Apache HTTP Serverとの連携
静的リソースの配信をApacheに担当させ、動的リクエスト(JSP/Servlet)のみをResinに転送する構成を構築します。これには mod_caucho モジュールを使用します。
まず、Apacheをインストールします(必要なライブラリも併せて導入)。
yum install -y zlib-devel libxml2-devel pcre-devel openssl-devel
cd /usr/local/src/
httpd_version="2.4.XX"
wget http://archive.apache.org/dist/httpd/httpd-${httpd_version}.tar.gz
tar -xzf httpd-${httpd_version}.tar.gz
cd httpd-${httpd_version}
./configure --prefix=/opt/apache \
--enable-so \
--enable-rewrite \
--enable-ssl \
--with-mpm=worker
make && make install
続いて、Resinのソースコードに含まれる mod_caucho をコンパイルし、Apacheに組み込みます。
# mod_cauchoのコンパイルとインストール
cd /usr/local/src/resin-4.0.XX/modules/c/src
./configure --with-apxs=/opt/apache/bin/apxs
make && make install
# モジュールの確認
ls -l /opt/apache/modules/mod_caucho.so
Apacheの設定ファイル httpd.conf の末尾に連携設定を追加します。ここでは、Resinサーバー(ポート6800)へリクエストを転送するよう設定します。
# vim /opt/apache/conf/httpd.conf
LoadModule caucho_module modules/mod_caucho.so
# Resin Config
ResinConfigServer 192.168.1.10 6800
CauchoConfigCacheDirectory /tmp
CauchoStatus yes
設定後、Apacheを再起動して動作を確認します。ApacheのDocumentRootにHTMLを、ResinのROOTディレクトリにJSPを配置し、それぞれが正しく処理されるか検証します。
# Apacheコンテンツ
echo "Static Content from Apache" > /opt/apache/htdocs/index.html
# Resinコンテンツ
echo "<%= 'Dynamic Content from Resin' %>" > /opt/resin/webapps/ROOT/index.jsp
# テスト実行
curl http://192.168.1.10/index.html # Apacheが応答
curl http://192.168.1.10/index.jsp # Resinが応答
ApacheバーチャルホストとResinの連携
複数のドメインで異なるアプリケーションを動作させる場合、Apacheのバーチャルホスト設定とResinのホスト定義を連携させます。
Apache設定 (extra/httpd-vhosts.conf):
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot "/var/www/vhosts/www"
DirectoryIndex index.html index.jsp
<Directory "/var/www/vhosts/www">
AllowOverride None
Require all granted
</Directory>
# Resinへの転送設定
ResinConfigServer 192.168.1.10 6800
</VirtualHost>
<VirtualHost *:80>
ServerName blog.example.com
DocumentRoot "/var/www/vhosts/blog"
DirectoryIndex index.html index.jsp
<Directory "/var/www/vhosts/blog">
AllowOverride None
Require all granted
</Directory>
# 別のResinインスタンスまたは設定へ転送
ResinConfigServer 192.168.1.10 6801
</VirtualHost>
Resin設定 (resin.conf):
Apacheのバーチャルホストに対応するため、Resin側でも <host> タグを使用してドメインごとの設定を行います。マルチインスタンス構成にする場合は、<server> ブロックを複数定義し、ポートを分けます。
<!-- Server 1 for www.example.com -->
<server id="web-srv01" address="192.168.1.10" port="6800" watchdog-port="6900">
<jvm-arg>-Xmx512m</jvm-arg>
<!-- Host Definition -->
<host id="www.example.com" root-directory="/opt/resin/webapps">
<web-app id="/" root-directory="/opt/resin/webapps/www-root"/>
</host>
</server>
<!-- Server 2 for blog.example.com -->
<server id="web-srv02" address="192.168.1.10" port="6801" watchdog-port="6901">
<jvm-arg>-Xmx512m</jvm-arg>
<!-- Host Definition -->
<host id="blog.example.com" root-directory="/opt/resin/webapps">
<web-app id="/" root-directory="/opt/resin/webapps/blog-root"/>
</host>
</server>
この構成により、Apacheがフロントエンドとしてリクエストを受け取り、ドメインに応じて適切なResinインスタンス(またはコンテキスト)へ処理を振り分けることが可能になります。各ディレクトリにテストファイルを配置し、curl コマンドやブラウザを使用して、静的・動的コンテンツが正しく分岐されていることを確認してください。