サンプル集  >  other  >  apache+tomcat
apache+tomcat
2009/05/14

Linux 環境で、apache + tomcat の連携をできるようにしたい。 mod_jk というモジュールを使ったことがありますが、Linux でも同じでしょうか。

◆環境
OS Linux 2.6.18-92.el5

まずは、mod_jkをダウンロードします。

tomcat-connectors-1.2.23-src.tar.gz というファイルをダウンロードしました。 ftp でサーバーに送って解凍します。

# tar xvzf tomcat-connectors-1.2.23-src.tar.gz
tomcat-connectors-1.2.23-src/
tomcat-connectors-1.2.23-src/KEYS
tomcat-connectors-1.2.23-src/LICENSE
tomcat-connectors-1.2.23-src/NOTICE
tomcat-connectors-1.2.23-src/BUILD.txt

# ll
合計 92
-rw-r--r--  1 root bin   647  2月  3  2007 BUILD.txt
-rw-r--r--  1 root bin 41514  3月  1  2007 KEYS
-rw-r--r--  1 root bin 11358  2月 24  2004 LICENSE
-rw-r--r--  1 root bin   101  2月 28  2004 NOTICE
drwxr-xr-x  2 root bin  4096  5月 18  2007 conf
drwxr-xr-x 10 root bin  4096  5月 18  2007 docs
drwxr-xr-x  6 root bin  4096  5月 18  2007 jkstatus
drwxr-xr-x 11 root bin  4096  5月 18  2007 native
drwxr-xr-x  2 root bin  4096  5月 18  2007 support
drwxr-xr-x  3 root bin  4096  5月 18  2007 tools
drwxr-xr-x  9 root bin  4096  5月 18  2007 xdocs

フォルダがいっぱいできました。

BUILD.txt というファイルがあるので中を見てみます。

This is a source release of the mod_jk 1.2 web server connector for
Tomcat. Only the web server connector source is included.  The Tomcat
side of the connector is available with the normal Tomcat distributio
n.

Documentation for how to build mod_jk 1.2 from source and configure
it for your webserver is located in the native/BUILDING file in this
source distribution.

For the impatient Apache admins:
$> cd native
$> ./configure --with-apxs=/usr/sbin/apxs (or where ever the apxs/apx
s2 is)
$> make
$> su -c 'make install'

For the impatient SunONE admins:
$> cd native
$> ./configure --enable-netscape
$> cd netscape
$> make -f Makefile.solaris

これに従ってやればよさそうです。

# cd native/
# ./configure --with-apxs=/usr/sbin/apxs
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes

checking whether to use SO_RCVTIMEO with setsockopt()... yes
checking whether to use SO_SNDTIMEO with setsockopt()... yes
need to check for Perl first, apxs depends on it...
checking for perl... /usr/bin/perl
configure: error: Invalid location for apxs: '/usr/sbin/apxs'

エラーがでました。 /usr/sbin/apxs がないというメッセージが表示されています。 確認してみます。

# ll /usr/sbin/apxs
ls: /usr/sbin/apxs: そのようなファイルやディレクトリはありません

確かに無いようです。 探してみます。

# find / -name "apxs"
#

見つかりませんでした。 apxs で検索したところ以下の記載をみつけました。

apxsという、apacheの拡張モジュールをビルドして、インストールしてくれる便利なコマンドを使用します。

更に調べると、apxs は「apache-devel - Development tools for the Apache Web server」というパッケージに含まれることが分かりました。 apache-devel がインストールされているかどうかの確認方法ものっていたので試してみます。

# rpm -qa | grep apxs
#

インストールされていないようです。

httpd-devel-2.2.3-11.el5_1.centos.3.x86_64.rpm というファイルをダウンロードしたので、インストールしてみます。

# yum install httpd-devel-2.2.3-11.el5_1.centos.3.x86_64.rpm
Setting up Install Process
Parsing package install arguments
Examining httpd-devel-2.2.3-11.el5_1.centos.3.x86_64.rpm: httpd-devel
 - 2.2.3-11.el5_1.centos.3.x86_64
Marking httpd-devel-2.2.3-11.el5_1.centos.3.x86_64.rpm to be installe
d
Resolving Dependencies
--> Running transaction check
---> Package httpd-devel.x86_64 0:2.2.3-11.el5_1.centos.3 set to be u
pdated
--> Processing Dependency: apr-devel for package: httpd-devel
--> Processing Dependency: apr-util-devel for package: httpd-devel
--> Running transaction check
---> Package apr-devel.i386 0:1.2.7-11 set to be updated
---> Package apr-util-devel.i386 0:1.2.7-7.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================
========
 Package                 Arch       Version          Repository      
  Size
=====================================================================
========
Installing:
 httpd-devel             x86_64     2.2.3-11.el5_1.centos.3  httpd-de
vel-2.2.3-11.el5_1.centos.3.x86_64.rpm  512 k
Installing for dependencies:
 apr-devel               i386       1.2.7-11         base          
    237 k
 apr-util-devel          i386       1.2.7-7.el5      base          
     54 k

Transaction Summary
=====================================================================
========
Install      3 Package(s)
Update       0 Package(s)
Remove       0 Package(s)

Total download size: 803 k
Is this ok [y/N]:

「y」と入力しEnterを押します。

Downloading Packages:
(1/2): apr-util-devel-1.2 100% |=========================|  54 kB    
00:00
(2/2): apr-devel-1.2.7-11 100% |=========================| 237 kB    
00:00
warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID e8
562897
Importing GPG key 0xE8562897 "CentOS-5 Key (CentOS 5 Official Signing
 Key) <centos-5-key@centos.org>" from http://mirror.centos.org/cent
os/RPM-GPG-KEY-CentOS-5
Is this ok [y/N]:

また選択肢が表示されたので「y」と入力しEnterを押します。

Running rpm_check_debug
Running Transaction Test
Finished Transaction Test


Transaction Check Error:
  package httpd-devel-2.2.3-11.el5_1.centos.3 is intended for a x86_6
4 architecture

Error Summary
-------------

エラーがでました。

パッケージ「httpd-devel-2.2.3-11.el5_1.centos.3」は x86_64 アーキテクチャでないといけないようです。

uname でハードウェア情報を確認します。

# uname -m
i686

ちゃんとインストールできたマシンを見てみます。

# uname -m
x86_64

インストールしようとした「httpd-devel」は x86_64 用だったようです。 今回インストールしようとしたマシンは i686 用でないとインストールできない模様です。 「apxs i686」で検索してみましたがみつかりませんでした。

「rpm httpd-devel」で検索したところダウンロードできそうなページを見つけました。

httpd-devel-2.2.8-3.i386.rpm というファイルをダウンロードしてインストールしてみます。

# yum install httpd-devel-2.2.8-3.i386.rpm
Setting up Install Process
Parsing package install arguments
Examining httpd-devel-2.2.8-3.i386.rpm: httpd-devel - 2.2.8-3.i386
Marking httpd-devel-2.2.8-3.i386.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package httpd-devel.i386 0:2.2.8-3 set to be updated
--> Processing Dependency: apr-devel for package: httpd-devel
--> Processing Dependency: apr-util-devel for package: httpd-devel
--> Processing Dependency: httpd = 2.2.8-3 for package: httpd-devel
--> Running transaction check
---> Package httpd-devel.i386 0:2.2.8-3 set to be updated
--> Processing Dependency: httpd = 2.2.8-3 for package: httpd-devel
---> Package apr-devel.i386 0:1.2.7-11 set to be updated
---> Package apr-util-devel.i386 0:1.2.7-7.el5 set to be updated
--> Finished Dependency Resolution
Error: Missing Dependency: httpd = 2.2.8-3 is needed by package httpd
-devel

エラーがでました。 依存エラー:httpd = 2.2.8-3 は httpd-devel パッケージが必要なのでしょうか。

調べてたら、httpd-devel だけでインストールしているのをみかけましたのでやってみます。

# yum install httpd-devel
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package httpd-devel.i386 0:2.2.3-22.el5.centos set to be updated
--> Processing Dependency: apr-util-devel for package: httpd-devel
--> Processing Dependency: apr-devel for package: httpd-devel
--> Processing Dependency: httpd = 2.2.3-22.el5.centos for package: h
ttpd-devel
--> Running transaction check
---> Package apr-devel.i386 0:1.2.7-11 set to be updated
---> Package httpd.i386 0:2.2.3-22.el5.centos set to be updated
---> Package apr-util-devel.i386 0:1.2.7-7.el5 set to be updated
--> Processing Dependency: httpd = 2.2.3-11.el5_1.centos.3 for packag
e: mod_ssl
--> Running transaction check
---> Package mod_ssl.i386 1:2.2.3-22.el5.centos set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================
========
 Package                 Arch       Version          Repository        
Size
=====================================================================
========
Installing:
 httpd-devel             i386       2.2.3-22.el5.centos  base        
      145 k
Updating:
 httpd                   i386       2.2.3-22.el5.centos  base        
      1.2 M
Installing for dependencies:
 apr-devel               i386       1.2.7-11         base            
  237 k
 apr-util-devel          i386       1.2.7-7.el5      base            
   54 k
Updating for dependencies:
 mod_ssl                 i386       1:2.2.3-22.el5.centos  base      
         87 k

Transaction Summary
=====================================================================
========
Install      3 Package(s)
Update       2 Package(s)
Remove       0 Package(s)

Total download size: 1.7 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): httpd-2.2.3-22.el5 100% |=========================| 1.2 MB    
00:00
(2/3): mod_ssl-2.2.3-22.e 100% |=========================|  87 kB    
00:00
(3/3): httpd-devel-2.2.3- 100% |=========================| 145 kB    
00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Updating  : httpd                        ######################### 
[1/7]
  Installing: apr-devel                    ######################### 
[2/7]
  Installing: apr-util-devel               ######################### 
[3/7]
  Installing: httpd-devel                  ######################### 
[4/7]
  Updating  : mod_ssl                      ######################### 
[5/7]
  Cleanup   : mod_ssl                      ######################### 
[6/7]
  Cleanup   : httpd                        ######################### 
[7/7]

Installed: httpd-devel.i386 0:2.2.3-22.el5.centos
Dependency Installed: apr-devel.i386 0:1.2.7-11 apr-util-devel.i386 0
:1.2.7-7.el5
Updated: httpd.i386 0:2.2.3-22.el5.centos
Dependency Updated: mod_ssl.i386 1:2.2.3-22.el5.centos
Complete!

無事インストールできました! 「/usr/sbin/apxs」にちゃんとファイルができています!

もう一度、tomcat-connector のインストールを再開します。

# ./configure --with-apxs=/usr/sbin/apxs
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes

config.status: creating common/list.mk
config.status: creating common/jk_types.h
config.status: creating jni/Makefile
config.status: creating common/portable.h
config.status: executing depfiles commands

特にエラーはでていないようなので、makeします!

# make
Making all in common

インストールします。

5分ぐらいかかりましたが、特にエラーはでませんでした。

# cd apache-2.0/
# apxs -n jk -i mod_jk.so
/usr/lib/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib/apr-1/build/libt
ool' mod_jk.so /usr/lib/httpd/modules
/usr/lib/apr-1/build/libtool --mode=install cp mod_jk.so /usr/lib/htt
pd/modules/
cp mod_jk.so /usr/lib/httpd/modules/mod_jk.so
Warning!  dlname not found in /usr/lib/httpd/modules/mod_jk.so.
Assuming installing a .so rather than a libtool archive.
chmod 755 /usr/lib/httpd/modules/mod_jk.so

Warning がでていますがインストールを進めます。

続いて mod_jk.conf を作成します。

mod_jk.conf /etc/httpd/conf.d
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
<IfModule !mod_jk.so>
# Load mod_jk module
LoadModule  jk_module   /etc/httpd/modules/mod_jk.so
JkWorkersFile   conf/workers.properties

# Where to put jk logs
JkLogFile   /home/WORK/logs/httpd_log/mod_jk_log

# Set the jk log level[debug/error/info]
JkLogLevel  warn

# Send to Tomcat URL file
JkMountFile /etc/httpd/conf/reqmap

workers.properties /etc/httpd/conf
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
worker.list=jkstatus,lb
worker.jkstatus.type=status

worker.ymprap.type=ajp13
worker.ymprap.host=ymprap
worker.ymprap.port=8009
worker.ymprap.retries=10
worker.ymprap.lbfactor=10

worker.lb.type=lb
worker.lb.balance_workers=ymprap

reqmap /etc/httpd/conf
1: 
2: 
/servlet/*=lb
/test/*=lb

続いて Java を導入します。

jre-6u2-linux-i586.bin というファイルをダウンロードして実行します。

# ./jre-6u2-linux-i586.bin
-bash: ./jre-6u2-linux-i586.bin: 許可がありません

chmod で権限を付与して再度実行します。

# chmod 777 jre-6u2-linux-i586.bin
# ./jre-6u2-linux-i586.bin
Sun Microsystems, Inc. Binary Code License Agreement

for the JAVA SE RUNTIME ENVIRONMENT (JRE) VERSION 6

SUN MICROSYSTEMS, INC. ("SUN") IS WILLING TO LICENSE THE

Do you agree to the above license terms? [yes or no]

yesと入力しEnterを押します。

Unpacking...
Checksumming...
Extracting...
UnZipSFX 5.50 of 17 February 2002, by Info-ZIP (Zip-Bugs@lists.wku.ed
u).
   creating: jre1.6.0_02/
   creating: jre1.6.0_02/bin/

できたフォルダを丸ごと、/usr/local へコピーしてリンクを作成します。

# mv jre1.6.0_02/ /usr/local/
# cd /usr/local/
# ln -s /usr/local/jre1.6.0_02/ java

続いて Tomcat を導入します。

apache-tomcat-5.5.23.tar.tar ファイルを解凍します。

# tar zxvf apache-tomcat-5.5.23.tar.tar
apache-tomcat-5.5.23/bin/catalina.sh
apache-tomcat-5.5.23/bin/digest.sh
apache-tomcat-5.5.23/bin/setclasspath.sh
apache-tomcat-5.5.23/bin/shutdown.sh
apache-tomcat-5.5.23/bin/startup.sh

/usr/local にフォルダを移動しリンクを作成します。

# mv apache-tomcat-5.5.23 /usr/local/
# ln -s /usr/local/apache-tomcat-5.5.23/ tomcat

環境設定シェルを作成します。

setenv.sh /usr/local/tomcat/bin
 1: 
 2: 
 3: 
 4: 
 5: 

 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 

13: 

#if["`/usr/bin/whoami`"!="tomcat"];then
#    echo
#    echo "It is necessary to start by the tomcat user."
#    echo
#    echo "please try /etc/init.d/tomcat {start/stop}, by root.
"
#    echo
#    exit
#fi
export LANG=C
JAVA_HOME=/usr/local/java
CATALINA_HOME=/usr/local/tomcat
#CATALINA_OPTS="-server -Xms2048m -Xmx2048m -XX:MaxPermSize=102
4M"
#CATALINA_OPTS="$CATALINA_OPTS -XX:+PrintGCDetails -Xloggc:$CAT
ALINA_HOME/logs/gc.log"

tomcat 起動・停止シェルを作成します。

tomcat /etc/init.d
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
#!/bin/sh
# startup script for tomcat
#
# chkconfig: - 80 15
# description:Tomcat is a Servlet+JSP Engine
#
# pidfile:/var/run/tomcat.pid

# source functionlibrary
. /etc/init.d/functions

CATALINA_HOME=/usr/local/tomcat
RETVAL=0

case "$1" in
  start)
#    su - tomcat -c "$CATALINA_HOME/bin/startup.sh"
    "$CATALINA_HOME/bin/startup.sh"
    ;;
  stop)
#    su - tomcat -c "$CATALINA_HOME/bin/shutdown.sh"
    "$CATALINA_HOME/bin/shutdown.sh"
    rm -f /var/run/tomcat.pid
    ;;
  *)
    echo $"Usage: $0 {start|stop}"
    RETVAL=1
esac

exit $RETVAL

URL にアクセスしましたが、apache のページが表示されました。 ポート 8080 を指定すると tomcat のページが表示されるので、mod_jk が正しく起動していないようです。

httpd.conf に mod_jk の記入をしなければならないようです。

# mod_jkのロード
LoadModule jk_module modules/mod_jk.so
# mod_jkの設定ファイルを指定
<IfModule mod_jk.c>
  Include conf.d/mod_jk.conf
</IfModule>

apacheを再起動します。

# /etc/init.d/httpd stop
httpd を停止中:                                            [  OK  ]
# /etc/init.d/httpd start
httpd を起動中: [Thu May 14 14:44:59 2009] [warn] module jk_module is
 already loaded, skipping
[Thu May 14 14:44:59 2009] [warn] module jk_module is already loaded,
 skipping
httpd: Could not reliably determine the server's fully qualified doma
in name, using 127.0.0.1 for ServerName
                                                           [失敗]

jk_module は既に読み込まれているのでスキップしたようなメッセージが表示されました。

エラーは翻訳したところ「ServerNameに"127.0.0.1"を使用して、サーバの完全修飾ドメイン名を確かに決定できませんでした。」ということでした。

警告は、恐らく httpd.conf に jk_module のロードが2行あるのだと思います。

エラーは、worker.properties の記述が間違っているようです。

現時点では「ymlibrealprep」サーバーは無いので、これを「ymlibrealweb」に修正したら正常起動しました。 でも tomcat にはフォワードされていないようです。

調べたところ、tomcat 自体に webサーバーの機能があるので、その機能を止めないとダメみたいです。

次の行をコメントアウトしてみます。

    <Connector port="8080" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads=
"75"
               enableLookups="false" redirectPort="8443" acceptCount=
"100"
               connectionTimeout="20000" disableUploadTimeout="true" 
/>

コメントアウトしましたが特に変化はありませんでしたので元に戻します。

他のページを参考に、workers.properties を書き換えてみました。

workers.properties /etc/httpd/conf
1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
workers.tomcat_home=/usr/local/tomcat
workers.java_home=/usr/local/java

worker.list=ajp13

worker.ajp13.port=8009
worker.ajp13.host=ymweb
worker.ajp13.type=ajp13

これに伴い、JkMountFile で指定している、reqmap も修正します。

reqmap /etc/httpd/conf
1: 
2: 
/servlet/*=ajp13
/test/*=ajp13

以下のパターンでURLにアクセスしてみましたが上手くいきませんでした。

URL結果
http://192.168.1.1/apache のページが開く
http://192.168.1.1/servletページが見つかりません。
http://192.168.1.1/servlet/HTTP Status 404 - /servlet/
http://192.168.1.1:8009/ページを表示できません。
http://192.168.1.1:8009/servletページを表示できません。
http://192.168.1.1:8009/servlet/ページを表示できません。
http://192.168.1.1:8080/tomcat のページが開く

「HTTP Status 404」になったURLが気になります。 もしかすると、tomcat の設定が間違っているからかもしれません。

apache、tomcat のログを見ていたら、8009 にアクセスしたときにtomcatのログが更新されていました。

URL結果
http://192.168.1.1/apache の error.log に「Directory index forbidden by Options directive: /var/www/html/」が出た。
http://192.168.1.1/servletapache の error.log に「File does not exist: /var/www/html/servlet」が出た。
http://192.168.1.1/servlet/apache access.log 更新。
http://192.168.1.1:8009/tomcat の catalina.out に以下のエラーが出た。
http://192.168.1.1:8009/servlettomcat の catalina.out に以下のエラーが出た。
http://192.168.1.1:8009/servlet/tomcat の catalina.out に以下のエラーが出た。
http://192.168.1.1:8080/tomcat のページが開く

エラーは以下になります。

May 14, 2009 4:36:43 PM org.apache.jk.common.MsgAjp processHeader
SEVERE: BAD packet signature 18245
May 14, 2009 4:36:43 PM org.apache.jk.common.ChannelSocket processCon
nection
SEVERE: Error, processing connection
java.lang.IndexOutOfBoundsException
        at java.io.BufferedInputStream.read(Unknown Source)
        at org.apache.jk.common.ChannelSocket.read(ChannelSocket.java
:626)
        at org.apache.jk.common.ChannelSocket.receive(ChannelSocket.j
ava:583)
        at org.apache.jk.common.ChannelSocket.processConnection(Chann
elSocket.java:691)
        at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(
ChannelSocket.java:895)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.
run(ThreadPool.java:685)
        at java.lang.Thread.run(Unknown Source)

状況から考えると、ポート 8009 にアクセスすると tomcat にフォワードされているようです。 フォワードというより、tomcat が子プロセス(=ワーカー?)として生成され 8009 ポート待ちになっている感じがします。

ということは、tomcat が 8009 ポートを受け付けていれば大丈夫なはず。 server.xml を見てみます。

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009"
               enableLookups="false" redirectPort="8443" protocol="AJ
P/1.3" />

よく分からないですが、見た感じではポート 8009 へのアクセスをポート 8443 へリダイレクトしているように見えます。 以下のように書き換えて再度テストします。

    <Connector className="org.apache.ajp.tomcat4.Ajp13Connector"
               port="8009" minProcessors="5" maxProcessors="75"
               acceptCount="10" debug="0"/>

上手く動作しました!結果は以下の通りです。

URL結果
http://192.168.1.1/apache のページが開く
http://192.168.1.1/servletページが見つかりません。
http://192.168.1.1/servlet/応答が返って来ない。
http://192.168.1.1:8009/tomcat のページが表示される。
http://192.168.1.1:8009/servletHTTP Status 404 - /servlet
http://192.168.1.1:8009/servlet/HTTP Status 404 - /servlet/
http://192.168.1.1:8080/tomcat のページが開く

ポート 8009 へのアクセスで tomcat のページが表示されました。 ということは、apache -> mod_jk -> tomcat の連携は上手く行っていると判断できます。

servlet へのアクセスができないのはなぜでしょうか。 JkMountFile の設定が効いていないのでしょうか。

応答が返ってこないのをずっと待ってたらこんなエラーがでました。

Service Temporarily Unavailable
The server is temporarily unable to service your request due to maint
enance downtime or capacity problems. Please try again later.


---------------------------------------------------------------------
-----------

Apache/2.2.3 (CentOS) Server at 192.168.1.1 Port 80

▲ PageTop  ■ Home


Copyright (C) 2009 - 2025 ymlib.com